I would really appreciate someone showing me what i am missing in the following example..
in Form2 I try to access a Control from Form1. (in this case a ComboBox)
Option 1 works and the control is accessible.
This is when Form1 was initially opened/shown using the Form1 itself:
Dim myForm1 As Form1
MyForm1 = New Form1() < xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
result = Form1.ShowDialog ‘Here I am calling/Showing Form1
Form1.refresh()
.
.
Public Class Form2
Inherits System.Windows.Forms.Form
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MessageBox.Show(" ComboBox from Form1= " & Form1.TeamComboBox1.Text)
Option 2 does not work and the control is inaccessible
This is when Form1 was initially opened/shown using an instance:
Dim myForm1 As Form1
MyForm1 = New Form1() 'create instance of form2
result = MyForm1.ShowDialog ‘Here I am calling/Showing MyForm1(instance of Form1).
.
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MessageBox.Show(" ComboBox from Form1= " & MyForm1.TeamComboBox1.Text)
Please could someone tell me the correct method or what is not working.
Thanks a lot

Pass controls from Form1 to form2 - option 1 works, option 2 does not?
Yilmaz Ay
Thanks for your help.
This clears up something I did not understand regarding the existance of the default instance of the form.
I will stick to the default instances and this will assure the desired result in my case.
I find that sometimes I spend enormous time trying to solve problems
and in this case I had put my project on hold a week ago to research this question.
Thanks to both Ed Tenholder and Huy Nguyen for your help and putting me back to work.
Bernard
jbowers
What happens when you run Option 2
Is MyForm1 in scope
Could you post the entire code
Ed Tenholder
harry_s
Hi,
First, please take a look at this article: http://msdn2.microsoft.com/en-us/library/87y2hdsf.aspx.
With the Application Framework in Visual Basic 2005, the My.Forms object provides an instance of each form in your project. So for your case, you have My.Forms.MainForm, My.Forms.Form1 and My.Forms.Form2 as instances that you can access throughout your application. You can also access them by MainForm, Form1, Form2 (that's actually what you are doing here).
In your case:
- In Form2 code, by using Form1, you're accessing the default instance of Form1.
- Option 1: You create a different instance of Form1 (myForm1) and load this instance. Form1 default instance is not loaded, so its ComboBox is still empty. Form2 ComboBox will be empty because it uses Form1 default instance.
- Option 2: You replace "myForm1" with "Form1". Now you're loading Form1 default instance and populating its ComboBox. That's why Form2 ComboBox can get the values.
So you can:
- Use the default instances through your application (the easy option), for example you only need this in MainForm's OpenForm1Button_Click:
Dim result As DialogResult = Form1.ShowDialog
and this in Form1's OpenForm2Button_Click
Dim result As DialogResult = Form2.ShowDialog()
- Create other instances of the forms. In this case, you have to pass myForm1 instance to Form2 by setting a propery or through a constructor.
Best regards,
SvenP
Hello Ed,
In option 1, I do read the content of comboBox from Form1 successfully.
In my program, I actually go further and pass across the content of the ComboBox Drop-down list as well.
But that only happens when Form1 was initially opened as shown in option1.
If I open Form1 as an instance as shown in option 2, then nothing is reachable from Form2.
My problem is, eveywhere I read up on the subject seems to indicate that I should be opening the Instance of forms. And that does not work.
I think I am missing something in my code or obviously I do not understand something.
By the way, all my forms have this line:
Inherits System.Windows.Forms.Form
at the front.
Bernard
tnorth
hi Ed,
Thanks for your persistence, and thanks a lot for trying to help me.
Here is a simplified version of the code:
MainForm: with a button to open Form1 (this is where the form1 can either be opened as an instance or as the form itself)-Version1 or 2.
Public
Class mainForm Inherits System.Windows.Forms.Form Private Sub mainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub OpenForm1Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenForm1Button.Click Dim myForm1 As Form1 Dim result As DialogResultmyForm1 =
New Form1() 'create instance of formresult = myForm1.ShowDialog
'Close form and set instance of form to nothingmyForm1 =
Nothing End SubEnd
ClassFORM1: with a ComboBox named ComboBox and a Button to open Form2
Public
Class Form1 Inherits System.Windows.Forms.Form Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadComboBox.Text =
"20" 'Just a dummy value for testing and filling ComboBox displayComboBox.Items.Clear()
'zero drop down list Dim ic As Integer = 0 Do While ic < 11 '# of items in listComboBox.Items.Add(ic.ToString())
'Place something in DROP-DOWN LIST (JUST FOR TESTING)ic = ic + 1
Loop End Sub Private Sub ComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox.SelectedIndexChanged End Sub Private Sub OpenForm2Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenForm2Button.Click Dim myForm2 As Form2myForm2 =
New Form2() Dim result As DialogResultresult = myForm2.ShowDialog
myForm2 =
Nothing
End SubEnd
ClassFORM2: with a ComboBox named ComboBoxForm2 which should receive the data from Form1.ComboBox.
Public
Class Form2 Inherits System.Windows.Forms.Form Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadMessageBox.Show(
" ComboBox from Form1= " & Form1.ComboBox.Text) 'Fill Form2 ComboBox drop-down list with Form1 dataComboBoxForm2.Items.Clear()
Dim ic As Integer = 0 If Not Form1.ComboBox.Items Is Nothing Then ' If there is a drop-down list in Form1 ComboBox Do While ic < Form1.ComboBox.Items.Count '# of items in drop-down listComboBoxForm2.Items.Add(Form1.ComboBox.Items.Item(ic))
'Bring it across, starts with index of 0ic = ic + 1
Loop End If 'fill Form2 comboBox window with Form1 data.ComboBoxForm2.Text = Trim(Form1.ComboBox.Text)
End Sub Private Sub ComboBoxForm2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxForm2.SelectedIndexChanged End SubEnd
ClassHERE it is. By changing "myForm1" to "Form1" in the "Form1 call" in MainForm in the 2 lines:
result = myForm1.ShowDialog
'Close form and set instance of form to nothing
myForm1 =
NothingIT WILL WORK ON MY MACHINE. I know I must be missing something.
radone
Are you actually able to successfully execute the Option 1 code
Ed Tenholder