简体   繁体   English

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

[英]Referencing control on one form from another form

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. 从这个表单到按钮的click事件,我想显示另一个表单frmCustomers,并让该表单引用txtCustomerNo中的值。

frmCustomers.ShowDialog()

It has to be something simple that I'm just not grasping. 它必须是简单的,我只是没有抓住。

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

My.Forms.frmGenerate.txtCustomerNo.Text

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

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;
    }
}

Make a private field. 建立一个私人领域。 Right click 'Refactor', select 'Encapsulate Field'. 右键单击“Refactor”,选择“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. 仅共享通过公共属性/构造函数所必需的字段。

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. 这里的技巧是确保类代码在被调用的类中使用“ByRef”参数,否则这将不起作用。 In memory you are referencing the same control without creating new properties for it, and that lessens code creep. 在内存中,您引用相同的控件而不为其创建新属性,这减少了代码蠕变。

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"

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()结束

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(). Dim logged As String logged = Login.txtUser.Text Login.Close()。 'Close hidden login.vb' End Sub '关闭隐藏的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. 因此,用户按下登录按钮,它会在txtUser文本框中隐藏具有我想要的值的登录表单。 Then it opens the Admin page. 然后它会打开Admin页面。 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." Admin页面的load事件从txtUser文本框中的隐藏登录页面获取我的值,并将其放入参数“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." 现在我想在任何地方使用登录人员的姓名,比如在文本框中我只使用“已登录”。

txtchangesby.Text = logged txtchangesby.Text =已记录

Very simple and works. 很简单,很有效。

While you can create a property on your second form that will return the data needed, you can just as easily change the Modifier property of that control. 虽然您可以在第二个表单上创建一个将返回所需数据的属性,但您可以轻松更改该控件的Modifier属性。 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. 单击该控件,然后找到Modifier属性。 Set that to Public, and you'll be able to access it from outside the form, such as: 将其设置为Public,您将能够从表单外部访问它,例如:

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

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

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