-2

I have 9 TextBoxes called Textbox1 all the way to Textbox9. I also have a button called Letter which selects a random letter from an array. I have another button called Start. This button when pressed displays a timer counting down from 30 seconds all the to zero. I want this button to take place only if all the 9 textboxes have text in them. I tried the following but it did not seem to work:

The flash
  • 1
  • 1
  • 3
  • You may want to read something about disabling buttons so that you set `BtnStart.Enabled = false` if the user is not allowed to press it. – Alex Apr 21 '15 at 14:42

3 Answers3

1

Change all of your "And" in your if statement to "Or"

Private Sub BtnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStart.Click
    If TxtBox1.Text = "" Or TxtBox2.Text = "" Or TxtBox3.Text = "" Or 
       TxtBox4.Text = "" Or TxtBox5.Text = "" Or TxtBox6.Text = "" Or
       TxtBox7.Text = "" Or TxtBox8.Text = "" Or TxtBox9.Text = "" Then
       TmrGame.Enabled = False
       MessageBox.Show("There Must Be A Total Of Nine Letters To Start The Game", "Not Enough Letters")
    Else
       TmrGame.Enabled = True
    End If
End Sub

By using And, you're checking that all of the text boxes must be blank, then you'll set your timer.Enabled to false. With Or, you're saying if any of the text boxes are blank you'll set the timer.Enabled to false.

Shar1er80
  • 9,001
  • 2
  • 20
  • 29
1

Shar1er80's answer is correct. Another method is to enable or disable the button, once the text of any of the textboxes changes. You can do this easily using event handlers:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim Boxes As TextBox() = {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5, TextBox6, TextBox7, TextBox8, TextBox9}
    For Each b In Boxes
        AddHandler b.TextChanged, Sub(s, ee)
                                      btnStart.Enabled = Not Boxes.Any(Function(box) box.Text = "")
                                      'The same, not using a negation would be:
                                      'btnStart.Enabled = Boxes.All(Function(box) box.Text <> "")
                                  End Sub
    Next
End Sub

First I'm creating an array that contains all the textboxes. They are objects like anything else. Then I'm using the AddHandler keyword to wire the TextChanged event of each box to a sub. In the Sub I use LINQ to check if any of the textboxes is empty and disable the button if so (this is similar to checking each box in an or connected If statement).

Jens
  • 6,275
  • 2
  • 25
  • 51
0

Try something like this:

Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
    Private Sub BtnStart_Click(sender As Object, e As EventArgs) Handles BtnStart.Click
        Dim execute As Boolean = True
        For i As Integer = 1 To 9
            If DirectCast(Me.Controls("TextBox" & i.ToString), TextBox).Text = "" Then execute = False
        Next
        If execute Then
            'Your code to execute here
        Else
            MessageBox.Show("Please provide input in all fields.", "Incomplete Submission", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        End If
    End Sub
End Class
Paul Ishak
  • 1,093
  • 14
  • 19