Question TextBox Value to another Form

msvhub

Member
Joined
Dec 14, 2009
Messages
5
Programming Experience
1-3
Hi,

I have a question regarding using a TextBox in second form within the same project.

Currently I am using two forms. From the main form you can click a button which opens the second form. There, I want to type in an integer value into a textbox and use it for the code in my first form.

Here's my code:

Public Class Form1

Private Sub configure_button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles configure_button.Click
Dim F As New Form2
F.Show()
End Sub

Private Sub Test_button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Test_button.Click
Dim test
test = Form2.TextBox1.Text
MsgBox(test)
End Sub

End Class

This doesn't work. Theres no value at all in the MsgBox.

Instead of the first example I could also use this one, which gives me a zero in the MsgBox:

Public Class Form1

Private Sub Test_button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Test_button.Click
MsgBox(Form2.testvar)
End Sub

End Class



Public Class Form2

Public testvar As New Integer

Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
testvar = TextBox1.Text
End Sub

End Class

Where is my mistake? What can I do betterß

Please help.
 
Last edited:
On form2 add this:
VB.NET:
Private _TextboxReturn As TextBox
Public Property TextboxReturn() As TextBox
    Get
      Return _TextboxReturn
    End Get
    Set(ByVal value As TextBox)
      _TextboxReturn = value
    End Set
End Property
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    _TextboxReturn.Text = TextBox1.Text
End Sub
When you create form2:
VB.NET:
Dim frm2 As New form2
frm2.TextboxReturn = textbox1 'the textbox you want to send the data to
frm2.Show()
 
This is not really what I want to do. Sorry if I was not percise enough with my previous question.

I'd like to import the value of each of the textboxes in Form2 in variables which then can be used in Form1.

Thank you for yur statement, newguy, but your code seems only to return the value to copy it into a textbox in Form1. I'd rather assign the value of the textbox in Form2 to a variable, if that is possible.
 
Last edited:
Maybe a custom object with fields(16 of them) then pass that to the form and fill them and return? I am thinking about a nested class object, are you familiar?
 
What I tried is using an extern Module to declare the field variable for the textboxes.

Like this:
Public Module Module1
Public gettext(16) As Integer
End Module

Then get the Input from the TextBox in Form2 with:

Module1.gettext(0) = TextBox1.Text
Module1.gettext(1) = TextBox2.Text
'...

And Then use the "gettext()" field variable in Form1 for whatever I want.


Sorry, I am relatively new to everyday programming. Still I find this method awkward... Is there another, yet smarter solution than mine?
 
Back
Top