Handling empty fields

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello. I am using Visual Studio .NET (Academic Edition) to write a VB
program. My computer is running Win XP Pro.

I am writing a calculator and requires users to enter two numbers. After
entering the numbers, they should click a button called 'Display Results'.
How can I display a dialog box prompting the users to check their entry if
they left any of the boxes empty? I have tried the .IsNaN function but in
vein.

Thanks.
 
Hi,

check if isnumeric(textbox.text)
or for the length you could check textbox.text.length > 0
or combine them

hth
Peter
 
Xero,

To show the result from Peters actions when not
messagebox.show("You should enter something")

I hope this helps?

Cor
 
I see. Thanks very much.

Peter Proost said:
Hi,

check if isnumeric(textbox.text)
or for the length you could check textbox.text.length > 0
or combine them

hth
Peter
 
Eh ... sorry, I still have some problem ...
I copied the line of code but it didn't work out.

Could you post the exact line of code? I want to display the message 'One of
the boxes is empty.' if any of the two boxes named as 'num1' and 'num2' is
left empty. Thanks again.
 
Here are 2 possible pieces of code, place it behind the click event of a
button, or where you want to check the textboxes

<<<<<<<code 1>>>>>>

If Not IsNumeric(num1.Text) Or Not IsNumeric(num2.Text) Then
MsgBox("one of the boxes is empty")
End If

<<<<<<code 1>>>>>>

<<<<<<code 2>>>>>>

Dim blnOk As Boolean
If Not IsNumeric(num1.Text) Then
blnOk = False
MsgBox("Box one is empty")
Else
blnOk = True
End If
If Not IsNumeric(num2.Text) Then
blnOk = False
MsgBox("Box two is empty")
Else
blnOk = True
End If

If blnOk Then
'execute the rest of the code
MsgBox("They're both filled")
End If

<<<<<<code 2>>>>>>>>

greetz Peter
 
Sorry but they don't work either.
The code 'num1.Text' is underlined by a blue line, saying that 'Text is not
a member of Double.'
Sorry that I forgot to tell you that both num1 and num2 are of data type
'Double'.
 
is it a windows forms application?

grtz Peter


Xero said:
Sorry but they don't work either.
The code 'num1.Text' is underlined by a blue line, saying that 'Text is not
a member of Double.'
Sorry that I forgot to tell you that both num1 and num2 are of data type
'Double'.
 
when do you place the entered values in the double variables?

I propbably wont be able to reply anymore untill tomorrow

grtz peter
 
Perhaps I should post the lines of code of the event handlier the 'Display
Result' button. Thanks again.

'*******************************
'Begin of code

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim num1 As Double
Dim num2 As Double
num1 = TextBox1.Text
num2 = TextBox2.Text
If reverse.Checked = False Then
If s.Checked Then
MsgBox("The sum of the numbers " & num1 & " and " & num2 & "
is " & num1 + num2 & ".", MsgBoxStyle.Information)
End If
If d.Checked Then
MsgBox("The difference of the numbers " & num1 & " and " &
num2 & " is " & num1 - num2 & ".", MsgBoxStyle.Information)
End If
If m.Checked Then
MsgBox("The multiple of the numbers " & num1 & " and " &
num2 & " is " & num1 * num2 & ".", MsgBoxStyle.Information)
End If
If q.Checked Then
MsgBox("The quotient of the numbers " & num1 & " and " &
num2 & " is " & num1 / num2 & ".", MsgBoxStyle.Information)
End If
End If
If reverse.Checked = True Then
If s.Checked Then
MsgBox("The sum of the numbers " & num1 & " and " & num2 & "
is " & num2 + num1 & ".", MsgBoxStyle.Information)
End If
If d.Checked Then
MsgBox("The difference of the numbers " & num1 & " and " &
num2 & " is " & num2 - num1 & ".", MsgBoxStyle.Information)
End If
If m.Checked Then
MsgBox("The multiple of the numbers " & num1 & " and " &
num2 & " is " & num2 * num1 & ".", MsgBoxStyle.Information)
End If
If q.Checked Then
MsgBox("The quotient of the numbers " & num1 & " and " &
num2 & " is " & num2 / num1 & ".", MsgBoxStyle.Information)
End If
End If
If (s.Checked = False) And (d.Checked = False) And (m.Checked =
False) And (q.Checked = False) Then
MsgBox("No operation key has been selected. Please try again.",
MsgBoxStyle.Exclamation, "Calculator")
End If
End Sub

'End of code
'*******************************
 
Hi, this code should do the trick

<<<<<<<<<<code>>>>>>>>>>>>

Dim num1 As Double
Dim num2 As Double
Dim blnOk As Boolean = True
If Not IsNumeric(TextBox1.Text) Then
blnOk = False
MsgBox("Box one is empty")
Else
num1 = TextBox1.Text
blnOk = True
End If
If Not IsNumeric(TextBox2.Text) Then
blnOk = False
MsgBox("Box two is empty")
Else
num2 = TextBox2.Text
blnOk = True
End If

If blnOk Then
If reverse.Checked = False Then
If s.Checked Then
MsgBox("The sum of the numbers " & num1 & " and " & num2
& " is " & num1 + num2 & ".", MsgBoxStyle.Information)
End If
If d.Checked Then
MsgBox("The difference of the numbers " & num1 & " and "
& num2 & " is " & num1 - num2 & ".", MsgBoxStyle.Information)
End If
If m.Checked Then
MsgBox("The multiple of the numbers " & num1 & " and " &
num2 & " is " & num1 * num2 & ".", MsgBoxStyle.Information)
End If
If q.Checked Then
MsgBox("The quotient of the numbers " & num1 & " and " &
num2 & " is " & num1 / num2 & ".", MsgBoxStyle.Information)
End If
End If
If reverse.Checked = True Then
If s.Checked Then
MsgBox("The sum of the numbers " & num1 & " and " & num2
& " is " & num2 + num1 & ".", MsgBoxStyle.Information)
End If
If d.Checked Then
MsgBox("The difference of the numbers " & num1 & " and "
& num2 & " is " & num2 - num1 & ".", MsgBoxStyle.Information)
End If
If m.Checked Then
MsgBox("The multiple of the numbers " & num1 & " and " &
num2 & " is " & num2 * num1 & ".", MsgBoxStyle.Information)
End If
If q.Checked Then
MsgBox("The quotient of the numbers " & num1 & " and " &
num2 & " is " & num2 / num1 & ".", MsgBoxStyle.Information)
End If
End If
If (s.Checked = False) And (d.Checked = False) And (m.Checked =
False) And (q.Checked = False) Then
MsgBox("No operation key has been selected. Please try
again.", MsgBoxStyle.Exclamation, "Calculator")
End If
End If

<<<<<<<<<code>>>>>>>>

greetz Peter
 
It won't work either ...
An error box containing the following message appeared:


An unhandled exception of type 'System.InvalidCastException' occurred in
microsoft.visualbasic.dll

Additional information: Cast from string "" to type 'Double' is not valid.
 
place num1 = cdbl(TextBox1.Text) and num2 = cdbl(TextBox2.Text) instead of
num1 = TextBox1.Text and num2 = TextBox2.Text, you probably have option
strict on that's what generates the error.

this piece of code I tested and works just fine:

<<<<<<code>>>>>>>
Dim num1 As Double
Dim num2 As Double
Dim blnOk As Boolean = True

If Not IsNumeric(TextBox1.Text) Then
blnOk = False
MsgBox("Box one is empty")
Else
num1 = CDbl(TextBox1.Text)
blnOk = True
End If

If Not IsNumeric(TextBox2.Text) Then
blnOk = False
MsgBox("Box two is empty")
Else
num2 = CDbl(TextBox2.Text)
blnOk = True
End If

<<<<<<code>>>>>>

greetz Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top