Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

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!



Const Message as String = "Please enter a number"

2 Pages V  1 2 >  
Reply to this topicStart new topic

Const Message as String = "Please enter a number", Having trouble with getting messagebox to work correctly

LadyWolf
post 2 Jul, 2008 - 12:07 AM
Post #1


D.I.C Head

**
Joined: 25 Jun, 2008
Posts: 122


My Contributions


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)

        isConverted = Integer.TryParse(Me.xNumberTextBox.Text, numberRegistered)

        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

        If Message Like "a-z" Then
            MessageBox.Show = Windows.Forms.DialogResult.OK("Please enter a number.")

        End If
        Me.xNumberTextBox.Focus()

    End Sub
End Class
User is offlineProfile CardPM

Go to the top of the page


AdamSpeight2008
post 2 Jul, 2008 - 01:09 AM
Post #2


D.I.C Addict

Group Icon
Joined: 29 May, 2008
Posts: 506



Thanked 34 times

Dream Kudos: 1900
My Contributions


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
            
Me.xTotalLabel.Text = CStr(numberRegistered * pricePerPerson)

        isConverted = Integer.TryParse(Me.xNumberTextBox.Text, numberRegistered)

        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

            Me.xNumberTextBox.Focus()

    End Sub
End Class


User is offlineProfile CardPM

Go to the top of the page

LadyWolf
post 2 Jul, 2008 - 08:20 AM
Post #3


D.I.C Head

**
Joined: 25 Jun, 2008
Posts: 122


My Contributions


QUOTE(AdamSpeight2008 @ 2 Jul, 2008 - 04:09 AM) *

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
            
Me.xTotalLabel.Text = CStr(numberRegistered * pricePerPerson)

        isConverted = Integer.TryParse(Me.xNumberTextBox.Text, numberRegistered)

        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

            Me.xNumberTextBox.Focus()

    End Sub
End Class




That didn't work, but thanks for trying...
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 2 Jul, 2008 - 08:22 AM
Post #4


DIC.Rules == true;

Group Icon
Joined: 26 Jul, 2007
Posts: 7,225



Thanked 52 times

Dream Kudos: 7775

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, GDI

My Contributions


Moved to VB.NET smile.gif
User is online!Profile CardPM

Go to the top of the page

AdamSpeight2008
post 2 Jul, 2008 - 08:38 AM
Post #5


D.I.C Addict

Group Icon
Joined: 29 May, 2008
Posts: 506



Thanked 34 times

Dream Kudos: 1900
My Contributions


QUOTE(LadyWolf @ 2 Jul, 2008 - 04:20 PM) *

That didn't work, but thanks for trying...


Ding,Ding "Round 2"

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
User is offlineProfile CardPM

Go to the top of the page

jayman9
post 2 Jul, 2008 - 09:00 AM
Post #6


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,320



Thanked 22 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


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

        Me.xTotalLabel.Text = CStr(numberRegistered * pricePerPerson)

        isConverted = Integer.TryParse(Me.xNumberTextBox.Text, numberRegistered)

        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
User is online!Profile CardPM

Go to the top of the page

LadyWolf
post 2 Jul, 2008 - 09:57 AM
Post #7


D.I.C Head

**
Joined: 25 Jun, 2008
Posts: 122


My Contributions


QUOTE(jayman9 @ 2 Jul, 2008 - 12:00 PM) *

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

        Me.xTotalLabel.Text = CStr(numberRegistered * pricePerPerson)

        isConverted = Integer.TryParse(Me.xNumberTextBox.Text, numberRegistered)

        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 crazy.gif 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.
User is offlineProfile CardPM

Go to the top of the page

LadyWolf
post 2 Jul, 2008 - 10:12 AM
Post #8


D.I.C Head

**
Joined: 25 Jun, 2008
Posts: 122


My Contributions


QUOTE(AdamSpeight2008 @ 2 Jul, 2008 - 11:38 AM) *

QUOTE(LadyWolf @ 2 Jul, 2008 - 04:20 PM) *

That didn't work, but thanks for trying...


Ding,Ding "Round 2"

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.
User is offlineProfile CardPM

Go to the top of the page

AdamSpeight2008
post 2 Jul, 2008 - 10:51 AM
Post #9


D.I.C Addict

Group Icon
Joined: 29 May, 2008
Posts: 506



Thanked 34 times

Dream Kudos: 1900
My Contributions


TKO in Round 2 turn.gif
User is offlineProfile CardPM

Go to the top of the page

AdamSpeight2008
post 2 Jul, 2008 - 11:15 AM
Post #10


D.I.C Addict

Group Icon
Joined: 29 May, 2008
Posts: 506



Thanked 34 times

Dream Kudos: 1900
My Contributions


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)

        isConverted = Integer.TryParse(Me.xNumberTextBox.Text, numberRegistered)

        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

        If Message Like "a-z" Then
            MessageBox.Show = Windows.Forms.DialogResult.OK("Please enter a number.")

        End If
        Me.xNumberTextBox.Focus()

    End Sub
End Class


User is offlineProfile CardPM

Go to the top of the page

jayman9
post 2 Jul, 2008 - 06:30 PM
Post #11


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,320



Thanked 22 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


Judging by your naming convention, I would say that you are using the wrong control to get the value you are entering.

Is it your intention to get the number from a Label?
CODE

IF Integer.TryParse(Me.xTotalLabel.Text, totalOwed) Then


Would this be the actual TextBox where you are entering you numbers?
CODE
IF Integer.TryParse(Me.xNumberTextBox.Text, totalOwed) Then
User is online!Profile CardPM

Go to the top of the page

LadyWolf
post 3 Jul, 2008 - 08:52 AM
Post #12


D.I.C Head

**
Joined: 25 Jun, 2008
Posts: 122


My Contributions


I finally got it!! icon_up.gif Adam's KeyPress was close...Here how I got it:
CODE

          Handles xNumberTextBox.KeyPress
        'allows numbers, and the Backspace key

        If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
            AndAlso e.KeyChar <> ControlChars.Back Then
            e.Handled = True
            MessageBox.Show("Please enter a number")

        End If
    End Sub

End Class
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 9/5/08 05:40PM