Passing values between forms
Hello!
I have been trying to figure out the best way to do this, read so much about tonite, head is spining, must sleep. But before I go, hoping someone can help me out. I have a text box that holds a value in FormA. I would like to pass that value from that textbox to a diffrent textbox on FormB, c and other forms; in the same app. I saw example where you can make the form public, but can't I create the texbox as a public varaible? Or would I try a diffrent way. Just not sure how to write this out in vb.net.
Thanks for the help!
Rudy
There are two common solutions to this issue:
1. Expose the TextBox's value via a public property (the value, not the TextBox):
Public Property CustomerName As String
Get
Return txtCustomerName.Text
End Get
Set ( ByVal value As String )
txtCustomerName.Text = value
End Set
End Property
2. Use events which the other forms can subscribe to:
'Declare an event for each field that could change so other forms can choose which ones to subscribe to
Public Event CustomerNameChanged As EventHandler(Of FieldChangedEventArgs)
'Declare a class to hold the new value of the field so the other forms can retrieve it
Public Class FieldChangedEventArgs
Inherits EventArgs
Private m_NewValue As String
Public Sub New( ByVal newValue As String )
m_NewValue = newValue
End Sub
Public ReadOnly Property NewValue As String
Get
Return m_NewValue
End Get
End Property
End Class
'When the field changes raise an event
Public Sub CustomerName_TextChanged( ByVal sender As Object, ByVal e As EventArgs ) Handles txtCustomerName.TextChanged
RaiseEvent CustomerNameChanged( New FieldChangedEventArgs( txtCustomerName.Text ) )
End Sub
Method 1 is easier, method 2 is probably more flexible.
If you have the variables declared in a module or form level then if they and the module are public scoped they will be visible from anywhere in your application as long as you fully qualify the variablePublic Module Module1
Public Foo as integer =1
End Module
Then in my callinhg code I should be able to refer to
Module1.Foo to reference the variable.
Forms are a little different as they are instanced, whereas modules are not. So
Public Class Form1
Public Foo as integer = 1
'... All the other form code in here.
End Class
and in the calling code
Dim x as new Form1
msgbox(x.Foo)
as x is the instance of the form and your referencing the public variable foo in this instance.
Variables are ok to do this but you can use properties to better control the access of these variables - some may be read only from outside of the form, whereas others you want to allow to be changed from outside.
To do this you will change the variable in the class to private scope and use a public property for accessing it.
Public Class Form1
Private ifoo As Integer
Public Property foo() As Integer
Get
Return ifoo
End Get
Set(ByVal value As Integer)
ifoo = value
End Set
End Property
End Class
You can then use things like to limit the access to readonly from outside of the class but from within the class you can modify the ifoo variable.
Public ReadOnly Property foo() As Integer
Get
Return ifoo
End Get
End Property
why not you declare properties in the form and using the property set the value of the text box. and you want to declare the text box as public, i dont think there is any problem.
Thank you all!!
With a good night sleep behind me and a cup of joe, it seems much clearer now.
Thanks for all the great advice!
Rudy
Hi William!
I tried your way, but I'm a little lost. I understand the first part. I wrote a sample app to help me figure this out. So I have my class.
Public
Class Person'Inherits EventArgsPrivate Name1 As StringPrivate TextBox2 As StringPublic Property FirstName() As StringGetReturn Name1End GetSet(ByVal Value As String)Name1 = Value
End SetEnd PropertyPublic Property LastName() As StringGetReturn TextBox2End GetSet(ByVal Value As String)TextBox2 = Value
End SetEnd PropertyEnd
ClassAnd on form1 I can pull a value from textbox1 , and put in textbox 3 through the class, like so.
Private
Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickDim myvalue As New PersonDim myvalue2 As New Person myvalue.FirstName = TextBox1.Text
myvalue2.LastName = TextBox2.Text
'MsgBox(Employee.FirstName)TextBox3.Text = (myvalue.FirstName)
End SubI'm a little confused about the second part. Inherits EventArgs
Private m_NewValue As String
Public Sub New( ByVal newValue As String )
m_NewValue = newValue
End Sub
Public ReadOnly Property NewValue As String
Get
Return m_NewValue
End Get
End Property
End Class
This goes into the class, right? So far so good. But where should this go?
Public Sub CustomerName_TextChanged( ByVal sender As Object, ByVal e As EventArgs ) Handles txtCustomerName.TextChanged
RaiseEvent CustomerNameChanged( New FieldChangedEventArgs( txtCustomerName.Text ) )
End Sub
Should this go in form2? I don't understand how I can call this into another form, and fill a textbox with the value from the textbox on the first form?
I'm basicly using the userID as a session, so I will pass that value to each textbox on each form. So the value will never change once it's created when the program starts.
Thanks again for all your help on this, I appreciate your patients on this.
Rudy
Hi William!
I tried your way, but I'm a little lost. I understand the first part. I wrote a sample app to help me figure this out. So I have my class.
Public
Class Person'Inherits EventArgsPrivate Name1 As StringPrivate TextBox2 As StringPublic Property FirstName() As StringGetReturn Name1End GetSet(ByVal Value As String)Name1 = Value
End SetEnd PropertyPublic Property LastName() As StringGetReturn TextBox2End GetSet(ByVal Value As String)TextBox2 = Value
End SetEnd PropertyEnd
ClassAnd on form1 I can pull a value from textbox1 , and put in textbox 3 through the class, like so.
Private
Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickDim myvalue As New PersonDim myvalue2 As New Person myvalue.FirstName = TextBox1.Text
myvalue2.LastName = TextBox2.Text
'MsgBox(Employee.FirstName)TextBox3.Text = (myvalue.FirstName)
End SubI'm a little confused about the second part. Inherits EventArgs
Private m_NewValue As String
Public Sub New( ByVal newValue As String )
m_NewValue = newValue
End Sub
Public ReadOnly Property NewValue As String
Get
Return m_NewValue
End Get
End Property
End Class
This goes into the class, right? So far so good. But where should this go?
Public Sub CustomerName_TextChanged( ByVal sender As Object, ByVal e As EventArgs ) Handles txtCustomerName.TextChanged
RaiseEvent CustomerNameChanged( New FieldChangedEventArgs( txtCustomerName.Text ) )
End Sub
Should this go in form2? I don't understand how I can call this into another form, and fill a textbox with the value from the textbox on the first form?
I'm basicly using the userID as a session, so I will pass that value to each textbox on each form. So the value will never change once it's created when the program starts.
Thanks again for all your help on this, I appreciate your patients on this.
Rudy
Hello All;
While some of the solutions offered above work in very simple cases, they will become problematic when used in a serious environment. I would like to offer a different solution based upon a variation of the MVC (ModelViewController) and Observer patterns.
Form1 in your scenerio is the View that provides the input data. Other forms; Form2, Form3, are only interested in changes to the state of a given String object. Form one should not care if the other forms even exist. And actually, the other forms should be able to reflect the value of the string object whether Form one is present or not.
The string object should exist in a State Model
The changes to the String Object should happen through a View-Controller
And the state of the String object should be reflected by one or more observers (Form2, Form3, etc.)
The State Model:
Public
Class StateModelPrivate Shared _TextValue1 As String = "Foo"Public Shared Event TextValue1Changed(ByVal sender As Object, ByVal e As TextValueArgs)Public Property TextValue1()GetReturn _TextValue1End GetSet(ByVal value)_TextValue1 = value
RaiseEvent TextValue1Changed(Nothing, New TextValueArgs(value))End SetEnd PropertyEnd
ClassState Model Event Helper Class
Public
Class TextValueArgsInherits EventArgsPrivate _TextValue As StringFriend Sub New(ByVal TextValueIn As String)Me._TextValue = TextValueInEnd SubPublic ReadOnly Property TextValue1() As StringGetReturn Me._TextValueEnd GetEnd PropertyEnd
ClassThe View-Controller:
Form 1 Code:
Public
Class Form1Private WithEvents MYModel As New StateModelPrivate Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChangedMYModel.TextValue1 =
Me.TextBox1.TextEnd SubEnd
ClassThe Observer Forms:
Public
Class Form2Private WithEvents MyModel As New StateModelPrivate Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadEnd SubPrivate Sub MyModel_TextValue1Changed(ByVal sender As Object, ByVal e As TextValueArgs) Handles MyModel.TextValue1ChangedMe.TextBox1.Text = e.TextValue1End SubEnd
ClassWrite your project so that one instance of Form1 is created and shown and 1 or more instances of Form2 are created and shown. As you type into the textbox on form one, the value will be shown in the other forms. This allows you to change anything you want in any of the forms. Only the StateModel remains the same. Any form that want sto observer the state of the string object can do so at any time. Regardless of form one being instanced or not.
Ibrahim Malluf