Question multiple instances of the same form

alicecooper1203

New member
Joined
Feb 16, 2014
Messages
3
Programming Experience
Beginner
Hi all I have a program that has two forms the first form has 2 buttons and a combobox on it and the second form has a text box

the first button on the first form loads an instance of the second form each time it is pressed (this bit works fine)
the combo and the second button on the first form is to select one of the instances of the second form created and put some text in the textbox on the selected second form. when I run it as I said previously the first button runs fine but the second throws the error:-An unhandled exception of type 'System.MissingMemberException' occurred in Microsoft.VisualBasic.dll
Additional information: Public member 'txt1' on type 'Form2' not found

Public
tester As New ArrayList


Public frmmessage As New Form2


Public formcount As Integer



Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


frmmessage =
New Form2

frmmessage.Text = formcount

frmmessage.txt1.Text =
"hello"

tester.Add(frmmessage)

tester(formcount).show()

ComboBox1.Items.Add(formcount)

ComboBox1.SelectedIndex = 0

formcount += 1



End Sub



Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click



Dim x As Integer


x = ComboBox1.SelectedIndex

tester(x).txt1.Text =
"goodbye"



End Sub


sorry cant make it post properly but hopefully u get the gist any help appreciated

many thanks coop
 
Last edited:
what the hell happened to the code!!!!!!!!!!!!

You have pasted it as HTML. In future, paste it as plain text inside formatting tags, e.g. this:

[xcode=vb]Dim n As Integer = 100[/xcode]

produces this:

Dim n As Integer = 100
 
Hi all I have a program that has two forms the first form has 2 buttons and a combobox on it and the second form has a text box

the first button on the first form loads an instance of the second form each time it is pressed (this bit works fine)
the combo and the second button on the first form is to select one of the instances of the second form created and put some text in the textbox on the selected second form. when I run it as I said previously the first button runs fine but the second throws the error:-An unhandled exception of type 'System.MissingMemberException' occurred in Microsoft.VisualBasic.dll
Additional information: Public member 'txt1' on type 'Form2' not found

Public
tester As New ArrayList


Public frmmessage As New Form2


Public formcount As Integer



Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


frmmessage =
New Form2

frmmessage.Text = formcount

frmmessage.txt1.Text =
"hello"

tester.Add(frmmessage)

tester(formcount).show()

ComboBox1.Items.Add(formcount)

ComboBox1.SelectedIndex = 0

formcount += 1



End Sub



Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click



Dim x As Integer


x = ComboBox1.SelectedIndex

tester(x).txt1.Text =
"goodbye"



End Sub


sorry cant make it post properly but hopefully u get the gist any help appreciated

many thanks coop

The fact that that code compiles at all means that you have Option Strict Off, which is something that you should change ASAP. You should turn Option Strict On in the project properties and also in the IDE options so that it will be On by default for all future projects. Option Strict On forces you to write code where the compiler can tell that all your types work correctly when you build rather than errors like this being picked up at run time.

For a start, when you get an item from an ArrayList, what you get is an Object reference. The compiler doesn't even know that the object is a Form2 instance. If you want to use it as a Form2 then you must cast it as that type. Even then, if Form2 has no public member named 'txt1' then the code will refuse to compile and will therefore not run.

Presumably, you are trying to set the Text of a TextBox named 'txt1' on Form2. That error message means that either there is no TextBox (or anything else for that matter) on Form2 named 'txt1' or else there is but it is not declared Public or Friend. Did you add that TextBox and then make it Private in the Properties window. If so then that's the issue. A Private field cannot be seen from outside the type it's declared in, hence you cannot access that TextBox from outside your Form2 instance.

In actual fact that is the proper way to do it. If you want to learn more, follow the Blog link in my signature below and check out my three-part series of Data Amongst Multiple Forms. One of the earlier parts will show you the easy way, i.e. just make that TextBox Public or Friend and your code will work as is. The last part will show you how to do it the proper way, i.e. make the TextBox Private and add a method that will allow you to pass the text in but still only access the TextBox from inside the form.
 
Casting that was mentioned can be done with DirectCast or CType, for example:
DirectCast(tester(x), Form2).txt1.Text = "goodbye"

You can avoid that and also get rid of the Object type ArrayList and instead use a strongly typed List(Of Form2), as long as you only put Form2 type objects in it.
 
Back
Top