我有一个自定义控件,其中有一个显示其他控件的面板。无论何时显示控件,我都将其添加到名为UCList的List(Of Control)中。然后,当他们点击“重新开始”按钮时,我有一个递归函数,可以清除UCList中的每个控件和任何子控件。
列表是否以不同方式处理ref / val?当我调用我的ResetUC函数时,调用的断点显示" oCon是oCashCash"但是,只要我通过传递Byref的变量进入重置函数,上面就是假的。它仍然是相同的类型,具有相同的名称,以及子项(具有相同的值)等。但它是一个新的实体。只需将其更改为接受Byval而不是Byref,一切正常(" oCon是oCashCash"即使进入重置功能后也是如此)。
这种行为对我来说似乎是倒退的。我的理解是错误的,还是由于某种原因列表处理参考不同?这是我第一次对ref / val在相当长的时间内如何响应感到困惑。
编辑: 控制级变量
Public UCList As List(Of Control)
将控件添加到列表中(大部分省略)
ElseIf ControlName = "ConfirmCashCash" Then
If Not UCList.Contains(oCashCash) Then
UCList.Add(oCashCash)
End If
pnlMain.Controls.Add(oCashCash)
Startover子点击事件(最初是foreach,但我试图弄明白)。此时,oCon是oCashCash - 编辑在评论中显示每个请求的完整按钮 -
Private Sub btnStartOver_Click(sender As Object, e As EventArgs) Handles btnStartOver.MouseUp
If pnlMain.Controls(0) Is oConfirmFinalize AndAlso oConfirmFinalize.btnFinalize.Text = "Finalize" Then
If MessageBox.Show("Deal Is Not Finalized. Are You Sure You Want To Leave This Page?", "Finalize Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.No Then
Return
End If
End If
If Not btnBack.Enabled Then
If MessageBox.Show("Deal Is Editing And Not Finalized. Are You Sure You Want To Leave This Page?", "Loss of Work Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.No Then
Return
End If
End If
ImpersonateUserID = String.Empty
'For Each con As Control In UCList
For i As Integer = 0 To UCList.Count - 1
Dim oCon As Control = UCList(i)
ResetUC(oCon)
Next
'ResetUC(New Control)
'Next
oConfirmFinalize.oLastCon = Nothing
oConfirmFinalize.oConfirm = New Confirm()
UCList.Clear()
'ResetUC(pnlMain.Controls(0))
ChangeUC("ConfirmMain", True, Nothing)
btnBack.Enabled = True
btnDelete.Visible = False
End Sub
重置sub - byref它不是oCashCash;然而,byval,它是。 (大多数省略)
Private Sub ResetUC(ByVal oCon As Control)
For Each con As Control In oCon.Controls
If TypeOf con Is ConfirmIndexDetail Then
DirectCast(con, ConfirmIndexDetail).DateNavigator1.DateTime = DateTime.Now()
End If
If con.HasChildren Then
For Each child As Control In con.Controls
ResetUC(child)
Next
End If
'Code that clears fields.
Next
End Sub