开发者

How to get text from form1

I have 2 forms. In form1 i have a textbox called TextBox1 In Form2 i have another textbox called TextBox2.

I want the text from textbox1 to textbox2, i tried this.

TextBox2.Text = Form1.TextBox1.Text
开发者_开发百科

Do i need to make changes to the first textbox?


As Joel mentioned you will need to make the TextBox control public, be default they are Friend which allows other classes/controls/forms in the same Assembly (VB.NET Project) to access the control but not classes/controls/forms from outside the Assembly.

The change can be made in two ways, in the GUI Designer or the generated code.

To make the change in the designer:

  • Select the TextBox
  • Go to the Properties window (or right-click and select "Properties")
  • Change the "Modifiers" property to "Public"

To change the generated code you will need to:

  • enable "Show All Files" in the Solution Explorer
  • Right-click the Form's .Designer.vb file and select "View Code"
  • Locate the TextBox's declaration, which will be like:

    Friend WithEvents TextBox1 As Windows.Forms.TextBox

Change "Friend" to "Public"

Public WithEvents TextBox1 As Windows.Forms.TextBox

On a side-note This works fine, but is generally not a good practice to have controls "reaching into" other controls to get/set values. I understand you're new to .net so I'll spare the details, but once you feel you've tackled the initial learning curve you'll want to read into the ideas behind the MVC / MVP / MVVM / MV* patterns--they focus on seperation of logic and UI


Controls in windows forms are not public by default. You either need to change the declaration of the TextBox on form1 to explicitly make it public or add a public property to form1 that gets and sets the textbox text (preferred) .


You will need to reference the instance of the form. Form1 is the class.


As I already answered you in this post you have all your code.

You should not access the Form2.TextBox directly, but to pass the textbox text as parameter in constructor OR via a public field (or property) of the second form.

This works, surely, if you can(and have necessary minimum knowledge of VB.NET in order to) modify the Form2 code.

If not, you should declare a unique (Shared in VB.NET) instance of Form2, that is make it Public Shared myUniqueForm2 as Form2 then access the TextBox2 via myUniqueForm2.TextBox2

If the post are still unclear for you, you should stop asking people and read some short basic articles about Object Oriented Programming (OOP) and VB.NET.

By example this one or this one. A lot of tutorials about VB.NET you can find also here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜