每次调用 ShowDialog 时删除控件
Deleting controls each time ShowDialog is called
我有一个专门使用 ShowDialog
方法显示的表单。在 Form_Shown
事件中,我根据调用表单中设置的 public 变量动态创建了一组标签和文本框。
我知道表单没有关闭或销毁,而只是隐藏在调用之间,因此我在 Form_Shown
事件的顶部添加了代码以清除之前调用的所有控件,但是控件没有被删除。我试过 ctrl.Dispose
(如下面的代码)和 Me.Controls.Remove(ctrl)
。两者都不会产生错误,但不会删除文本框并在其上创建新的文本框。 (出于某种原因,
这是我第一次在 .NET 中动态 created/removed 控件,所以可能是我对 VB6 的控件数组的向往与错误有关。
表单基于调用表单的 public ListView 变量构建自身。调用表单确保此变量不是空的,并且当且仅当用户正在编辑现有行时才会选择项目。
Public Class frmTableEdit
Private isNew As Boolean
Private inputText() As TextBox
Private Sub FormTableEdit_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
For Each ctrl As Control In Me.Controls
If TypeOf (ctrl) Is TextBox Or TypeOf (ctrl) Is Label Then
ctrl.Dispose()
End If
Next
With frmKioskData.TBLobj
Dim fieldCount As Integer = .Columns.Count - 1
isNew = .SelectedIndices.Count = 0
'(code setting size of form and location of OK/Cancel buttons is here)
ReDim inputText(fieldCount)
For i As Integer = 0 To fieldCount
Dim lbl As New Label, txt As New TextBox
inputText(i) = txt
Me.Controls.Add(lbl)
Me.Controls.Add(txt)
'(code setting size and and position of lbl & txt is here)
'lbl.Tag = i (I commented these lines out because I never used the
'txt.Tag = i Tag property, but can if a solution calls for it.)
lbl.Text = .Columns(i).Text
If isNew Then
txt.Text = ""
Else
txt.Text = .Items(.SelectedIndices(0)).SubItems(i).Text
End If
Next
End With
End Sub
End Class
如果上面评论中@Plutonix 的建议对你不太适用,我认为在调用 ShowDialog 之后处理表单将是最简单和更有意义的,然后当你需要显示你创建的表单时一个带有参数的新实例,该参数告诉它要加载哪些动态控件。
所以您在 frmTableEdit 中有一个使用此参数的新方法:
Public Sub New(ByVal fieldCount As Integer)
InitializeComponent()
fieldCount = fieldCount 'Where fieldCount is a class variable
End Sub
当您从 frmKioskData 调用此表单时,您可以这样做:
Dim newTableEdit As New frmTableEdit(Me.TBLobj.Columns.Count - 1)
newTableEdit.ShowDialog()
newTableEdit.Dispose()
然后您的 frmEditTable 的 Shown 事件中的代码只需简单地添加相应的控件,而无需删除旧控件:
ReDim inputText(fieldCount)
For i As Integer = 0 To fieldCount
Dim lbl As New Label, txt As New TextBox
inputText(i) = txt
Me.Controls.Add(lbl)
Me.Controls.Add(txt)
'(code setting size and and position of lbl & txt is here)
'lbl.Tag = i (I commented these lines out because I never used the
'txt.Tag = i Tag property, but can if a solution calls for it.)
lbl.Text = .Columns(i).Text
If isNew Then
txt.Text = ""
Else
txt.Text = .Items(.SelectedIndices(0)).SubItems(i).Text
End If
Next
如何获取 isNew 值由您决定 - 您可以将其作为第二个参数添加到表单的 New 中,或者以与现在相同的方式获取它...w/e。如果是我,我通常会将其添加为另一个新参数。
我有一个专门使用 ShowDialog
方法显示的表单。在 Form_Shown
事件中,我根据调用表单中设置的 public 变量动态创建了一组标签和文本框。
我知道表单没有关闭或销毁,而只是隐藏在调用之间,因此我在 Form_Shown
事件的顶部添加了代码以清除之前调用的所有控件,但是控件没有被删除。我试过 ctrl.Dispose
(如下面的代码)和 Me.Controls.Remove(ctrl)
。两者都不会产生错误,但不会删除文本框并在其上创建新的文本框。 (出于某种原因,
这是我第一次在 .NET 中动态 created/removed 控件,所以可能是我对 VB6 的控件数组的向往与错误有关。
表单基于调用表单的 public ListView 变量构建自身。调用表单确保此变量不是空的,并且当且仅当用户正在编辑现有行时才会选择项目。
Public Class frmTableEdit
Private isNew As Boolean
Private inputText() As TextBox
Private Sub FormTableEdit_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
For Each ctrl As Control In Me.Controls
If TypeOf (ctrl) Is TextBox Or TypeOf (ctrl) Is Label Then
ctrl.Dispose()
End If
Next
With frmKioskData.TBLobj
Dim fieldCount As Integer = .Columns.Count - 1
isNew = .SelectedIndices.Count = 0
'(code setting size of form and location of OK/Cancel buttons is here)
ReDim inputText(fieldCount)
For i As Integer = 0 To fieldCount
Dim lbl As New Label, txt As New TextBox
inputText(i) = txt
Me.Controls.Add(lbl)
Me.Controls.Add(txt)
'(code setting size and and position of lbl & txt is here)
'lbl.Tag = i (I commented these lines out because I never used the
'txt.Tag = i Tag property, but can if a solution calls for it.)
lbl.Text = .Columns(i).Text
If isNew Then
txt.Text = ""
Else
txt.Text = .Items(.SelectedIndices(0)).SubItems(i).Text
End If
Next
End With
End Sub
End Class
如果上面评论中@Plutonix 的建议对你不太适用,我认为在调用 ShowDialog 之后处理表单将是最简单和更有意义的,然后当你需要显示你创建的表单时一个带有参数的新实例,该参数告诉它要加载哪些动态控件。
所以您在 frmTableEdit 中有一个使用此参数的新方法:
Public Sub New(ByVal fieldCount As Integer)
InitializeComponent()
fieldCount = fieldCount 'Where fieldCount is a class variable
End Sub
当您从 frmKioskData 调用此表单时,您可以这样做:
Dim newTableEdit As New frmTableEdit(Me.TBLobj.Columns.Count - 1)
newTableEdit.ShowDialog()
newTableEdit.Dispose()
然后您的 frmEditTable 的 Shown 事件中的代码只需简单地添加相应的控件,而无需删除旧控件:
ReDim inputText(fieldCount)
For i As Integer = 0 To fieldCount
Dim lbl As New Label, txt As New TextBox
inputText(i) = txt
Me.Controls.Add(lbl)
Me.Controls.Add(txt)
'(code setting size and and position of lbl & txt is here)
'lbl.Tag = i (I commented these lines out because I never used the
'txt.Tag = i Tag property, but can if a solution calls for it.)
lbl.Text = .Columns(i).Text
If isNew Then
txt.Text = ""
Else
txt.Text = .Items(.SelectedIndices(0)).SubItems(i).Text
End If
Next
如何获取 isNew 值由您决定 - 您可以将其作为第二个参数添加到表单的 New 中,或者以与现在相同的方式获取它...w/e。如果是我,我通常会将其添加为另一个新参数。