2014-07-26 77 views
0

我正在製作一個程序,其中有三個文本框和一個計算按鈕,並且我只希望在所有三個文本框中都有值時啓用計算按鈕。這是我使用目前的代碼,但該按鈕保持啓用狀態,即使所有三個文本框爲空文本框和啓用按鈕

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    FirstNumber = CInt(TextBox1.Text) 
    SecondNumber = CInt(TextBox2.Text) 
    ThirdNumber = CInt(TextBox3.Text) 
    Result = (SecondNumber - FirstNumber)/ThirdNumber 
    Label5.Text = Result 
    If TextBox1.Text = "" And TextBox2.Text = "" And TextBox3.Text = "" Then 
     Button1.Enabled = False 
    End If 

如何解決這個問題?

+0

如果用戶鍵入字母你要啓用按鈕「然後在下面的代碼中使用雙像」號碼? – Steve

+0

用戶鍵入數字,我只希望按鈕被啓用,如果所有三個文本框都包含數字 – Todd432

+0

您應該看看3個文本框的驗證事件 – Steve

回答

0

您必須爲每個文本框的text_changed事件編寫代碼。當輸入文本時,按鈕不知道文本框中發生了什麼......

連接text_changed事件,您將必須評估所有三個文本框的文本更改事件的全部3個文本框。相應地禁用您的按鈕。

+0

我已經從按鈕的每個文本框的文本更改事件移動if語句但還是行不通 – Todd432

1

只有一個事件處理程序的所有3個文本框。
在事件代碼,你檢查的文本框的值,並設置一個布爾值來啓用或不按鈕

Private Sub textBoxes_Validating(ByVal sender As Object, _ 
       ByVal e As System.ComponentModel.CancelEventArgs) _ 
       Handles textBox1.Validating, textBox2.Validating, textBox3.Validating 

    Dim areValid As Boolean = True 
    Dim intValue As Integer 

    if Not Int32.TryParse(textBox1.Text, intValue) Then 
     areValid = false 
    else if Not Int32.TryParse(textBox2.Text, intValue) Then 
     areValid = false 
    else if Not Int32.TryParse(textBox3.Text, intValue) Then 
     areValid = false 
    End If 
    Button1.Enable = areValid 
    ' You could force the focus to remain on the invalid textbox enabling this line 
    ' e.Cancel = True 
End Sub 
0

把對TextBox1_TextChanged所有3個文本框的TextChange事件。
它會檢查它是否只包含數字。「如果不在其他地方再次禁用按鈕」
當然,該按鈕需要在開始時被禁用。

,也是我不知道,如果你想有一個像1.2

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox3.TextChanged, TextBox2.TextChanged 
    If IsNumeric(TextBox1.Text) = True And IsNumeric(TextBox2.Text) = True And IsNumeric(TextBox3.Text) = True Then 
     Button1.Enabled = True 
    Else 
     Button1.Enabled = False 
    End If 
End Sub 

Dim FirstNumber As Double 
Dim SecondNumber As Double 
Dim ThirdNumber As Double 
Dim Result As Double 

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    FirstNumber = CDbl(TextBox1.Text) 
    SecondNumber = CDbl(TextBox2.Text) 
    ThirdNumber = CDbl(TextBox3.Text) 
    Result = (SecondNumber - FirstNumber)/ThirdNumber 
    Label5.Text = Result 
End Sub