vb.net 从另一个表单引用一个表单上的控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4471514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:12:13  来源:igfitidea点击:

Referencing control on one form from another form

vb.netwinforms

提问by Tom

As you can guess, I'm kind of new to .NET and just want to reference a control on one form from another.

您可以猜到,我对 .NET 有点陌生,只想从另一个表单引用一个表单上的控件。

Usually I would just do Form.Control.Property but that doesn't work and every example I've found through Google doesn't work for me.

通常我只会做 Form.Control.Property 但这不起作用,我通过谷歌找到的每个例子都不适合我。

It just seems overly complicated with public classes, etc.

公共课程等似乎过于复杂。

Is there a more simpler way to do this? I'm ready to throw in the towel and just use a global variable at this point.

有没有更简单的方法来做到这一点?我准备认输,此时只使用一个全局变量。

I have the form containing the control I want to reference, frmGenerate which has a textbox called txtCustomerNo.

我有一个包含我想要引用的控件的表单,frmGenerate,它有一个名为 txtCustomerNo 的文本框。

From this form through a button's click event I want to show another form, frmCustomers, and have that form reference the value in txtCustomerNo.

从这个表单到一个按钮的点击事件,我想显示另一个表单 frmCustomers,并让该表单引用 txtCustomerNo 中的值。

frmCustomers.ShowDialog()

It has to be something simple that I'm just not grasping.

它必须是一些简单的东西,我只是没有掌握。

回答by Martin

In the form with the control you want to reference:

在带有要引用的控件的表单中:

Public Property CustomerNo() As TextBox
    Get
        Return txtCustomerNo
    End Get
    Set(ByVal Value As TextBox)
        txtCustomerNo = Value
    End Set
End Property

In the form you wish to reference the control:

在您希望引用控件的表单中:

Dim CustomerNo as string = _sourceForm.CustomerNo.Text

It's a bad design to do this, but it's the simplest method - and should set you on your way.

这样做是一个糟糕的设计,但它是最简单的方法 - 应该让你走上正轨。

If you are only interesting in the value entered in the text box then the following might be better:

如果您只对文本框中输入的值感兴趣,那么以下可能会更好:

Public ReadOnly Property CustomerNo() As String
    Get
        Return txtCustomerNo.Text
    End Get
End Property

The above will require the second form to have a reference to the actual instance of the first form. Add the below to the second form:

以上将要求第二个表单引用第一个表单的实际实例。将以下内容添加到第二个表单中:

Private _sourceForm as frmGenerate

Public Property SourceForm() As frmGenerate 
    Get
        Return _sourceForm
    End Get
    Set(ByVal Value As frmGenerate)
        _sourceForm = Value
    End Set
End Property

Now you can do the following where you handle the creation and startup of your second form:

现在,您可以在处理第二个表单的创建和启动时执行以下操作:

Dim customersForm as new frmCustomers
customersForm.SourceForm = Me
customersForm.Show()    

EDIT:I have constructed a sample project for you here:

编辑:我在这里为您构建了一个示例项目:

http://www.yourfilelink.com/get.php?fid=595015

http://www.yourfilelink.com/get.php?fid=595015

回答by Oded

You need to ensure that the properties you add are public, or they will not be accessible by other classes.

您需要确保您添加的属性是public,否则其他类将无法访问它们。

回答by Mike

A better way to approach this is to pass a reference of a control into your class, change any property of it there with your logic, then "send" it back to the calling form.

解决此问题的更好方法是将控件的引用传递到您的类中,使用您的逻辑更改它的任何属性,然后将其“发送”回调用表单。

The trick here is to make sure the class code is using the "ByRef" argument in the called class or else this will not work. In memory you are referencing the same control without creating new properties for it, and that lessens code creep.

这里的技巧是确保类代码在被调用的类中使用“ByRef”参数,否则这将不起作用。在内存中,您引用相同的控件而不为其创建新属性,这减少了代码蠕变。

For instance here is how you can disable a button from your class code.

例如,这里是如何从类代码中禁用按钮的方法。

In the form, call your class and pass it the button (or any other control):

在表单中,调用您的类并将按钮(或任何其他控件)传递给它:

' new the class your calling
Dim CallClass As New ProgramCall
' pass the control as a by reference argument
CallClass .SetUpTheCall("HOURLY", btnSendToBank)  

Then, in your class:

然后,在你的课堂上:

Public Sub SetUpTheCall(ByVal ParmToPass As String, ByRef btnSendToBank As Button)

' your class logic goes here...

' disable the send button on the calling form
btnSendToBank.Enabled = False

' change the button text on the calling form
btnSendToBank.text = "Disabled"

回答by Jonathan Wood

You should be able to make whatever you need to reference outside the form public. In many cases that's all that's needed.

您应该能够在表单之外公开您需要引用的任何内容。在许多情况下,这就是所需要的。

Sometimes, it's useful to create a separate public property or method. And have the method take care of the details. For example, if you just want to able able to access the text, you could create a property something like this (in C#).

有时,创建单独的公共属性或方法很有用。并让方法处理细节。例如,如果您只想能够访问文本,您可以创建一个类似这样的属性(在 C# 中)。

public string CustomerNo
{
    get
    {
        return txtCustomerNo.Text;
    }
    set
    {
        txtCustomerNo.Text = value;
    }
}

回答by Aseem Gautam

Make a private field. Right click 'Refactor', select 'Encapsulate Field'. This will automatically create a public property for the field.

做一个私人领域。右键单击“重构”,选择“封装字段”。这将自动为该字段创建一个公共属性。

Another approach is to overload the public constructor.

另一种方法是重载公共构造函数。

public CustomersForm(string property1, string property2...)
{
     //Get the properties and do what is necessary.
}

//Parent Form

CustomersForm frmCustomers = new CustomersForm(property1, property2..);

Also sending the complete control to another form is not a good strategy. Share only the fields that are necessary via public properties/constructors.

将完整的控件发送到另一个表单也不是一个好策略。仅通过公共属性/构造函数共享必要的字段。

回答by Aronas

My.Forms.frmGenerate.txtCustomerNo.Text

回答by Brian M.

While you can create a property on your second form that will return the data needed, you can just as easily change the Modifierproperty of that control. By default, the modifiers of controls are private, and therefore not accessible to anything other than the current form. Click on the control, and find the Modifier property. Set that to Public, and you'll be able to access it from outside the form, such as:

虽然您可以在第二个表单上创建一个属性来返回所需的数据,但您也可以轻松更改该控件的Modifier属性。默认情况下,控件的修饰符是私有的,因此除了当前表单之外的任何东西都无法访问。单击控件,然后找到 Modifier 属性。将其设置为 Public,您就可以从表单外部访问它,例如:

Dim f As New SecondForm
f.FirstNameTextBox.Text = "Bob"

回答by Mark

I save the name of the person logging in to a textbox on the next form after they login like this. My login button has the last lines: Me.Hide() ( Not Me.Close() ) Admin.Show() End

在他们像这样登录后,我将登录到下一个表单上的文本框的人的姓名保存起来。我的登录按钮有最后几行: Me.Hide() ( Not Me.Close() ) Admin.Show() End

When the user enters a correct username and password, it hides the login form and opens the next page, Admin.vb

当用户输入正确的用户名和密码时,它会隐藏登录表单并打开下一个页面 Admin.vb

In the Admin_Load of the Admin.vb page as the last lines I put:

在 Admin.vb 页面的 Admin_Load 中作为我放置的最后几行:

Dim logged As String logged = Login.txtUser.Text Login.Close(). 'Close hidden login.vb' End Sub

Dim 记录为字符串记录 = Login.txtUser.Text Login.Close()。'关闭隐藏的 login.vb' End Sub

So, the user presses the login button and it hides the login form with the value I want in the txtUser textbox. Then it opens the Admin page. The load event of the Admin page, gets my value from the hidden login page in the txtUser textbox and puts it in the parameter "logged." Once the value is grabbed, it closes the login page and leaves the sub. It all happens in the blink of an eye! Now anywhere I want to use the name of the person that logged in, like in a textbox I just use "logged."

因此,用户按下登录按钮并在 txtUser 文本框中使用我想要的值隐藏登录表单。然后它打开管理页面。管理页面的加载事件,从 txtUser 文本框中隐藏的登录页面获取我的值,并将其放入参数“已记录”中。获取值后,它会关闭登录页面并离开子页面。这一切都发生在眨眼之间!现在我想在任何地方使用登录者的名字,就像在文本框中我只使用“登录”。

txtchangesby.Text = logged

txtchangesby.Text = 已记录

Very simple and works.

非常简单且有效。