text box saying instructions

Glen

Member
Joined
Oct 15, 2012
Messages
12
Programming Experience
Beginner
Im currently teaching myself to be familiar with this language
Right now im familiarizing some techniques and I am stuck at this dilemma.

Can anyone help me or suggest some tips? I am looking through the net but somehow I cant manage to find any tips so Im hoping that anyone here can help me...

As a practice, Im creating a simple program that can add and subtract two integer. But Im putting some user constraints.


My dilemmas are these, I want to have a text in the text box saying instructions to the user.
The other is I want to disable the two operating buttons which are ADD and SUBTRACT if there is no data input.

Like this, see how the text in the text boxes are not in regular color... So as the Add and Subtract buttons.
NOTE: I just edited this pic but this is how my app should be

1.jpeg


Then, when a data has been inserted, the text will be what the user had entered on the textbox and the
two operating buttons shall be enabled...

2.jpeg

Any Tips would help... Thanks...
 
Interestingly, IsNumeric calls Double.TryParse internally. In fact, it was to improve the performance of IsNumeric that the Double.TryParse method was created in the first place. It was so successful that many other TryParse methods were added later.

In this case, the OP did say that they wanted to add integers. That means that IsNumeric is not appropriate and Integer.TryParse should be used.

This is very interesting... can you give me a code on how to use TryParse sir?
 
This is very interesting... can you give me a code on how to use TryParse sir?
Is there any reason that you couldn't just Google that for yourself? We are more than happy to help with problems that you can't solve on your own but forums should not be a substitute for a simple web search.
 
Is there any reason that you couldn't just Google that for yourself? We are more than happy to help with problems that you can't solve on your own but forums should not be a substitute for a simple web search.

Try to research it but I coundnt find simple explanations.

This, I think, is the easiest way for a beginner to do this.

If IsNumeric(TextBox1.Text) Then
    'Textbox1 contains a numeric value
    Button1.Enabled = True

Else
    'Textbox1 does not contain a numeric value
    Button1.Enabled = False

End If


That code should be put in a sub that handles the Textchanged events of the textboxes.


Sidenote:
To write 'true' .Net-code, the TryParse method would probably be more correct to use...

tried this but even if you put a value on the textbox, it is still disabled, but the button is also enabled which is a progress... Thanks, I will research to improve the code...:D
 
TryParse returns a Boolean value. It takes two arguments. The first is a string to be converted; the second is the number of the specified type to convert into. If the conversion is successful, it will return the number. If not, it will return 0 (which is False). It can be used two ways. The first is to assign a number of the class type. For example:

VB.NET:
Dim intnum As Integer
Integer.TryParse(Textbox1.Text, intnum)
If intnum = 0 Then
   Button1.Enabled = False
Else
   Button1.Enabled = True
End If

In the above scenario, if the textbox contains the number 5, it will return that number. If it contains text, it will return 0. However, it will not differentiate between a string and an actual value of 0 if the textbox contains the numeral 0. You could instead have it return either True or False:

VB.NET:
Dim intnum As Integer
If Integer.TryParse(Textbox1.Text, intnum) = False Then
   Button1.Enabled = False
Else
   Button1.Enabled = True
End If

A Boolean value can be implied if it returns True, so you can reverse the If statements as follows:

VB.NET:
Dim intnum As Integer
If Integer.TryParse(Textbox1.Text, intnum) Then
   Button1.Enabled = True
Else
   Button1.Enabled = False
End If


Note: TryParse can work with any number type, not just Integer:

VB.NET:
Dim dblnum As Double
Dim mystring As String = "52.8"
Double.TryParse(mystring, dblnum)
 
Last edited:
You can simply validate the field if it is for numeric input only without checking whether it is an number or not. and then include to your IF condition that if text box is null, button's enable property will set back to false.



 
Back
Top