包含表单的用户控件控件 [英] user control controling containing form
问题描述
我正在使用 vb.net winforms 创建一个包含用户控件的应用程序,我也在创作用户控件.
I am using vb.net winforms to create an application which contains a user control, I am also authoring the user control.
用户控件名为 activate1
The user control is named activate1
包含用户控件(和其他内容)的表单名为 form1
The form containing the usercontrol (and other content) is named form1
目前我正在尝试在 activate1 上的掩码文本框完成时启用 form1 上的按钮
currently I am trying to enable a button on form1 when a maskedtextbox on activate1 is completed
我试图通过以下方式做到这一点:
I attempted to do this by:
Private Sub MaskedTextBox1_TextChanged(sender As Object, e As EventArgs) Handles MaskedTextBox1.TextChanged
If MaskedTextBox1.Text.Count = 34 Then
Form1.SimpleButton1.Enabled = True
Else
Form1.SimpleButton1.Enabled = False
End If
End Sub
但是我在运行应用程序时收到一个错误表单在从默认实例构建期间引用了自己,这导致了无限递归"所以我尝试了
however I receive an error when running the application "The form referred to itself during construction from a default instance, which led to infinite recursion" so instead I tried
Private Sub MaskedTextBox1_TextChanged(sender As Object, e As EventArgs) Handles MaskedTextBox1.TextChanged
If MaskedTextBox1.Text.Count = 34 Then
me.SimpleButton1.Enabled = True
Else
me.SimpleButton1.Enabled = False
End If
End Sub
但是这个标志并且不允许编译,因为我指的是用户控件中不存在的按钮
however this flags and does not allow compiling as I am referring to a button that does not exist in the user control
我怎样才能做到这一点
推荐答案
第一个错误的原因是您使用类型名称来引用 Form1,其中您需要父表单的具体实例.您可以通过访问 ParentForm-属性:
The reason for the 1st error is that you use the type name to refer to Form1 where you'd need the concrete instance of the parent form. You can access the parent form by accessing the ParentForm-property:
Private Sub MaskedTextBox1_TextChanged(sender As Object, e As EventArgs) Handles MaskedTextBox1.TextChanged
Dim form = TryCast(ParentForm, Form1)
If form IsNot Nothing Then
If MaskedTextBox1.Text.Count = 34 Then
form.SimpleButton1.Enabled = True
Else
form.SimpleButton1.Enabled = False
End If
End If
End Sub
请注意,这种方法将您的 Form 和您的 UserControl 紧密结合在一起.虽然它可以工作,但它降低了 UserControl 的可重用性(实际上,您只能在 Form1 上使用它,而不能在其他表单上使用它).虽然你可以改进这一点,例如通过创建界面,但 UserControl 将始终以一种或另一种方式与支持此按钮的表单相关联.
一种更可重用的方法是在您的 UserControl 中引发一个事件并在父表单中处理它,例如:
Please note that this approach couples your Form and your UserControl tightly. Though it will work, it reduces the reusability of your UserControl (in fact, you can only use it on Form1 and not on other forms). Though you can improve this, e.g. by creating an interface, but the UserControl will always by tied to the forms that support this button in one way or the other.
A more reusable approach would be to raise an event in your UserControl and handle this in the parent form, e.g.:
Public Class ActivationInfoEventArgs
Inherits EventArgs
Public Property Completed As Boolean
End Class
Public Event ActivationInfoChanged As EventHandler(Of ActivationInfoEventArgs)
Private Sub MaskedTextBox1_TextChanged(sender As Object, e As EventArgs) Handles MaskedTextBox1.TextChanged
Dim completed As Boolean
If MaskedTextBox1.Text.Count = 34 Then
completed = True
Else
completed = False
End If
RaiseEvent ActivationInfoChanged(Me, New ActivationInfoEventArgs() With { .Completed = completed })
End Sub
您为 Form1 中的事件添加一个处理程序并(取消)激活此处理程序中的按钮.这样,您就可以在更广泛的表单上使用 UserControl.
You add a handler for the event in Form1 and (de-) activate the button in this handler. This way, you can use the UserControl on a wider variety of Forms.
这篇关于包含表单的用户控件控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!