Join 109,157 VB.NET Programmers for FREE! Ask your question and get quick answers from experts. There are 1,028 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!
I've been stuck on this awhile...If a letter is placed in the numberRegistered text box then a messagebox is suppose to come up telling the user to enter a number. Here the code I have as of right now...
CODE
Option Explicit On Option Strict On
Public Class MainForm
Private Sub xExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xExitButton.Click Me.Close() End Sub
Private Sub xNumberTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles xNumberTextBox.Enter Me.xNumberTextBox.SelectAll() End Sub
Private Sub xNumberTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles xNumberTextBox.TextChanged Me.xTotalLabel.Text = String.Empty End Sub
Private Sub xCalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCalcButton.Click ' calculates the amount owed for a seminar
Const Message As String = "Please enter a number." Dim numberRegistered As Integer Dim pricePerPerson As Integer Dim totalOwed As Integer Dim isConverted As Boolean
'calculates and displays total depending on the number registered Integer.TryParse(Me.xTotalLabel.Text, totalOwed) Me.xTotalLabel.Text = CStr(numberRegistered * pricePerPerson)
I think the IF statement was incorrect and in the wrong place. Should it be like this.
QUOTE(LadyWolf @ 2 Jul, 2008 - 08:07 AM)
CODE
Option Explicit On Option Strict On
Public Class MainForm
Private Sub xExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xExitButton.Click Me.Close() End Sub
Private Sub xNumberTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles xNumberTextBox.Enter Me.xNumberTextBox.SelectAll() End Sub
Private Sub xNumberTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles xNumberTextBox.TextChanged Me.xTotalLabel.Text = String.Empty End Sub
Private Sub xCalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCalcButton.Click ' calculates the amount owed for a seminar
Const Message As String = "Please enter a number." Dim numberRegistered As Integer Dim pricePerPerson As Integer Dim totalOwed As Integer Dim isConverted As Boolean
'calculates and displays total depending on the number registered Integer.TryParse(Me.xTotalLabel.Text, totalOwed) ' if numberRegisted contains a letter display message and exit If numberRegistered.text Like "*[a-zA-Z]*" Then MessageBox.Show = Windows.Forms.DialogResult.OK("Please enter a number.") endsub End If
I think the IF statement was incorrect and in the wrong place. Should it be like this.
QUOTE(LadyWolf @ 2 Jul, 2008 - 08:07 AM)
CODE
Option Explicit On Option Strict On
Public Class MainForm
Private Sub xExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xExitButton.Click Me.Close() End Sub
Private Sub xNumberTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles xNumberTextBox.Enter Me.xNumberTextBox.SelectAll() End Sub
Private Sub xNumberTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles xNumberTextBox.TextChanged Me.xTotalLabel.Text = String.Empty End Sub
Private Sub xCalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCalcButton.Click ' calculates the amount owed for a seminar
Const Message As String = "Please enter a number." Dim numberRegistered As Integer Dim pricePerPerson As Integer Dim totalOwed As Integer Dim isConverted As Boolean
'calculates and displays total depending on the number registered Integer.TryParse(Me.xTotalLabel.Text, totalOwed) ' if numberRegisted contains a letter display message and exit If numberRegistered.text Like "*[a-zA-Z]*" Then MessageBox.Show = Windows.Forms.DialogResult.OK("Please enter a number.") endsub End If
Under the numberRegistered textbox keypress event try. This only allowthrough the number 0->9 , Delete, Return & enter
VB
Private Sub numberRegistered_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles numberRegistered.KeyPress Dim AllowedChar() As Char = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ChrW(Keys.Delete), ChrW(Keys.Return), ChrW(Keys.Enter)} If AllowedChar.Contains(e.KeyChar) = False Then ' Not an allowed char ' Display your messagebox ' Cancel the keypress e.Handled = True End If
I think this was your intention, you just need to utilize the Try.Parse in the IF statement.
CODE
Option Explicit On Option Strict On
Public Class MainForm
Private Sub xExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xExitButton.Click Me.Close() End Sub
Private Sub xNumberTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles xNumberTextBox.Enter Me.xNumberTextBox.SelectAll() End Sub
Private Sub xNumberTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles xNumberTextBox.TextChanged Me.xTotalLabel.Text = String.Empty End Sub
Private Sub xCalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCalcButton.Click ' calculates the amount owed for a seminar
Const Message As String = "Please enter a number." Dim numberRegistered As Integer Dim pricePerPerson As Integer Dim totalOwed As Integer Dim isConverted As Boolean
'calculates and displays total depending on the number registered IF Integer.TryParse(Me.xTotalLabel.Text, totalOwed) Then
I think this was your intention, you just need to utilize the Try.Parse in the IF statement.
CODE
Option Explicit On Option Strict On
Public Class MainForm
Private Sub xExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xExitButton.Click Me.Close() End Sub
Private Sub xNumberTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles xNumberTextBox.Enter Me.xNumberTextBox.SelectAll() End Sub
Private Sub xNumberTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles xNumberTextBox.TextChanged Me.xTotalLabel.Text = String.Empty End Sub
Private Sub xCalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCalcButton.Click ' calculates the amount owed for a seminar
Const Message As String = "Please enter a number." Dim numberRegistered As Integer Dim pricePerPerson As Integer Dim totalOwed As Integer Dim isConverted As Boolean
'calculates and displays total depending on the number registered IF Integer.TryParse(Me.xTotalLabel.Text, totalOwed) Then
Select Case numberRegistered Case 1 To 4 Me.xTotalLabel.Text = CStr(numberRegistered * 100)
Case 5 To 10 Me.xTotalLabel.Text = CStr(numberRegistered * 80)
Case Is >= 11 Me.xTotalLabel.Text = CStr(numberRegistered * 60)
Case Else Me.xTotalLabel.Text = CStr(numberRegistered * 0)
End Select
Else MessageBox.Show("Please enter a number.") End If
Me.xNumberTextBox.Focus()
End Sub End Class
Nope, that didn't work either I believe the problem is between the 'Const Message as string' and getting the MessageBox.Show to work with it..Way this is I put in a number and the messagebox comes up telling me to enter one...lmao It should only do that when a letter is put in.
Under the numberRegistered textbox keypress event try. This only allowthrough the number 0->9 , Delete, Return & enter
VB
Private Sub numberRegistered_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles numberRegistered.KeyPress Dim AllowedChar() As Char = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ChrW(Keys.Delete), ChrW(Keys.Return), ChrW(Keys.Enter)} If AllowedChar.Contains(e.KeyChar) = False Then ' Not an allowed char ' Display your messagebox ' Cancel the keypress e.Handled = True End If
End Sub
Buzzzzzz lol That didn't work either, thank-you though.
Just can't leave this one, so a last suggest. Insert a breakpoint at the point suggested in the code below.
A breakpoint stops the code at runtime, allowing it to step through to further aid debugging. To insert a breakpoint click on the line, right click-> Breakpoint -> Insert breakpoint
Run the code, enter some test data and click the button, the program will break at that line. Now by pressing F8 or Shift-F8 (This key combination ignore functions and subroutines.) to step through the code.
Examine the content of variable, either by highlighting the variable and hover the mouse over it. Or double-left click on the variable to highlight it, rightclick add watch. This will open a window titled Watches, and it will list all of the variable being watched.
Keep stepping though the code, nothing path / sequence the program takes.
I hope this will lead to further clue to the cause(es) of problem.
QUOTE(LadyWolf @ 2 Jul, 2008 - 08:07 AM)
CODE
Option Explicit On Option Strict On
Public Class MainForm
Private Sub xExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xExitButton.Click Me.Close() End Sub
Private Sub xNumberTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles xNumberTextBox.Enter Me.xNumberTextBox.SelectAll() End Sub
Private Sub xNumberTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles xNumberTextBox.TextChanged Me.xTotalLabel.Text = String.Empty End Sub
Private Sub xCalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCalcButton.Click ' calculates the amount owed for a seminar
Const Message As String = "Please enter a number." '<-- Insert a breakpoint here, step through the code. Dim numberRegistered As Integer <- Breakpoint will possible jump to this one? Dim pricePerPerson As Integer Dim totalOwed As Integer Dim isConverted As Boolean
'calculates and displays total depending on the number registered Integer.TryParse(Me.xTotalLabel.Text, totalOwed) Me.xTotalLabel.Text = CStr(numberRegistered * pricePerPerson)