Anas Ahmed
Anas Ahmed

Reputation: 123

Passing Data from one form to another in vb.net

I have two forms namely "MainForm" and "SubForm" Now "Mainform" has a button and a textbox. Upon clicking the button, "SubForm" should be opened while MainForm will also remain opened. Now SubForm has a text box and an OK button. User will input the value in the textbox and on clicking OK, SubForm should be closed and MainForm textbox text should be changed to the text which has been entered in SubForm textbox. Thanks in advance

Upvotes: 0

Views: 493

Answers (1)

Maciej Los
Maciej Los

Reputation: 8591

Change the constructor for SubForm and add a property this way:

Private sValue As String = String.Empty

Public New(myText As String)
    SomeText = myText
    TextBox1.Text = SomeText
End Sub

Public Property SomeText As String
    Get
        Return sValue
    End Get

    Set(value As String)
        sValue = value
    End Set
End Property

'on OK button click event
 SomeValue = TextBox1.Text

Then in MainForm (on button click):

Using sf As SubForm = new SubForm(Me.InitialText.Text)
    Dim dlg As DialogResult = sf.ShowDialog()
    If dlg = DialogResult.OK Then
        Me.InitialText.Text = sf.SomeText
    End If
End Using

Other settings:

SubForm:

  • AcceptButton property - refers to OK button
  • OKButton.DialogResult = OK
  • TextBox1 - the name of textbox for user input

Main form

  • InitialText - the name of textbox

Upvotes: 2

Related Questions