Results 1 to 17 of 17

Thread: [RESOLVED] question in textbox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    246

    Resolved [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

  2. #2
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: question in textbox

    What you are looking for is something on the keypress event...put this in your code:

    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2. If InStr(Chr(KeyAscii), "1234567890+") = 0 Then KeyAscii = 0
    3. 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.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    246

    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 "+"

  4. #4
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: question in textbox

    Textboxes have a MaxLength property.
    VB Code:
    1. TextBox.MaxLength = 5
    You could do something like this:
    VB Code:
    1. Private Sub Form_Load()
    2. Text1.MaxLength = 4
    3. End Sub
    4.  
    5. Private Sub Text1_KeyPress(KeyAscii As Integer)
    6. If (KeyAscii = 13) Then ' if enter is pressed
    7. Text1.MaxLength = 5
    8. Text1.SelStart = Len(Text1.Text)
    9. Text1.SelText = "+"
    10. Text1.Locked = True
    11. End If
    12. End Sub
    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    246

    Re: question in textbox

    the maxlength must not be fix
    the user will be the one who will input the maxlength...

  6. #6
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: question in textbox

    So setup another textbox, do it like this:
    VB Code:
    1. TextBox.MaxLength = CLng(txtBoxMaxLength.Text)
    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    246

    Re: question in textbox

    excuse me again, is there another code for this?

  8. #8
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: question in textbox

    Another code? How do you mean?

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    246

    Re: question in textbox

    excuse me, can I loop this statement? like this:
    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2. For x = 0 To 10
    3. If (KeyAscii = 43) Then
    4. Text1.MaxLength = Val(Text2.Text) + 1
    5. End If
    6. Next x
    7. End Sub

  10. #10
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: question in textbox

    rather than a loop, try using instr:
    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2. If InStr(text1,"+") <> 0 Then Text1.MaxLength = Val(Text2.Text) + 1 else Text1.MaxLength = Val(Text2.Text)
    3. 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.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    246

    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?

  12. #12
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: question in textbox

    How many times would you be adding a "+"? If more than once, try this:
    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2. tx = Text1 + Chr(KeyAscii)
    3. tx = Replace(tx, "+", "")
    4. Text1.MaxLength = Val(text2) + (Len(Text1) - Len(tx)) +1
    5. 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.

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    246

    Re: question in textbox

    when I input 2 in text2, in only accepts 1 digit

  14. #14
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    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.

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    246

    Re: question in textbox

    waaa...another question, how can I compare if there are 2 or more "+" like:
    1342+++
    and must be an error

  16. #16
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: question in textbox

    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2. tx = Text1 + Chr(KeyAscii)
    3. tx = Replace(tx, "+", "")
    4. totplus = (Len(Text1) - Len(tx)) +1
    5. if totplus >1 then 'ERROR CODE HERE!
    6. Text1.MaxLength = Val(text2) + totplus
    7. 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.

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    246

    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
  •  



Click Here to Expand Forum to Full Width