14 Replies - 576 Views - Last Post: 16 February 2012 - 11:33 AM Rate Topic: -----

#1 rantonio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-February 12

i cant run my program... because i declare my pq1.text as double!

Posted 13 February 2012 - 06:03 AM

Dim num1, num2, num3, num4, num5, total As Double

        num1 = pq1.Text
        num2 = pq2.Text
        num3 = labact1.Text
        num4 = labact2.Text
        num5 = preexam.Text
        pgrade.Text = total

   If pq1.Text > 100 Then
            pq1.Text = ""
            MsgBox("Invalid Input:", MsgBoxStyle.Critical, "Prelim Quiz1")
        Else
            If pq1.Text = "" Then
                MsgBox("Must Input A value!", MsgBoxStyle.Critical, "Prelim Quiz1")
            End If
        End If

my problem is it is not working if i leave the textbox blank what should i do..?

This post has been edited by smohd: 13 February 2012 - 06:29 AM
Reason for edit:: Code tags added. Please use [code] tags when posting codes


Is This A Good Question/Topic? 0
  • +

Replies To: i cant run my program... because i declare my pq1.text as double!

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: i cant run my program... because i declare my pq1.text as double!

Posted 13 February 2012 - 06:27 AM

You should use double.TryParse to check that the string entered in the textboxes can indeed be convert to a double.

Double.TryParse(pq1.Text, num1)


Now if the string entered can be converted, num1 will hold that value you entered, otherwise it will store a 0.

You can also use boolean logic to check. For example
If Double.TryParse(pq1.Text, num1) = False Then
  MessageBox.Show("Entry is not valid")
...


Also on this line:
if pq1.Text > 100 is probably your problem. The .Text property holds a string. "100" is not the same as 100

Try my example above and then check if num1 > 100 because it will be the numeric value you parsed from the textbox.

Also, read this entry in AdamSpeight2008's blog.

This post has been edited by CharlieMay: 13 February 2012 - 06:36 AM

Was This Post Helpful? 0
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: i cant run my program... because i declare my pq1.text as double!

Posted 13 February 2012 - 06:36 AM

Ok, the question has been answered by CharlieMay, but I add few things here:
        pgrade.Text = total 'assign value to text property
	 
	   If pq1.Text > 100 Then ' and then check if the value is >100
	            pq1.Text = ""
	            MsgBox("Invalid Input:", MsgBoxStyle.Critical, "Prelim Quiz1") 'if so tell user that he/she enter invalid input!!
	        Else

How you tell user that enter invalid input while you are the one who assign the value? did you mean the reverse of it?

Also if you need empty input to be treated as 0, then you can first check if input is empty, assign "0" to it and then you can parse your input.
Was This Post Helpful? 0
  • +
  • -

#4 rantonio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-February 12

Re: i cant run my program... because i declare my pq1.text as double!

Posted 13 February 2012 - 07:38 PM

[quote name='smohd' date='13 February 2012 - 06:36 AM' timestamp='1329140162' post='1550711']
Ok, the question has been answered by CharlieMay, but I add few things here:
        pgrade.Text = total 'assign value to text property
	 
	   If pq1.Text > 100 Then ' and then check if the value is >100
	            pq1.Text = ""
	            MsgBox("Invalid Input:", MsgBoxStyle.Critical, "Prelim Quiz1") 'if so tell user that he/she enter invalid input!!
	        Else

How you tell user that enter invalid input while you are the one who assign the value? did you mean the reverse of it?

Also if you need empty input to be treated as 0, then you can first check if input is empty, assign "0" to it and then you can parse your input.

i assigned value so if the user input greater than 100, msgbox will appear that the user input a invalid value this is a a grading system, so they cant input not exceeding to 100.. my problem is what if they leave the textbox empty and if they process it, there will be an error.. what will be the statement? for when the user leave a textbox empty msgbox will just appear telling them that they must input a value... thanks!

[quote name='CharlieMay' date='13 February 2012 - 06:27 AM' timestamp='1329139625' post='1550704']
You should use double.TryParse to check that the string entered in the textboxes can indeed be convert to a double.

Double.TryParse(pq1.Text, num1)


Now if the string entered can be converted, num1 will hold that value you entered, otherwise it will store a 0.

You can also use boolean logic to check. For example
If Double.TryParse(pq1.Text, num1) = False Then
  MessageBox.Show("Entry is not valid")
...


Also on this line:
if pq1.Text > 100 is probably your problem. The .Text property holds a string. "100" is not the same as 100

Try my example above and then check if num1 > 100 because it will be the numeric value you parsed from the textbox.

Also, read this entry in AdamSpeight2008's blog.


i assigned value so if the user input greater than 100, msgbox will appear that the user input a invalid value this is a a grading system, so they cant input not exceeding to 100.. my problem is what if they leave the textbox empty and if they process it, there will be an error.. what will be the statement? for when the user leave a textbox empty msgbox will just appear telling them that they must input a value... thanks!
Was This Post Helpful? 0
  • +
  • -

#5 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: i cant run my program... because i declare my pq1.text as double!

Posted 13 February 2012 - 07:47 PM

Again, tryparse will do this for you. If you want to check against double then use double.tryparse, also, just check against a blank string in case nothing was entered.

Read up on .TryParse and attempt to set up the validation checks. If you get stuck, post what you have so we can help you through it.

Don't forget you can use Or when checking against two different validations.
If This = value or This = "" then
  Display Message
End If

This post has been edited by CharlieMay: 13 February 2012 - 07:50 PM

Was This Post Helpful? 0
  • +
  • -

#6 rantonio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-February 12

Re: i cant run my program... because i declare my pq1.text as double!

Posted 14 February 2012 - 11:35 AM

[quote name='CharlieMay' date='13 February 2012 - 07:47 PM' timestamp='1329187660' post='1551375']
Again, tryparse will do this for you. If you want to check against double then use double.tryparse, also, just check against a blank string in case nothing was entered.

Read up on .TryParse and attempt to set up the validation checks. If you get stuck, post what you have so we can help you through it.

Don't forget you can use Or when checking against two different validations.
If This = value or This = "" then
  Display Message
End If


if i leave pq1.text blank.
'conversion from string to type Double is not valid'
i've try my best but i'm stuck with this one,,
-just a beginner sorry for nagging..

 Dim num1, num2, num3, num4, num5, total As Double
        
        num1 = pq1.Text 
        num2 = pq2.Text 
        num3 = labact1.Text
        num4 = labact2.Text
        num5 = preexam.Text
       pgrade.Text = total

        num1 = num1 * 0.15
        num2 = num2 * 0.15
        num3 = num3 * 0.15
        num4 = num4 * 0.15
        num5 = num5 * 0.4
        pgrade.Text = num1 + num2 + num3 + num4 + num5

       If pgrade.Text > 100 Then
            pgrade.Text = ""
            MsgBox("Invalid Input!", MsgBoxStyle.Critical, "Prelim Grade")
        End If


        If num1 > 100 Then
            MsgBox("Invalid Input:", MsgBoxStyle.Critical, "Prelim Quiz1")
        End If 

Was This Post Helpful? 0
  • +
  • -

#7 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 638
  • Joined: 03-February 10

Re: i cant run my program... because i declare my pq1.text as double!

Posted 14 February 2012 - 11:53 AM

CharlieMay and smohd gave you all the information you need to solve this problem, except thinking it through and/or giving you the 'codez'. Perhaps instead of just writing the code write some Psuedo code first.


Get the value from textbox pq1
Is the value valid
     Is the value blank
     Is the value numerical
     Is the value within the criteria (>= 0, <= 100)
     Parse the value
Do some calculation with the value.
And more pseudo code



Now go back to your method and see what you can take from what these people posted and do the validation.
Was This Post Helpful? 0
  • +
  • -

#8 rantonio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-February 12

Re: i cant run my program... because i declare my pq1.text as double!

Posted 14 February 2012 - 07:46 PM

View Postdemausdauth, on 14 February 2012 - 11:53 AM, said:

CharlieMay and smohd gave you all the information you need to solve this problem, except thinking it through and/or giving you the 'codez'. Perhaps instead of just writing the code write some Psuedo code first.


Get the value from textbox pq1
Is the value valid
     Is the value blank
     Is the value numerical
     Is the value within the criteria (>= 0, <= 100)
     Parse the value
Do some calculation with the value.
And more pseudo code



Now go back to your method and see what you can take from what these people posted and do the validation.


finally i got it guys.... thanks for helping me with this one!
im so stupid T_T because the code i need is just like this one..


        If pq1.Text = "" Then
            pq1.Text = 0
            MsgBox("You Must input a value!", MsgBoxStyle.Critical, "Prelim Quiz1")
        End If 

Was This Post Helpful? 0
  • +
  • -

#9 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: i cant run my program... because i declare my pq1.text as double!

Posted 15 February 2012 - 05:06 AM

OK, what is someone types an A in that textbox?

This post has been edited by CharlieMay: 15 February 2012 - 05:07 AM

Was This Post Helpful? 0
  • +
  • -

#10 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 205
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: i cant run my program... because i declare my pq1.text as double!

Posted 15 February 2012 - 07:04 AM

You're not paying attention to what CharlieMay has told you.

First, Use Double.TryParse to check whether the input of the Textbox can be converted to Double.
Then to solve your problem with the empty Textbox, using or in the same if statement, check if the Textbox is empty.
If one of these conditions are true, it'll pop up an error message.
And you have to use Exit Sub to to break out from the program flow if that happens or it'll just show the error message and continue with the execution.

If Not (checking with Double.TryParse) Or (Checking if the textbox is empty) Then
    MessageBox.Show("Enter a valid number")
    Exit Sub
End If

This post has been edited by nK0de: 16 February 2012 - 10:33 AM

Was This Post Helpful? 0
  • +
  • -

#11 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: i cant run my program... because i declare my pq1.text as double!

Posted 15 February 2012 - 07:19 AM

nK0de the TryParse would also handle the blank entry so no need for the Or. But I would use
If Not Double.TryParse(String, Result*) Then
  MessageBox.Show("Invalid Entry")
  Exit Sub
End If


*didn't want to fully reveal the actual solution

This post has been edited by CharlieMay: 15 February 2012 - 07:20 AM

Was This Post Helpful? 1
  • +
  • -

#12 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 205
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: i cant run my program... because i declare my pq1.text as double!

Posted 15 February 2012 - 07:28 AM

View PostCharlieMay, on 15 February 2012 - 07:49 PM, said:

nK0de the TryParse would also handle the blank entry so no need for the Or. But I would use
If Not Double.TryParse(String, Result*) Then
  MessageBox.Show("Invalid Entry")
  Exit Sub
End If


*didn't want to fully reveal the actual solution


you're right. I only checked it after I posted the reply.
Was This Post Helpful? 1
  • +
  • -

#13 rantonio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-February 12

Re: i cant run my program... because i declare my pq1.text as double!

Posted 16 February 2012 - 06:37 AM

View PostnK0de, on 15 February 2012 - 07:04 AM, said:

You're not paying attention to what CharlieMay has told you.

First, Use Double.TryParse to check whether the input of the Textbox can be converted to Double.
Then to solve your problem with the empty Textbox, using or in the same if statement, check if the Textbox is empty.
If one of these conditions are true, it'll pop up an error message.
And you have to use Exit Sub to to break out from the program flow if that happens or it'll just show the error message and continue with the execution.

If (checking with Double.TryParse) Or (Checking if the textbox is empty) Then
    MessageBox.Show("Enter a valid number")
    Exit Sub
End If



i use try
as Catch ex as Exception sir...
and even if you put a null value or a letter
msgbox will just appear telling that they have to input value or the value they input is invalid..
thanks for helping.. ^^,

This post has been edited by modi123_1: 16 February 2012 - 07:41 AM
Reason for edit:: removed odd quote tag

Was This Post Helpful? 0
  • +
  • -

#14 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 205
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: i cant run my program... because i declare my pq1.text as double!

Posted 16 February 2012 - 08:41 AM

View Postrantonio, on 16 February 2012 - 07:07 PM, said:

i use try
as Catch ex as Exception sir...
and even if you put a null value or a letter
msgbox will just appear telling that they have to input value or the value they input is invalid..
thanks for helping.. ^^,


you used Try Catch for that? That's wrong actually. Try Catch is used to trap errors thrown from the program. Not to do validations.

This post has been edited by nK0de: 16 February 2012 - 08:43 AM

Was This Post Helpful? 0
  • +
  • -

#15 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: i cant run my program... because i declare my pq1.text as double!

Posted 16 February 2012 - 11:33 AM

Crash course in error trapping! :D two kinds of errors: system thrown errors or the user defined errors you pick! :) try catch can be used just the same but you'll have to throw an error. ;) hope I said that right.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1