2017-04-02 62 views
1

在我的代码,当TextBox3没有任何价值,它必须显示在MsgBox通知书TextBox1消息框出现第二次

输入一个值,但是,当我运行它两次,在屏幕上出现的MsgBox通知当它应该只显示一次。

这里是我的代码:

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged 
    If TextBox3.Text = Nothing Then 
     TextBox1.Clear() 
     MsgBox("Enter Number to Textbox1") 
    Else 
     Dim digit As Integer = CInt(TextBox3.Text) 
     If TextBox1.TextLength = digit Then 
      Dim fields() As String = ListBox1.Text.Split(";") 
      Dim idx As Integer = ListBox1.FindString(TextBox1.Text) 
      If idx <> -1 Then 
       ListBox1.SelectedIndex = idx 
       ListBox1.SelectedIndex.ToString(fields(0)) 
       ListBox2.Items.Add(Now() + Space(1) + ListBox1.Text.Substring(0, 13)) 
       PrintDocument1.Print() 
      Else 
       TextBox1.Clear() 
      End If 
     End If 
    End If 
End Sub 
+0

您的问题有答案。请不要忘记提供帮助你的答案,并接受答案。 –

+0

@ 3vts:未来,请不要**添加**无用的绒毛,如“感谢您的帮助”,“提前致谢”或“祝您有愉快的一天”。相反(如果已经编辑帖子)**删除**依照[政策](https://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed -from-职位);他们不应该故意包含在帖子中。 –

回答

0

这里的问题是该事件处理程序被触发另一次是因为清除TextBox1的等于textbox1_changed事件处理程序中去。你也可以禁用文本框,直到textbox3不再是任何东西。 或快速的解决办法是藏汉

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged 
If not TextBox1.Text = Nothing AndAlso TextBox3.text = Nothing Then 
    TextBox1.Clear() 
    MsgBox("Enter Number to Textbox1") 
............. 
0

您正在使用错误的事件。当您清除文本框时,Textchanged触发器也会导致两个消息框。

使用LostFocus代替

0

这里是解决方案,

Public Class Form1 
      Dim message as boolean = true  
    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged 
      If TextBox3.Text = Nothing Then 
      If message Then 'show the message as true 
      message = False 'set the message false for textbox_changed not appear again 
      Textbox1.Clear() 
      message = True 'set the message true for next time textbox change appear again 
      MsgBox("Enter Number to Textbox3") 
    End If 
        Else 
         Dim digit As Integer = CInt(TextBox3.Text) 
         If TextBox1.TextLength = digit Then 
          Dim fields() As String = ListBox1.Text.Split(";") 
          Dim idx As Integer = ListBox1.FindString(TextBox1.Text) 
          If idx <> -1 Then 
           ListBox1.SelectedIndex = idx 
           ListBox1.SelectedIndex.ToString(fields(0)) 
           ListBox2.Items.Add(Now() + Space(1) + ListBox1.Text.Substring(0, 13)) 
           PrintDocument1.Print() 
          Else 
           TextBox1.Clear() 
          End If 
         End If 
        End If 
       End Sub