2017-04-02 79 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