调试程序时,UserControl的按钮消失

时间:2014-03-07 15:50:41

标签: vb.net

调试程序时,我的UserControl按钮消失了。我已经检查了包括designer.vb代码在内的代码无数次没有任何东西可以使按钮.enabled = false.visible = false。任何想法为什么会发生这种情况?

在我的UserControl上:

 Private Sub btn_Begin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Begin.Click
    Start_Race()
End Sub

Public Sub Start_Race()
    TimeNow(Past_Time)
    TimeNow(Start_Time)
    lbl_Start_Time_Driver.Text = Past_Time
    btn_Begin.BackColor = Color.Green
    btn_Begin.Text = "Started!"
End Sub

Public Property Active_bool As Boolean
    Get
        Return btn_Begin.Visible
    End Get
    Set(ByVal value As Boolean)
        btn_Begin.Visible = value
    End Set
End Property

在Form1上:

Private Sub btn_Start_All_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Start_All.Click
    Dim allActiveUserControls = From uc_Index In Controls.OfType(Of LapTimerGUI)()
    Where uc_Index.Active_bool
    For Each User_Control In allActiveUserControls
        User_Control.Start_Race()
    Next
End Sub

在我发布我的荒谬问题之前,我先把谷歌关掉了,顺便说一句:)

2 个答案:

答案 0 :(得分:1)

这很奇怪。如果将此代码添加到UserControl,是否会弹出任何MsgBox:

Private Sub UserControl_ControlRemoved(sender As Object, e As System.Windows.Forms.ControlEventArgs) Handles Me.ControlRemoved
    MsgBox("Control Removed!")
End Sub

Private Sub Button2_EnabledChanged(sender As Object, e As System.EventArgs) Handles Button2.EnabledChanged
    MsgBox("EnabledChanged!")
End Sub

如果是这样,那么你可以为这些MsgBox添加一个断点并锁定它触发的CallStack(CTRL+L)。

顺便说一下:如果以某种方式删除了控件,.PerformClick()仍会触发(对我来说)。因此我敢打赌,控件以某种方式被禁用(Enabled = False)。

最后,如果按钮的任何容器(例如您的UserControl)被禁用,该按钮也将被禁用,

答案 1 :(得分:0)

经过大量的游戏,我终于找到了问题!

我的属性中的值设置为=False。我太白了!谢谢大家的帮助^ _ ^ /

    Public Property Active_bool As Boolean
    Get
        Return btn_Begin.Visible
    End Get
    Set(ByVal value As Boolean)
        btn_Begin.Visible = value
    End Set
End Property

虽然,Something偶尔会将值设置为=False。非常讨厌:3 我无法在属性中将值设置为=True ...仅在隐藏的设计器代码中...