简体   繁体   English

如何使用VB.Net中的继承将文本框的值从一种形式获取到另一种形式

[英]How to get value of textbox from one form to another form using inheritance in VB.Net

Let say, I want to get value of textbox from one winform to another with the help of inheritance in vb.net比方说,我想借助 vb.net 中的继承将文本框的值从一个 winform 获取到另一个

If Form1.TextBox1.Text = "1" Then
    'This is Form2'
    TextBox1.Text = "1"
Else
    TextBox1.Text = "2"
End If

The code below is just for demo.下面的代码仅用于演示。 Advised not to do in this way.建议不要这样做。 This is really really bad coding这真的是非常糟糕的编码

  1. Create two forms;创建两种形式; from1 and form2. from1 和 form2。
  2. Add two textboxes and one button.添加两个文本框和一个按钮。
  3. copy the code below.复制下面的代码。
  4. you type in textbox2 and click the button.您输入 textbox2 并单击按钮。
  5. it will display in form1, if you enter it from form2 and vice versa.它将显示在 form1 中,如果您从 form2 输入它,反之亦然。

     // Form1.vb Public Partial Class Form1 Inherits Form Private f2 As Form2 Public Sub New() InitializeComponent() f2 = New Form2(Me) f2.Show() End Sub Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) f2.setText(textBox2.Text) End Sub Public Sub setText(ByVal text As String) textBox1.Text = text End Sub End Class // Form2.vb Public Partial Class Form2 Inherits Form Private f1 As Form1 Public Sub New(ByVal form As Form1) InitializeComponent() f1 = form End Sub Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) f1.setText(textBox2.Text) End Sub Public Sub setText(ByVal text As String) textBox1.Text = text End Sub End Class

what this code do is create form2 with reference to form1 and call setText which will change text in textbox1 in either form when button click.这段代码所做的是参考 form1 创建 form2 并调用 setText ,当按钮单击时,它将以任一形式更改 textbox1 中的文本。 I repeat never ever do this.我重复从来没有这样做过。 there you go vb你去vb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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