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

[566 byte] By [Rudemusik] at [2007-12-24]
# 1

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.

WilliamBartholomew at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2
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 variable

Public 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

spotty at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3
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.
PrasantSwain at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4

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

Rudemusik at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5

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 EventArgs

Private Name1 As String

Private TextBox2 As String

Public Property FirstName() As String

Get

Return Name1

End Get

Set(ByVal Value As String)

Name1 = Value

End Set

End Property

Public Property LastName() As String

Get

Return TextBox2

End Get

Set(ByVal Value As String)

TextBox2 = Value

End Set

End Property

End Class

And 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.Click

Dim myvalue As New Person

Dim myvalue2 As New Person

myvalue.FirstName = TextBox1.Text

myvalue2.LastName = TextBox2.Text

'MsgBox(Employee.FirstName)

TextBox3.Text = (myvalue.FirstName)

End Sub

I'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

Rudemusik at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 6

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 EventArgs

Private Name1 As String

Private TextBox2 As String

Public Property FirstName() As String

Get

Return Name1

End Get

Set(ByVal Value As String)

Name1 = Value

End Set

End Property

Public Property LastName() As String

Get

Return TextBox2

End Get

Set(ByVal Value As String)

TextBox2 = Value

End Set

End Property

End Class

And 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.Click

Dim myvalue As New Person

Dim myvalue2 As New Person

myvalue.FirstName = TextBox1.Text

myvalue2.LastName = TextBox2.Text

'MsgBox(Employee.FirstName)

TextBox3.Text = (myvalue.FirstName)

End Sub

I'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

Rudemusik at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 7

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 StateModel

Private Shared _TextValue1 As String = "Foo"

Public Shared Event TextValue1Changed(ByVal sender As Object, ByVal e As TextValueArgs)

Public Property TextValue1()

Get

Return _TextValue1

End Get

Set(ByVal value)

_TextValue1 = value

RaiseEvent TextValue1Changed(Nothing, New TextValueArgs(value))

End Set

End Property

End Class

State Model Event Helper Class

Public Class TextValueArgs

Inherits EventArgs

Private _TextValue As String

Friend Sub New(ByVal TextValueIn As String)

Me._TextValue = TextValueIn

End Sub

Public ReadOnly Property TextValue1() As String

Get

Return Me._TextValue

End Get

End Property

End Class

The View-Controller:

Form 1 Code:

Public Class Form1

Private WithEvents MYModel As New StateModel

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

MYModel.TextValue1 = Me.TextBox1.Text

End Sub

End Class

The Observer Forms:

Public Class Form2

Private WithEvents MyModel As New StateModel

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub MyModel_TextValue1Changed(ByVal sender As Object, ByVal e As TextValueArgs) Handles MyModel.TextValue1Changed

Me.TextBox1.Text = e.TextValue1

End Sub

End Class

Write 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

IbrahimY at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...