|
-
Sep 11th, 2006, 06:30 AM
#1
Thread Starter
Addicted Member
[RESOLVED] question in textbox
can you limit the numbers that the textbox will accept? but also you can
input a positive sign?
for example:
I have 2 textboxes
text1
text2
in text1 I input 4 then
text2 will accept 4 numbers: 9876 but you can also add a "+" to it
can it be possible?
thanks
-
Sep 11th, 2006, 06:45 AM
#2
PowerPoster
Re: question in textbox
What you are looking for is something on the keypress event...put this in your code:
VB Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If InStr(Chr(KeyAscii), "1234567890+") = 0 Then KeyAscii = 0
End Sub
change the "text1" to whatever you need, and change the "1234567890+" to whatever you need to *allow*
However, if you're talking about limiting the length, that's a different matter...still doable though. If this is so, tell me and I'll have a go :-)
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
-
Sep 12th, 2006, 04:12 AM
#3
Thread Starter
Addicted Member
Re: question in textbox
hello smux...
what I mean is, can I specify the number of digits that a textbox can accept?
but also you can add a "+"
-
Sep 12th, 2006, 04:18 AM
#4
Re: question in textbox
Textboxes have a MaxLength property.
You could do something like this:
VB Code:
Private Sub Form_Load()
Text1.MaxLength = 4
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii = 13) Then ' if enter is pressed
Text1.MaxLength = 5
Text1.SelStart = Len(Text1.Text)
Text1.SelText = "+"
Text1.Locked = True
End If
End Sub
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Sep 12th, 2006, 04:21 AM
#5
Thread Starter
Addicted Member
Re: question in textbox
the maxlength must not be fix
the user will be the one who will input the maxlength...
-
Sep 12th, 2006, 04:29 AM
#6
Re: question in textbox
So setup another textbox, do it like this:
VB Code:
TextBox.MaxLength = CLng(txtBoxMaxLength.Text)
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Sep 12th, 2006, 04:54 AM
#7
Thread Starter
Addicted Member
Re: question in textbox
excuse me again, is there another code for this?
-
Sep 12th, 2006, 05:28 AM
#8
Re: question in textbox
Another code? How do you mean?
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Sep 13th, 2006, 03:56 AM
#9
Thread Starter
Addicted Member
Re: question in textbox
excuse me, can I loop this statement? like this:
VB Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
For x = 0 To 10
If (KeyAscii = 43) Then
Text1.MaxLength = Val(Text2.Text) + 1
End If
Next x
End Sub
-
Sep 13th, 2006, 04:02 AM
#10
PowerPoster
Re: question in textbox
rather than a loop, try using instr:
VB Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If InStr(text1,"+") <> 0 Then Text1.MaxLength = Val(Text2.Text) + 1 else Text1.MaxLength = Val(Text2.Text)
End Sub
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
-
Sep 13th, 2006, 04:07 AM
#11
Thread Starter
Addicted Member
Re: question in textbox
It didn't work, what code can I use to add 1 to the maxlength whenever I input a positive sign?
-
Sep 13th, 2006, 04:15 AM
#12
PowerPoster
Re: question in textbox
How many times would you be adding a "+"? If more than once, try this:
VB Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
tx = Text1 + Chr(KeyAscii)
tx = Replace(tx, "+", "")
Text1.MaxLength = Val(text2) + (Len(Text1) - Len(tx)) +1
End Sub
This bit of code first adds the new character to a temporary string...then it removes all "+" and compares length to original then changes maxlength, adding the number of + to the value of text2 and setting maxlength with this
Edit: minor bugfix :-)
Last edited by smUX; Sep 13th, 2006 at 04:24 AM.
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
-
Sep 13th, 2006, 04:21 AM
#13
Thread Starter
Addicted Member
Re: question in textbox
when I input 2 in text2, in only accepts 1 digit
-
Sep 13th, 2006, 04:24 AM
#14
PowerPoster
Re: question in textbox
See above...fixed the bug :-)
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
-
Sep 13th, 2006, 04:29 AM
#15
Thread Starter
Addicted Member
Re: question in textbox
waaa...another question, how can I compare if there are 2 or more "+" like:
1342+++
and must be an error
-
Sep 13th, 2006, 04:35 AM
#16
PowerPoster
Re: question in textbox
VB Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
tx = Text1 + Chr(KeyAscii)
tx = Replace(tx, "+", "")
totplus = (Len(Text1) - Len(tx)) +1
if totplus >1 then 'ERROR CODE HERE!
Text1.MaxLength = Val(text2) + totplus
End Sub
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
-
Sep 13th, 2006, 04:55 AM
#17
Thread Starter
Addicted Member
Re: question in textbox
I'll just edit some errors. Thanks!
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
|