当我单击我创建的清除按钮时,我创建了一个类来处理清除文本框和其他控件。我目前的项目只有一个来自frmMain。我想要做的是设置类,以便它可以清除控件从哪个窗体调用类。如何设置我的类来识别哪个表单正在调用它?我在VB.net工作,但如果需要,我可以从C#翻译。
Private Sub btnClear_Click(sender As System.Object, e As System.EventArgs) Handles btnClear.Click
Dim iClear As New ClearForm
iClear.ClearTextBoxes()
End Sub
Public Class ClearForm
Public Sub ClearTextBoxes(Optional ByVal ctlcol As Control.ControlCollection = Nothing)
If ctlcol Is Nothing Then ctlcol = frmMain.Controls
For Each ctl As Control In ctlcol
If TypeOf (ctl) Is ComboBox Then
DirectCast(ctl, ComboBox).Text = Nothing
Else
If Not ctl.Controls Is Nothing OrElse ctl.Controls.Count <> 0 Then
ClearTextBoxes(ctl.Controls)
End If
End If
Next
If ctlcol Is Nothing Then ctlcol = frmMain.Controls
For Each ctl As Control In ctlcol
If TypeOf (ctl) Is TextBox Then
DirectCast(ctl, TextBox).Clear()
Else
If Not ctl.Controls Is Nothing OrElse ctl.Controls.Count <> 0 Then
ClearTextBoxes(ctl.Controls)
End If
End If
Next
If ctlcol Is Nothing Then ctlcol = frmMain.Controls
For Each ctl As Control In ctlcol
If TypeOf (ctl) Is CheckBox Then
DirectCast(ctl, CheckBox).Checked = False
Else
If Not ctl.Controls Is Nothing OrElse ctl.Controls.Count <> 0 Then
ClearTextBoxes(ctl.Controls)
End If
End If
Next
End Sub
结束班
答案 0 :(得分:2)
你有一般的想法,但你错过了对表单的引用,并且迭代控件集合的次数超过了必要的次数。我对此的看法是让类能够接受表单引用或控件集合,然后执行单个递归循环而不是几个:
Public Class ClearForm
Private _Controls As Control.ControlCollection = Nothing
Public Sub New(ByRef ControlCollection As Control.ControlCollection)
Me._Controls = ControlCollection
End Sub
Public Sub New(ByRef FormToClear As Form)
Me._Controls = FormToClear.Controls
End Sub
Public Sub ClearControls()
RecursiveClearControls(Me._Controls)
End Sub
Private Sub RecursiveClearControls(ctlCol As Control.ControlCollection)
If ctlCol IsNot Nothing AndAlso ctlCol.Count > 0 Then
For Each ctl As Control In ctlCol
If TypeOf (ctl) Is ComboBox Then
DirectCast(ctl, ComboBox).Text = Nothing
ElseIf TypeOf (ctl) Is TextBox Then
DirectCast(ctl, TextBox).Clear()
ElseIf TypeOf (ctl) Is CheckBox Then
DirectCast(ctl, CheckBox).Checked = False
Else
If ctl.Controls.Count > 0 Then
RecursiveClearControls(ctl.Controls)
End If
End If
Next
End If
End Sub
End Class
这样,您可以使用表单引用或控件集合来实例化新类,然后只需调用ClearControls()
。调用时,它会启动一个私有递归循环,它只会迭代控件一次。此外,通过ByRef而不是ByVal有助于提高效率。
答案 1 :(得分:0)
正如Soo所说 - 添加一个New构造函数和一个属性,以便您可以引用它。虽然ClearTextBoxes可以稍微优化,但我已经单独留下了它,因为它适合你。希望这有助于.. :))
Public Class ClearForm
Private m_FormToClear As frmMain
Public Property FormToClear As frmMain
Get
Return m_FormToClear
End Get
Set(value As frmMain)
m_FormToClear = value
End Set
End Property
Public Sub New(frm As frmMain)
m_FormToClear = frm
End Sub
Public Sub ClearTextBoxes(Optional ByVal ctlcol As Control.ControlCollection = Nothing)
If ctlcol Is Nothing Then ctlcol = m_FormToClear.Controls
For Each ctl As Control In ctlcol
If TypeOf (ctl) Is ComboBox Then
DirectCast(ctl, ComboBox).Text = Nothing
Else
If Not ctl.Controls Is Nothing OrElse ctl.Controls.Count <> 0 Then
ClearTextBoxes(ctl.Controls)
End If
End If
Next
If ctlcol Is Nothing Then ctlcol = m_FormToClear.Controls
For Each ctl As Control In ctlcol
If TypeOf (ctl) Is TextBox Then
DirectCast(ctl, TextBox).Clear()
Else
If Not ctl.Controls Is Nothing OrElse ctl.Controls.Count <> 0 Then
ClearTextBoxes(ctl.Controls)
End If
End If
Next
If ctlcol Is Nothing Then ctlcol = m_FormToClear.Controls
For Each ctl As Control In ctlcol
If TypeOf (ctl) Is CheckBox Then
DirectCast(ctl, CheckBox).Checked = False
Else
If Not ctl.Controls Is Nothing OrElse ctl.Controls.Count <> 0 Then
ClearTextBoxes(ctl.Controls)
End If
End If
Next
End Sub
End Class
然后使用..打电话
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim CF As ClearForm = New ClearForm(Me)
CF.ClearTextBoxes()
End Sub
答案 2 :(得分:0)
另一种创建基类的方法,它将知道它所属的形式
Public MustInherit Class ClearControlsForm
Inherits System.Windows.Forms.Form
Protected Sub ClearControls()
ClearControls(Me.Controls)
End Sub
Protected Overridable Sub ClearControls(controls As Control.ControlCollection)
For Each textbox In controls.OfType(Of TextBox)
textbox.Text = String.Empty
Next
For Each checkBox In controls.OfType(Of CheckBox)
checkBox.Checked = False
Next
' ... clear other controls types
End Sub
End Class
然后所有新表单都继承自ClearControlsForm
,您可以使用基类中的ClearControls
方法
Public Class MyAnotherForm
Inherits MyNamespace.ClearControlsForm
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
ClearControls() // Will clear current form's controls
End Sub
End Class
如果使ClearControls
方法可以覆盖,则可以在派生类中自定义它的行为。
使用这种方法,您将在表单类中保留与表单相关的逻辑,这将很容易维护。