简体   繁体   English

如何从vb.net的另一个表单中检查表单1的复选框?

[英]How to check a checkbox of form 1 from another form in vb.net?

I have two forms, form 1 and form 2 (windows application). 我有两种形式,即形式1和形式2(Windows应用程序)。 How can i access and check a checkbox in form 1 from form 2. Initially i tried calling the form name and then the control like form1.chkCanada.checked = true , it did not work. 我如何从表单2中访问和检查表单1中的复选框。最初,我尝试调用表单名称,然后调用诸如form1.chkCanada.checked = true类的控件,但它不起作用。 And then i added a property in form 1 然后我在表格1中添加了一个属性

Public Property abc As Boolean
    Get
        Return chkCanadianStmtInd.Checked
    End Get
    Set(value As Boolean)
        chkCanadianStmtInd.Checked = value
    End Set
End Property

and then in form 2 然后以表格2

Dim frm As New frm1
frm.abc = True  'Checked

And it still doesnt work. 而且它仍然无法正常工作。 Am i missing anything here? 我在这里想念什么吗?

Consolidating comments here: 在此处合并评论:

In order to access controls on a form that shows another form to the user you have two options, if no interaction is needed with the first form while the second form is active you can use showdialog and do all of your logic after the second form has closed, if you ned to maintain the ability to interact with the first form while the second is still open then you need to use custom events. 为了访问向用户显示另一种表单的表单上的控件,您有两个选择,如果在第二种表单处于活动状态时不需要与第一种表单进行交互,则可以使用showdialog并在第二种表单具有第二种表单之后执行所有逻辑如果您希望在第二个窗体仍处于打开状态时保持与第一个窗体进行交互的能力,则需要使用自定义事件。

Showdialog: 显示对话:

The simpler of the two options is to switch your form.show() function calls to form.showdialog() . 这两个选项中最简单的一个是将form.show()函数调用切换为form.showdialog() This effectively tells the first form that it should stop processing at the form.showdialog() line and wait for the child form to close before proceeding. 这有效地告诉第一个表单它应该在form.showdialog()行中停止处理,并等待子表单关闭后再继续。 Once the second form is closed the first form will pick up where it left off and that would be where any processing that relies on the values of the second form would take place. 一旦关闭第二个窗体,第一个窗体将在它停下来的地方开始,这将是依赖于第二个窗体的值进行任何处理的地方。

Custom Events: 自定义事件:

If you want to allow the user to interact with both the first and second forms at the same time then you will need to use custom events. 如果要允许用户同时与第一种和第二种形式进行交互,则需要使用自定义事件。 In order to do this you will need three things. 为了做到这一点,您将需要三件事。 The custom event, a raiseevent call and an event handler. 自定义事件,raiseevent调用和事件处理程序。

So in your Form2 class you will need to declare the custom event. 因此,在Form2类中,您需要声明自定义事件。 In this case since you are trying to check(or uncheck I assume) a box your custom event declaration will look like: 在这种情况下,由于您尝试选中(或取消选中我认为)一个框,因此您的自定义事件声明将如下所示:

public event ChangeCheckedValue(byref state as boolean)

Now on your button click event you will need to raise the event to the handler on Form1: 现在在您的按钮单击事件上,您将需要将该事件引发给Form1上的处理程序:

RaiseEvent ChangeCheckedValue(booleanValue)

Now that those statements are in place you will need to changed your form2 object that is being shown by Form1. 现在这些语句就位了,您将需要更改由Form1显示的form2对象。 What I normally do is make Form2 a form wide variable on Form1 and declare it like: 我通常要做的是使Form2在Form1上成为表单范围的变量,并像这样声明它:

private withevents frm as Form2

Once you have the frm variable in your Form1 class you can add a handler for the ChangeCheckedValue event: 在Form1类中具有frm变量后,可以为ChangeCheckedValue事件添加处理程序:

protected sub HandleCheckChanged(byref bln as boolean) handles frm.ChangeCheckedValue
    'Set the checked state of your checkbox.
End sub

Once you have all that set up you should see what you expect. 完成所有设置后,您应该会看到期望的结果。

Alternatively you can pass a handle of form1 to form2 constructor 或者,您可以将form1的句柄传递给form2构造函数

Form1: 表格1:

Public Class Form1

  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim _form2 As New Form2(Me)

    _form2.Show()
  End Sub
End Class

Form2: 表格2:

Public Class Form2

  Public Sub New(ByVal _form1 As Form1)

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    _form1.CheckBox1.Checked = True

  End Sub


End Class

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

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