1

我有两种表格,表格 1 和表格 2(Windows 应用程序)。如何从表单 2 访问和检查表单 1 中的复选框。最初我尝试调用表单名称,然后调用控件form1.chkCanada.checked = true,但它不起作用。然后我在表格 1 中添加了一个属性

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

然后在表格 2 中

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

它仍然不起作用。我在这里错过了什么吗?

4

2 回答 2

0

或者,您可以将 form1 的句柄传递给 form2 构造函数

表格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

表格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
于 2013-05-09T15:33:01.230 回答
0

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:

The simpler of the two options is to switch your form.show() function calls to 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. 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.

So in your Form2 class you will need to declare the custom event. 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:

RaiseEvent ChangeCheckedValue(booleanValue)

Now that those statements are in place you will need to changed your form2 object that is being shown by Form1. What I normally do is make Form2 a form wide variable on Form1 and declare it like:

private withevents frm as Form2

Once you have the frm variable in your Form1 class you can add a handler for the ChangeCheckedValue event:

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.

于 2013-05-08T17:31:08.227 回答