Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select Case 1

Status
Not open for further replies.

sp76

Programmer
Jul 1, 2003
59
AU
How can achieve the following with Select Case Statement.

'Validate data.
If txtFirstName.Text = "" Then
MessageBox.Show("Please provide First Name", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtFirstName.Focus()
Exit Sub
Else
If txtLastName.Text = "" Then
MessageBox.Show("Please provide Last Name ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtLastName.Focus()
Exit Sub
Else
If txtDOB.Text = "" Then
MessageBox.Show("Please provide DOB", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtDOB.Focus()
Exit Sub
Else
If txtTime.Text = "" Then
MessageBox.Show("Please provide Time", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtTime.Focus()
Exit Sub
End If
End If
End If
End If
 
thats a simple one.

you cant make it into a select case.

Christiaan Baes
Belgium
"What a wonderfull world" - Louis armstrong
 
chrissie's right, you can only use select case if you are testing the same variable in each section.

if you just want ot make your code look elegant, how about using the ELSEIF...

If txtFirstName.Text = "" Then
[do stuff]
ElseIf txtLastName.Text = "" Then
[do stuff]
ElseIf txtDOB.Text = "" Then
[do stuff]
ElseIf txtTime.Text = "" Then
[do stuff]
Else
[do stuff]
End If
 
Code:
    Private Sub txtBox_Validated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtFirstName.Validated, txtLastName.Validated, txtDOB.Validated, txtTime.Validate

  Dim t as TextBox

  t = DirecCast(sender, TextBox)

  If t.Text = "" Then
    MessageBox.Show("Please provide " & t.Name.SubString(2), _ 
     "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    t.Focus()
   End If

Something like this should work. Won't have all that code for If and IfElse's
 
If txtFirstName.Text = "" Then
MessageBox.Show("Please provide First Name", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtFirstName.Focus()
Exit Sub
End If
If txtLastName.Text = "" Then
MessageBox.Show("Please provide Last Name ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtLastName.Focus()
Exit Sub
End If
If txtDOB.Text = "" Then
MessageBox.Show("Please provide DOB", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtDOB.Focus()
Exit Sub
End If
If txtTime.Text = "" Then
MessageBox.Show("Please provide Time", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtTime.Focus()
Exit Sub
End If

You don't need else when you do exit sub on every condition
 
Yes, but whats the point of using the 24 lines of code. You are doing the same thing 4 times. The way I showed is 7 lines.

And later, if you put more fields in, you just have to name your textbox in an approiate manner, eg txtSuburb, to get Suburb to be displayed in the message box if its not entered and then just add the handler to the already coded sub.
 
Nice code, SJN.

Bobisto, Exit Sub breaks a basic programming paradigm. There are only ever the need for 3 types of code, Continuation, Branching and Looping. Exit Sub does not fit into these.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top