sending text to textbox on another form

johnadonaldson

Well-known member
Joined
Nov 9, 2005
Messages
48
Programming Experience
10+
I have a couple of USER CONTROLS that I have textboxes on. I want to send text to these textboxs from the main form. I have it set up as follows

MainForm

Private
fOpGen As New GenReg1 '<- user control form

If sDone = "00" Then
Me.TextBox2.Text = "Test1"
fOpGen.TextBox1.Text = "Test1"
fOpGen.Label24.Visible =
True
EndIf
If sDone = "01" Then
Me.TextBox2.Text = "Test2"
fOpGen.Label25.Visible =
True
fOpGen.TextBox2.Text = "Test2"
EndIf

It does not work. I do not see any text show up in the textbox on the user control. Neither do I see the label associated with the textbox go visible.
I know the code is working as I see the Me.TextBox2.Text display correctly.

 
Update

As a test I created another form that is displayed using "Show"

MainForm

Private
fOpTest As New TestForm


Private Sub MenuItem18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem18.Click
'Display Test Window
fOpTest.Show()
End Sub

Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
If sDone = "00" Then
Me.TextBox2.Text = sResult

fOpTest.TextBox1.Text = sResult
End If
If sDone = "01" Then
Me.TextBox2.Text = sResult
fOpTest.TextBox2.Text = sResult
End If
End Sub

When the timer ticks, the textboxes on the new form get updated, but when I do the same thing with a user control form, they do not get updated.

 
In the first block of code you are refering to members of the form, e.g. fOpGen.TextBox1. What object is that? If it's the UserControl itself then have you defined it such that setting its Text property automatically propagates that to the TextBox it contains? If it's the TextBox within the UserControl, it seems very odd to me that you've created a member variable within the form to refer to a constituent of the UserControl. Usually you would hide each individual control within the UserControl, i.e. make them private, and only expose their members through public members of the UserControl itself.
 
Here's what I did. I created a User Control Forms. Populated it with Textboxes and CheckBoxes. I then attached it to a tab on main form.

Now I want while in the mainform to put text into the user control textboxes and check/uncheck the checkboxes. The user control form is called "GenReg1.vb" Thus at the top of the MainForm I put.

Private fOpGen As New GenReg1

I gather the data I want to display in the textboxs and send it as

fOpGen.TextBox1.Text = sResult

I set the CheckBox to TRUE

fOpGen.CheckBox1.Check = TRUE

No text appears in the User Control Form Textbox and neither is the checkbox checked.

Now if I do this with a regular Form, it works OK.
 
So are you saying that fOpGen is a form that contains an instance of your UserControl or that it is itself an instance of your UserControl? A UserControl is NOT a form, so this:
The user control form is called "GenReg1.vb"
indicates that fOpGen is a form that contains your UserControl, but if that's the case then your code makes no sense. Please be clear and try to use accurate terminology. A form is a form and a usercontrol is a usercontrol.
 
Basicly what I am doing is creating a User Control, populate it with TextBoxes and CheckBoxes,
tie it to a TAB on the MainForm. Then I want to access the TextBoxes and CheckBoxes from
the MainForm. The idea is to create different User Controls that at runtime are attached to the TABS
on the MainForm depending on how the XML is setup. This is a program that
will be used in testing various hardware configurations. Thus depending on
configuration, the TABs on the MainForm may be different. I already have
the code that will attach different User Controls to the TABs on the
MainForm and it works great. At runtime, I can lock User Control1 to TAB1,
UserControl3 to Tab2 and etc. I can change the XML and next time it will
lock User Control5 to TAB1, User Control7 to TAB2, and ect.

Next part is to gain access to the TextBoxes and CheckBoxes on the User Controls from the MainForm.
If I can not access them like you do controls on a Form, then how do you access controls on a User
Control from the MainForm??

I am trying to minimize the number of TABs on the MainForm. My fall back
is to create all the TABs that I will need and move all the controls from
the User Controls onto the MainForm TABs and go from there. Then delete
all the User Controls and forget using User Controls.

If I can not access controls on a User Control like you can on a Form,
(VB.NET does compile correctly) then why does VB.NET let you do it in
the first place.
 
john,

I am reading this post late in the game however, I have just did some reading on user controls and am in the process of creating several user controls. Anyway, I think your issue is once you add your controls to the user control you have to add public property to expose your textbox.text property and add another public properties to expose your label.text and label.enabled (or .visiable) properties on your constituent controls. Constituent controls are the controls you added to your user controls. Hope this helps even though it's late.
 
How about the clipboard?

I have much to learn about vb.net, but I have successfully transferred text from a textbox on one form to a textbox on another form via the system clipboard. Worth a try?
 
I'd say no... reason being that in vb.net we are doing OOP now and so all forms are classes. So, you should start to think in classes and use the best OOP method to achieve your objective.

If you are trying to pass data from one form to the next then create a public property to expose a private member variables in your 2nd form class.

Then you can declare in form1 your form2 class as follows...

public myForm2 as New Form2()

myForm2.MyDataToForm2 = "Data In"

myForm2.show()

Write code inside myForm2 to display this text in a textbox control.

sample declaration...

private mMyDataToForm2 as string

Sample property in side myForm2 class...

Public Property MyDataToForm2() AsString
Get
MyDataToForm2= mMyDataToForm2 'Declare private variable inside class Form2.
End Get
Set(ByVal Value As String)
Try
mMyDataToForm2= Value
txtForm2Textbox.Text = mMyDataToForm2 'If you have a textbox on Form2 called txtForm2Textbox.

Catch ex As Exception
'Raise Error...
EndTry
End Set
End Property

You could also pass date in the contructor by overloading the New() method with input arguments. VB.net creates an empty contructor by default and you can add code to it. You can also create an overloading New(Byval inData as string) to pass in some data. I have not done this but you should be able too. The New() contructor is the first method that is called when you instantiate the class as in a form...

Public myForm2 as New Form2()

or

Public myForm2 as Form2()
myForm2 = New Form2()

Note: You should be able to pass data out of the form class using the same MyDataToForm2 property. Set the data to it's new value before hiding the form and then in Form1 read the data using...

Private localVariable as String
localVariable = myForm2.MyDataToForm2
myForm2.Close

I hope this helps and It may help me talk about this coding stuff as I prepare for both the C# and VB.net cert test.

Chow! :)
 
Last edited:
Back
Top