有没有办法隐藏MessageBox?

时间:2016-08-02 08:59:23

标签: vb.net

有没有办法在表单加载时隐藏MessageBox?

我使用了Checkedlistbox,并且checkeditems已加载Form2

我想要做的是,当我点击Form1时,Form2显示Checkedlistbox。我的问题是,当我点击Form1时,它会在Form2之前显示MessageBox。

这是我在vb.net上的代码:

Form1

Private Sub cmdSubmitModifyQuant_Click(sender As Object, e As EventArgs) Handles cmdSubmitModifyQuant.Click
    Form2.Show()
End Sub

Form2

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    chklstBox1Fill()
End Sub

Private Sub CheckedListBox1_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
    If e.NewValue = CheckState.Checked Then
      question = MsgBox("Area you sure you want to remove?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Message")
        If question = MsgBoxResult.Yes Then
           'Nevermind
        ElseIf question = MsgBoxResult.No Then
          e.NewValue = CheckState.Checked
        End If
    End If
End Sub

在我的代码中,您可以看到我还需要检查checklistbox1

2 个答案:

答案 0 :(得分:1)

这个问题可能是在chklstBox1Fill方法中检查复选框列表中的项目,这会导致事件提升,显示复选框。避免这种情况的一种方法是设置一个标志,表示你正在填充列表,而不是在设置标志时显示消息框:

Private FillingList As Boolean

Private Sub chklstBox1Fill()
    FillingList = True

    'Rest of method here.

    FillingList = False
End Sub

Private Sub CheckedListBox1_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
    If FillingList = True Then
        Return
    End If

    If e.NewValue = CheckState.Checked Then
      question = MsgBox("Area you sure you want to remove?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Message")
        If question = MsgBoxResult.Yes Then
           'Nevermind
        ElseIf question = MsgBoxResult.No Then
          e.NewValue = CheckState.Checked
        End If
    End If
End Sub

(原谅我的VB.Net,自从我写了几年以来已经好几年了!)

答案 1 :(得分:1)

添加一个布尔变量,指示您的加载过程是否完成。这样做不会执行CheckedChanged,直到变量设置为True。

Dim FormLoaded As Boolean = False

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    chklstBox1Fill()
    FormLoaded = True
End Sub

Private Sub CheckedListBox1_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
    If FormLoaded = False Then Return 'Don't execute the rest of the code if it evaluates to False.

    If e.NewValue = CheckState.Checked Then
        question = MsgBox("Area you sure you want to remove?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Message")
        If question = MsgBoxResult.Yes Then
            'Nevermind
        ElseIf question = MsgBoxResult.No Then
            e.NewValue = CheckState.Checked
        End If
    End If
End Sub