|
-
Sep 19th, 2001, 12:55 PM
#1
Thread Starter
Lively Member
Passing listbox contents from one form to another - HELP!
Ok, I have been trying to figure this out forever. On one form (1) I have a list box and I want to pass its values to a combo box on another form (2) when form (2) loads. The first thing form (2) does onload is to get the list box contents from form (1) and fill the combo box on form (2). I have figured out how to pass just one value but not a collection of values. Assuming that vb.net required the use of propety gets to pass values between forms then I would guess this is how I have to do it, but not sure exactly how. This is apparently much more difficult than it should be. Any help would be GREATLY appreciated....
-
Sep 25th, 2001, 04:41 PM
#2
Are you using webforms or winforms?
-
Nov 12th, 2001, 01:10 PM
#3
Thread Starter
Lively Member
WinForms - This is a desktop application.
-
Nov 12th, 2001, 10:12 PM
#4
I am not sure on this because I have no .net here with me, but I think this may work
This is the Form Load event for your form with the combo box
Private Sub Form_Load()
Dim Frm1 As New Form1
Dim X As Long
For X = 0 To Frm1.List1.ListCount
Combo1.AddItem (Frm1.List1.List(X))
Next
End Sub
Good luck
-
Nov 13th, 2001, 01:10 PM
#5
Thread Starter
Lively Member
I tried it and still a no go. For some reason it is almost like it cannot see the control on the other form. I printed out the count of the items in the form load of the other page and it comes back with 0 even though their are at least 2 or 3 items in the collection. I did something similiar on the same form but passed a single entry, but I had to do this with a property declaration on form 1 and call the property from form 2 to get the value. Just not sure how to do this with a list box?
-
Nov 13th, 2001, 01:11 PM
#6
Thread Starter
Lively Member
Private Sub frmVersion_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Main As New frmMain()
Dim X As Long
For X = 0 To Main.lstConnections.Items.Count
cmbServer.Items.Add(Main.lstConnections.Items(X).Text)
Next
End Sub
-
Nov 19th, 2001, 01:27 AM
#7
Member
Hi Mike,
I had a problem to show a second form in .Net, so I upgraded an app from VB6, 2 forms, on button click, show form 2.
It declares some kind of DefInstance object.
Add this to your second form:
Private Shared m_vb6FormDefInstance As frmZZZ
Private Shared m_InitializingDefInstance As Boolean
Public Shared Property DefInstance() As frmZZZ
Get
If m_vb6FormDefInstance Is Nothing OrElse m_vb6FormDefInstance.IsDisposed Then
m_InitializingDefInstance = True
m_vb6FormDefInstance = New frmZZZ()
m_InitializingDefInstance = False
End If
DefInstance = m_vb6FormDefInstance
End Get
Set(ByVal Value As frmZZZ)
m_vb6FormDefInstance = Value
End Set
End Property
#End Region
In form1, you can use the following:
Dim X As Long
For X = 0 To List1.ListCount
frmZZZ.DefInstance.Combo1.AddItem (List1.List(X))
Next
Hope this helps!
-
Nov 20th, 2001, 07:15 AM
#8
Thread Starter
Lively Member
Thanks Linda_SA. That more or less did it! Kind of a pain in the rump if you ask me. Too bad you can't simply referance a control on another form just by calling it like VB6. Guess that is part of the learning curve.
-
Nov 20th, 2001, 07:27 AM
#9
Member
I suppose it is part of the learning curve, something to get use to. I'm sure that we will need each other's help in the future with .net. 'Til then, good luck, and happy coding!
Linda
-
Nov 20th, 2001, 07:50 AM
#10
Actually it has for a long time been considered bad practice to be able to reach the properties and method of controls from an other form.
A control should always be considered to be private.
In VB.Net this has been enforced so all controls are private to the form that host them.
So if you need to pass values between forms you should write your own wrapper property procedures.
For example: You have a TextBox on Form1 and you need to get the value of that textbox in Form2.
Then you could add the following to Form1:
VB Code:
Public Property TextBoxContent() As String
Get
TextBoxContent = TextBox1.Text
End Get
Set(ByVal Value As String)
TextBox1.Text = Value
End Set
End Property
Since this property is Public it can be reached from Form2
VB Code:
MsgBox Form1.TextBoxContent
Best regards
-
Nov 20th, 2001, 08:02 AM
#11
Member
Hi Joacim,
Of course, you are absolutely right, it is just difficult when you can't write programs like you used to write them, because of changes like this. There is quite a lot to get used to in .net!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|