Textboxes

ravoll

Well-known member
Joined
Aug 26, 2010
Messages
50
Programming Experience
Beginner
Hi All.
First an explaination of my plans.
I have 6 textboxes set to calculate final drive ratios.Textbox 1 is enabled at form load,all others disabled.
What I want is for the user to enter his number of gears,from 1 to 5,into Textbox1 to enable only the text boxes he will need
for the calculations.
For example If he only has a 3 speed ,then 3 will be entered into Textbox1 and only textboxes 2,3, and 4 will be enabled.

I have it this far,but when I clear textbox 1 the textboxes enabled don't disable, and throws an error."A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll"

Code example provided is for 1 gear for simplicity.

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text = 1 Then
TextBox2.Enabled = True
Else : TextBox2.Enabled = False
End If
End Sub

What am I doing wrong?
 
You should be using a loop. Put all the TextBoxes other than the first into an array and then use a For loop to go through the array. If the loop counter is less than the number entered by the user then the TextBox at that index is enable, otherwise it is disabled.
 
Also, you can't simply compare the Text of the first TextBox to a number when it may not be a number itself. You have to validate the text first to make sure it is a number and, if it's not, then don't treat it as one.
 
You should be using a loop. Put all the TextBoxes other than the first into an array and then use a For loop to go through the array. If the loop counter is less than the number entered by the user then the TextBox at that index is enable, otherwise it is disabled.

Sounds easy when I read it,but I have no Idea what your talking about.Sorry my VB handbook doesn't go into depth on the subject of loops.
 
Also, you can't simply compare the Text of the first TextBox to a number when it may not be a number itself. You have to validate the text first to make sure it is a number and, if it's not, then don't treat it as one.
Like this?

' Class to override TextBox Control:
Public Class ConorodTextBox
Inherits TextBox

' List of possible choices
Public Enum InputType
All = 0
NumeralsOnly = 1
LettersOnly = 2
NumeralsAndCurrency = 3
NoSymbols = 4
End Enum

Private _allowedcharacters As InputType

Property AllowedCharacters() As InputType
Get
Return _allowedcharacters
End Get
Set(ByVal Value As InputType)
Select Case Value
Case 1 To 5 ' User choice
_allowedcharacters = Value
Case Else ' Default of All
_allowedcharacters = 0
End Select
End Set
End Property

Protected Overrides Sub OnKeyPress(ByVal e As KeyPressEventArgs)
Dim KeyAscii As Short

MyBase.OnKeyPress(e)

'Convert the KeyChar argument to an Ascii value
KeyAscii = Asc(e.KeyChar)
'Decide what to do based on the AllowedCharacters property
Select Case _allowedcharacters
Case 1 'Numerals only
If KeyAscii >= 48 And KeyAscii <= 57 Then
Exit Sub
Else
e.Handled = True
Beep()
End If

' ****
' Other Cases here
' *****

End Select

End Sub
End Class
 
If you don't know how to write a loop then no, not like that. It's rather difficult to answer questions when people ask us how to do something but give us no idea what restrictions there may be on what they can use. Loops are one of the elementary building blocks of any language. If you're not up to that yet then I don't think you're up to filtering input in a TextBox.

Noone would ever do it that way in a real app but if you can't write a loop then I guess you'll have write multiple If statements. As for testing the input, does your assignment state that you can assume that the input will always be valid? If not then you might consider IsNumeric, althougn that still doesn't guarantee a whole number. Integer.TryParse would be the best option.
 
If you don't know how to write a loop then no, not like that. It's rather difficult to answer questions when people ask us how to do something but give us no idea what restrictions there may be on what they can use. Loops are one of the elementary building blocks of any language. If you're not up to that yet then I don't think you're up to filtering input in a TextBox.

Noone would ever do it that way in a real app but if you can't write a loop then I guess you'll have write multiple If statements. As for testing the input, does your assignment state that you can assume that the input will always be valid? If not then you might consider IsNumeric, althougn that still doesn't guarantee a whole number. Integer.TryParse would be the best option.


Yes.I did it now with multiple "If" statements.And "if not numeric" statements to keep the alphabeticals out.

Have it now so when the digits 1 thru 5 are keyed into textbox1, the appropriate textboxes are activated. If the digits 6 thru 9 are entered into Textbox1,
boxes remain deactivated.
The problem now is
1) Keeping the boxes off when double digits such as 10,11,12...ect are logged. And 0 (zero) for some reason activates all textboxes.

2)Next.If I enter 1 thru 5 in the textbox1,the appropriate boxes are activated. When I clear the text in Textbox1 the boxes are deactivated, but if I have entered any numbers into an activated textbox, and clear Textbox1,the numbers remainin the deactivated box(es).

Here's a look at my neanderthal code:

VB.NET:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
   
    


    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

        If Not IsNumeric(TextBox1.Text) Or TextBox1.Text = "" Then
            TextBox1.Text = "" 'prevents input other the number' 
            TextBox2.Enabled = False ' No input, disable button...' 
            TextBox3.Enabled = False
            TextBox4.Enabled = False
            TextBox5.Enabled = False
            TextBox6.Enabled = False
            TextBox7.Enabled = False
        End If
        If TextBox1.Text = "1" Then
            TextBox2.Enabled = True
            TextBox3.Enabled = True
        End If
        If TextBox1.Text = "2" Then
            TextBox2.Enabled = True
            TextBox3.Enabled = True
            TextBox4.Enabled = True
        End If
        If TextBox1.Text = "3" Then
            TextBox2.Enabled = True
            TextBox3.Enabled = True
            TextBox4.Enabled = True
            TextBox5.Enabled = True
        End If
        If TextBox1.Text = "4" Then
            TextBox2.Enabled = True
            TextBox3.Enabled = True
            TextBox4.Enabled = True
            TextBox5.Enabled = True
            TextBox6.Enabled = True

        End If
        If TextBox1.Text = "5" Then
            TextBox2.Enabled = True
            TextBox3.Enabled = True
            TextBox4.Enabled = True
            TextBox5.Enabled = True
            TextBox6.Enabled = True
            TextBox7.Enabled = True
        End If
        If TextBox1.Text > "5" Then
            TextBox2.Enabled = False
            TextBox3.Enabled = False
            TextBox4.Enabled = False
            TextBox5.Enabled = False
            TextBox6.Enabled = False
            TextBox7.Enabled = False

        End If
       

    End Sub

   
    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
        If Not IsNumeric(TextBox2.Text) Or TextBox2.Text = "" Then
            TextBox2.Text = "" 'prevents input other the number' 
        End If
        

    End Sub

    Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
        If Not IsNumeric(TextBox3.Text) Or TextBox3.Text = "" Then
            TextBox3.Text = "" 'prevents input other the number' 
        End If
    End Sub

    Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged
        If Not IsNumeric(TextBox4.Text) Or TextBox4.Text = "" Then
            TextBox4.Text = "" 'prevents input other the number' 
        End If
    End Sub

    Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged
        If Not IsNumeric(TextBox5.Text) Or TextBox5.Text = "" Then
            TextBox5.Text = "" 'prevents input other the number' 
        End If
    End Sub

    Private Sub TextBox6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox6.TextChanged
        If Not IsNumeric(TextBox6.Text) Or TextBox6.Text = "" Then
            TextBox6.Text = "" 'prevents input other the number' 
        End If
    End Sub

    Private Sub TextBox7_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox7.TextChanged
        If Not IsNumeric(TextBox7.Text) Or TextBox7.Text = "" Then
            TextBox7.Text = "" 'prevents input other the number' 
        End If
    End Sub

    
End Class
 
Got the clearing problems weeded out.
If TextBox1.Text = "" Then
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()
TextBox6.Clear()
TextBox7.Clear()
End If
Just need to keep double digits and zero's entered into Textbox1 from activating any others.
 
How about you actually convert the input to a number? You can then test whether that number is greater than the maximum number of TextBoxes or less than the minimum and display an error message. From there you know that the input is valid. Also, you don;t have to keep repeating yourself over and over. You simply test whether the input is greater than zero and, if so, you enable TextBox1. That saves you having to write code to enable TextBox1 over and over. You then test whether the inout is greater than 1 and, if so, you enable TextBox2 and so on.
 
How about you actually convert the input to a number? You can then test whether that number is greater than the maximum number of TextBoxes or less than the minimum and display an error message. From there you know that the input is valid. Also, you don;t have to keep repeating yourself over and over. You simply test whether the input is greater than zero and, if so, you enable TextBox1. That saves you having to write code to enable TextBox1 over and over. You then test whether the inout is greater than 1 and, if so, you enable TextBox2 and so on.


Just forget it.
 
I would strongly suggest using a NumericUpDown control instead of a textbox for the user input. You can set the minimum to 1 (or 0 to see if the selection was never made) and the maximum to 5. That way, you don't have to worry about incorrect user input or the need to convert a string into a number. After the correct value has been selected by the user, you can have a button clicked to enable the textboxes.
 
I would strongly suggest using a NumericUpDown control instead of a textbox for the user input. You can set the minimum to 1 (or 0 to see if the selection was never made) and the maximum to 5. That way, you don't have to worry about incorrect user input or the need to convert a string into a number. After the correct value has been selected by the user, you can have a button clicked to enable the textboxes.

I have it .I used check boxes to activate the boxes needed.
I'm only playing around with this VB.I am not ,nor do strive to be a programmer.It's very aggrevating when a simple question is answered with complicated solutions.What is simple to those who deal with VB daily are not to those who don't.So please refrain.

What I am attempting is to write a simple mathmatical programm to figure wheel torque via engine torque and final drive ratios for each gear in a transmission.
And maybe have a little fun with VB while I'm at it.

I have run into my next delema.

First calculations are for a 2 speed transmission.
I have three textboxes,for 1st,2nd gears ,and the rearend ratio, and Button1 for calculating.I need to enable Button1 only after data has been entered into all 3 textboxes.
What I have now is the button is enabled either with input to txtbx1 or txtbox2,but doesn't wait for both.The "Button1.Enabled" needs to wait until all textboxes have data.Have tried all kinds of things with always the same result.
And please I don't need to hear about all the things I can't do to accomplish this.I'm only interested in what I can do


VB.NET:
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If Not IsNumeric(TextBox1.Text) Or TextBox1.Text = "" Then
            TextBox1.Text = "" 'prevents input other the number
            Button1.Enabled = False
        Else
            Button1.Enabled = True
        End If
        End Sub
      Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
            If Not IsNumeric(TextBox2.Text) Or TextBox2.Text = "" Then
                TextBox2.Text = "" 'prevents input other the number' 
            Button1.Enabled = False
        Else
            Button1.Enabled = True

        End If
 
I have it .I used check boxes to activate the boxes needed.
I'm only playing around with this VB.I am not ,nor do strive to be a programmer.It's very aggrevating when a simple question is answered with complicated solutions.What is simple to those who deal with VB daily are not to those who don't.So please refrain.

What I am attempting is to write a simple mathmatical programm to figure wheel torque via engine torque and final drive ratios for each gear in a transmission.
And maybe have a little fun with VB while I'm at it.

I have run into my next delema.

First calculations are for a 2 speed transmission.
I have three textboxes,for 1st,2nd gears ,and the rearend ratio, and Button1 for calculating.I need to enable Button1 only after data has been entered into all 3 textboxes.
What I have now is the button is enabled either with input to txtbx1 or txtbox2,but doesn't wait for both.The "Button1.Enabled" needs to wait until all textboxes have data.Have tried all kinds of things with always the same result.
And please I don't need to hear about all the things I can't do to accomplish this.I'm only interested in what I can do


VB.NET:
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If Not IsNumeric(TextBox1.Text) Or TextBox1.Text = "" Then
            TextBox1.Text = "" 'prevents input other the number
            Button1.Enabled = False
        Else
            Button1.Enabled = True
        End If
        End Sub
      Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
            If Not IsNumeric(TextBox2.Text) Or TextBox2.Text = "" Then
                TextBox2.Text = "" 'prevents input other the number' 
            Button1.Enabled = False
        Else
            Button1.Enabled = True

        End If

Never mind thread can be closed.I've got it.I created a form for each transmission type.Choose trans type on form 1 .On trans type form I just did
VB.NET:
If Not IsNumeric(TextBox1.Text) Or TextBox1.Text = "" Then
            TextBox1.Text = ""
            TextBox2.Enabled = False
        Else
            TextBox2.Enabled = True
        End If

and so on.Each textbox must have data before the next is enabled.The last Textbox enables the calculation button.Just that simple
 
ravoll, what jmc was trying to get at was that you should be converting the numeric input from the user into numeric values instead of keeping them as strings (text), you can do that using the .TryParse() method of the data type that you need. For example, if the user is to enter an integer number into the textbox, you would use Integer.TryParse() to try and convert the value into an integer variable for your program to do math on it.
'The integer variable to hold the value
Dim GearNumberInt As Integer

If Integer.TryParse(GearNumberTextBox.Text.Trim, GearNumberInt) Then
    'Was successful, use GearNumberInt in the rest of this sub's code
Else
    'Was not successful, should let the user know somehow
End If


As for Enabling/Disabling a button based on something being entered you can use 1 event sub for the three textboxes' TextChanged event and simply check all three:
Private Sub TextBox_TextChanged (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged
    TheButton.Enabled = TextBox1.Text.Trim.Length > 0 AndAlso TextBox2.Text.Trim.Length > 0 AndAlso TextBox3.Text.Trim.Length > 0
End Sub
That will mean that the button is not enabled if one of them doesn't have anything typed in. Also I am trimming the leading and trailing spaces in that check too, that will keep the button disabled if a textbox only contains space(s) which you may or may not want.

But one question I do have is, if you're not a programmer and are not wanting to be a programmer, why are you trying to write a program in the first place?
 
ravoll:

You are attempting to climb a mountain when you haven't yet learned how to climb a molehill. You are going round and round instead of steadily to the top.

The three very basic concepts of all programming languages are:
Sequence,
Selection (If-Then and Select Case), and
Repetition (Do While/Until and For loops).

You must master those basics first, using very simple apps, before attempting anything else. It doesn't matter if you are a hobbiest or a professional programmer. If you don't know those basics, then you are putting yourself at a distinct disadvantage.

Also, you should put Option Strict On at the very top of your program code. This will force explicit type conversion and avoid many subtle errors.
 

Latest posts

Back
Top