Const Message as String = "Please enter a number"

Having trouble with getting messagebox to work correctly

Page 1 of 1

13 Replies - 1330 Views - Last Post: 03 July 2008 - 09:15 PM Rate Topic: -----

#1 LadyWolf  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 25-June 08

Const Message as String = "Please enter a number"

Post icon  Posted 02 July 2008 - 12:07 AM

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...


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



Is This A Good Question/Topic? 0
  • +

Replies To: Const Message as String = "Please enter a number"

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2256
  • View blog
  • Posts: 9,444
  • Joined: 29-May 08

Re: Const Message as String = "Please enter a number"

Posted 02 July 2008 - 01:09 AM

I think the IF statement was incorrect and in the wrong place. Should it be like this.

View PostLadyWolf, on 2 Jul, 2008 - 08:07 AM, said:


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


Was This Post Helpful? 0
  • +
  • -

#3 LadyWolf  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 25-June 08

Re: Const Message as String = "Please enter a number"

Posted 02 July 2008 - 08:20 AM

View PostAdamSpeight2008, on 2 Jul, 2008 - 04:09 AM, said:

I think the IF statement was incorrect and in the wrong place. Should it be like this.

View PostLadyWolf, on 2 Jul, 2008 - 08:07 AM, said:


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...
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Const Message as String = "Please enter a number"

Posted 02 July 2008 - 08:22 AM

Moved to VB.NET :)
Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2256
  • View blog
  • Posts: 9,444
  • Joined: 29-May 08

Re: Const Message as String = "Please enter a number"

Posted 02 July 2008 - 08:38 AM

View PostLadyWolf, on 2 Jul, 2008 - 04:20 PM, said:

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
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


Was This Post Helpful? 0
  • +
  • -

#6 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Const Message as String = "Please enter a number"

Posted 02 July 2008 - 09:00 AM

I think this was your intention, you just need to utilize the Try.Parse in the IF statement.
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

Was This Post Helpful? 0
  • +
  • -

#7 LadyWolf  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 25-June 08

Re: Const Message as String = "Please enter a number"

Posted 02 July 2008 - 09:57 AM

View Postjayman9, on 2 Jul, 2008 - 12:00 PM, said:

I think this was your intention, you just need to utilize the Try.Parse in the IF statement.
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: 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.
Was This Post Helpful? 0
  • +
  • -

#8 LadyWolf  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 25-June 08

Re: Const Message as String = "Please enter a number"

Posted 02 July 2008 - 10:12 AM

View PostAdamSpeight2008, on 2 Jul, 2008 - 11:38 AM, said:

View PostLadyWolf, on 2 Jul, 2008 - 04:20 PM, said:

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
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.
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2256
  • View blog
  • Posts: 9,444
  • Joined: 29-May 08

Re: Const Message as String = "Please enter a number"

Posted 02 July 2008 - 10:51 AM

TKO in Round 2 0_0
Was This Post Helpful? 0
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2256
  • View blog
  • Posts: 9,444
  • Joined: 29-May 08

Re: Const Message as String = "Please enter a number"

Posted 02 July 2008 - 11:15 AM

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.

View PostLadyWolf, on 2 Jul, 2008 - 08:07 AM, said:

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


Was This Post Helpful? 0
  • +
  • -

#11 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Const Message as String = "Please enter a number"

Posted 02 July 2008 - 06:30 PM

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?
IF Integer.TryParse(Me.xTotalLabel.Text, totalOwed) Then



Would this be the actual TextBox where you are entering you numbers?
IF Integer.TryParse(Me.xNumberTextBox.Text, totalOwed) Then


Was This Post Helpful? 0
  • +
  • -

#12 LadyWolf  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 25-June 08

Re: Const Message as String = "Please enter a number"

Posted 03 July 2008 - 08:52 AM

I finally got it!! :^: Adam's KeyPress was close...Here how I got it:
		  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


Was This Post Helpful? 0
  • +
  • -

#13 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Const Message as String = "Please enter a number"

Posted 03 July 2008 - 01:07 PM

So your original issue was that you were trying to get the value from the wrong control. Glad you were able to arrive at a solution.
Was This Post Helpful? 0
  • +
  • -

#14 LadyWolf  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 25-June 08

Re: Const Message as String = "Please enter a number"

Posted 03 July 2008 - 09:15 PM

View Postjayman9, on 3 Jul, 2008 - 04:07 PM, said:

So your original issue was that you were trying to get the value from the wrong control. Glad you were able to arrive at a solution.


Yep...duh lol
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1