简体   繁体   中英

vb.net Pass Textbox Between forms.

I have 2 forms. On Form1 I want to pass the textbox value to form 2 on load. This is what I thought would work. Form1 will be loaded and running and data will be populated in form1. I am exposing the property of the text box in form1. Then I am trying to use that exposed property in form2.

Public Class form1

Public ReadOnly Property GetTextBox() As String
    Get
        Return txtbox1.Value
    End Get
End Property

On form2

 Dim X As form1
 Me.TextBox1.Text = X.GetTextBox

VB will allow you to refer to instances of forms by their class name , so you'd be able to use:

Me.TextBox1.Text = Form1.GetTextBox

However you should not rely on this and should instead pass an explicit instance of Form1 to Form2 , eg in a constructor:

' Form2
Public Sub New(ByVal f As Form1)
    Me.New()

    ' save 'f' for future reference
End Sub

There are a handful of ways to skin this cat.

The simplest would be to create a second (or replace the existing) constructor for Form2 that accepts a string as an parameter. Then when Form1 creates Form2 you can pass the argument that way.

Public Class Form2
    Sub New(ByVal txt As String)
        InitializeComponent()
        Me.TextBox1.Text = txt
End Sub
End Class

Then in Form1 you'd have something like Dim f2 As Form2 = New Form2(myTextBox.Text)

The other ways are honestly basically the same as this, except you could pass the Textbox itself as an argument to the constructor, or even Form1 and assign Dim X As Form1 = theForm in the constructor. Generally speaking, if you don't need anything more than just Textbox.Text then you should only accept a string in the constructor. No reason to expose an entire control or form if you don't need all of it!

Your current code is pretty close, but as Plutonix commented your Form2 's X property is just another instance of Form1 , not the actual instance that's being displayed by the application.

When you do this:

Dim X As form1

You're create a new reference to a form1 . (And presumably instantiating it somewhere? Or perhaps relying on a feature in VB whereby a "default" form instance is used . Which... don't do that. Just trust me, don't.) This instance is entirely unrelated to the instance that already exists. What you're looking for is a reference to the instance that already exists.

If form2 has a dependency on form1 and requires a reference to an instance of form1 then require that reference on the constructor for form2 :

Private Property Form1Instance As form1

Sub New(ByVal form1Instance As form1)
    Me.Form1Instance = form1Instance
End Sub

When you create your instance of form2 , provide it with a reference to the form1 instance:

Dim form2Instance As New form2(Me)
form2Instance.Show()

Then within form2 you can reference that existing instance of form1 :

Dim someVariable As String = Me.Form1Instance.GetTextBox()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM