-
Mar 18th, 2017, 09:27 AM
#1
Thread Starter
Member
should be simple.. how to make a variable equa a lnumeric value typed into a textbox?
Not used VB6 for a while, and now I just can't remember how to do this...
I have a textbox on form1, I want to take a user inputted value (3 digits usually), and make a 'double' variable take it's value, using 'Val'. But I've fallen at the first hurdle... I've forgotten how to make a variable take a value inputted to a text box by a user.
What is the syntax for this?
The textbox appears as 'Textbox2' on the form while I'm writing code.
The sub is called 'Private Sub pictureHeight_Change()'
If IsNumeric(TextBox2.Text) Then picinput$ = TextBox2.Text
PicHeight_Px = Val(picinput$)
was my best effort
Thanks.
Last edited by moorea21; Mar 18th, 2017 at 09:32 AM.
-
Mar 18th, 2017, 09:58 AM
#2
Re: should be simple.. how to make a variable equa a lnumeric value typed into a text
Do you need to save the value in picinput$ for another purpose?
You can just do a Val on the TextBox text, although once you've verified it is numeric, then using CDbl is technically more efficient.
Code:
If IsNumeric(TextBox2.Text) Then
PicHeight_Px = Val(TextBox2.Text)
End If
Last edited by passel; Mar 18th, 2017 at 10:04 AM.
-
Mar 18th, 2017, 10:05 AM
#3
Re: should be simple.. how to make a variable equa a lnumeric value typed into a text
Not only more efficient, CDbl() is locale-aware unlike the crusty old Val() function.
And there isn't much point to your temporary picinput$ the way you are using it.
-
Mar 18th, 2017, 10:06 AM
#4
Thread Starter
Member
Re: should be simple.. how to make a variable equa a lnumeric value typed into a text
It seems I had to substitute pictureHeight.Text for TextBox2.Text...
If IsNumeric(pictureHeight.Text) Then
PicHeight_Px = Val(pictureHeight.Text)
End If
Only trouble is, as soon as I type just 1 digit into the textbox, it immediately starts running this sub.
How do I make sure it only does this when the user has finished typing in the new value into the textbox?
Thanks
-
Mar 18th, 2017, 11:30 AM
#5
Re: should be simple.. how to make a variable equa a lnumeric value typed into a text
Well, how do you know WHEN the user is 'done'? You said that 'usually' it is 3 digits, which means to me it could be 1, 2, 4 or more as well. One way to do that is when the user 'finishes', he can hit the enter key (you can figure that one out, I am sure). Or have a cmd button next to the textbox and the user would click it when 'done' entering whatever number of digits he wants.
-
Mar 18th, 2017, 11:38 AM
#6
Re: should be simple.. how to make a variable equa a lnumeric value typed into a text
Just a personal preference.
When I develop forms that require user input via textboxes, I generally create a separate procedure that validates all the textbox values are valid and filled in, as needed. In other words, unless absolutely necessary, I don't validate textbox input as the input occurs or when the textbox loses focus. This validation routine would be called when the user selects an action that uses the input. If validation fails, then the user is informed via a msgbox that required information is missing or invalid.
-
Mar 18th, 2017, 11:45 AM
#7
Thread Starter
Member
Re: should be simple.. how to make a variable equa a lnumeric value typed into a text
I had considered using an 'ok' or 'enter' button, not sure whether to or not yet. Validation will be something I'll add when I've got my head around the basic idea of adding the values. The textbox 'losing focus' seems like a good way of signalling the end of user input, and there's another button that has to be pressed immediately afterwards anyway, in most cases.
-
Mar 18th, 2017, 12:05 PM
#8
Re: should be simple.. how to make a variable equa a lnumeric value typed into a text
What do you mean by:
when I've got my head around the basic idea of adding the values.
Still need help with that...and what are you adding to what?
-
Mar 18th, 2017, 01:01 PM
#9
Thread Starter
Member
Re: should be simple.. how to make a variable equa a lnumeric value typed into a text
I meant user behaviour when adding values to this particular text box.
The fact that the bit of code I initially had trouble with gets called whenever any digit is changed in the text box turns out not to matter, so I left it as per Passel's example, and added a dummy 'ok' button next to it, which just makes the user feel more certain that the program has taken notice of them! A message box that pops up whenever a non numerical value is entered takes care of the odd typo.
-
Mar 18th, 2017, 01:10 PM
#10
Re: should be simple.. how to make a variable equa a lnumeric value typed into a text
Note that if numerical input is the main reason for this thread, you may want to search the forum for similar solutions. There are several ways to ensure user can only enter numerical data in a textbox including during input, post-input, using masked edit control, and using APIs.
Key search terms: textbox numbers only
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|