text from form1 to form 2
-
this may sound really basic and i'm sure it is. but throughout all of the books i've read (being self taught) i've never come across how to get the string in a text box on one form another text box/label/whatever on another form. for example: say form1 has a textbox for inputting a users name. how can i then display that name on form2 in a label control? thanks in advance cheers
-
this may sound really basic and i'm sure it is. but throughout all of the books i've read (being self taught) i've never come across how to get the string in a text box on one form another text box/label/whatever on another form. for example: say form1 has a textbox for inputting a users name. how can i then display that name on form2 in a label control? thanks in advance cheers
xtremean wrote: i've never come across how to get the string in a text box on one form another text box/label/whatever on another form. That's because your thinking about the code on one form directly manipulating the controls on another form. The reason you don't see that in the books is because it's bad practice. What you should be thinking about is having public variables somewhere that both forms can access. For example, you have two forms, one has a string that needs to be passed to the second. The first form just has a label, a textbox and a button on it:
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim newForm As New Form2
newForm.strDataThisFormNeeds = TextBox1.Text
newForm.Show()
End Sub
End ClassForm1 is simple, just type something in the textbox and click a button to send the string to Form2. The code in the button click just creates a new instance of Form2 and set the public variable that Form2 exposes to the string in Form1's textbox. Form2 just has a label and a textbox on it and exposes a public string like this:
Public Class Form2
Inherits System.Windows.Forms.Form
Public strDataThisFormNeeds As String
Private Sub Form2_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
TextBox1.Text = strDataThisFormNeeds
End Sub
End ClassThis is just a VERY simple example and does NOT show all the possible methods of passing data between forms! RageInTheMachine9532