September 21, 2020, 01:50:46 pm
News: If someone gives you crap then give them a Bad Star
Pages: [1]   Go Down
Author Topic: Fixed Topic Name  (Read 1768 times)
Pc_Madness
Ancient Poster
*

Stars: +0/-0
Offline Offline

Posts: 5349



WWW
« on: November 27, 2002, 09:12:27 am »

Sorry... I had to post something in here.

Just wanted to check if this was alright.  I\'m trying to restrict the amount total characters(3), and stop non numerical characters from being entered..

[vbcode]
Private Sub cbSizes_KeyPress(KeyAscii As Integer)
If Len(cbSizes.Text) ] 2 then
KeyAscii = 0
exit sub
end if

If KeyAscii [] 8 then
If IsNumeric(I'm probably trying to hack the board softwareKeyascii)) = True then
If Keyascii = 13 Then
cbSizes_Click
KeyAscii = 0
End if
Else
KeyAscii = 0
End if
End If

End Sub
[/vbcode]

Thanks in Advance.
Logged


Slowhand
Supporters
*

Stars: +0/-0
Offline Offline

Gender: Male
Awards:
Topic Necromancer
Posts: 4668


I code C#!


WWW
« Reply #1 on: November 27, 2002, 10:27:41 am »

I think the second part won\'t work.
First you check if the character is numeric, and if that\'s true you check if it\'s the Enter button. But the Enter button isn\'t numeric so that second statement will never be true! (I think)
Logged


peet
Knight of The Most Excellent Order of the Ancient Poster
*

Stars: +0/-0
Offline Offline

Posts: 8732



WWW
« Reply #2 on: November 27, 2002, 10:34:03 am »

you will also have to check if the user paste something into the textbox...

always do a final check using the validate event.
Logged

peet
Knight of The Most Excellent Order of the Ancient Poster
*

Stars: +0/-0
Offline Offline

Posts: 8732



WWW
« Reply #3 on: November 27, 2002, 10:35:19 am »

I would set the textbox max length property to 3
Add code for allowing only numeric input
and finally check the content using the validate event...
Logged

Slowhand
Supporters
*

Stars: +0/-0
Offline Offline

Gender: Male
Awards:
Topic Necromancer
Posts: 4668


I code C#!


WWW
« Reply #4 on: November 27, 2002, 10:49:59 am »

I would write it like this:


[vbcode]
Private Sub cbSizes_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
    cbSizes_Click
ElseIf KeyAscii [] 8 Then
    If IsNumeric(I'm probably trying to hack the board softwareKeyascii)) = False Then
         KeyAscii = 0
         Beep
    End If
End If
[/vbcode]
Logged


Slowhand
Supporters
*

Stars: +0/-0
Offline Offline

Gender: Male
Awards:
Topic Necromancer
Posts: 4668


I code C#!


WWW
« Reply #5 on: November 27, 2002, 10:51:11 am »

When I look at my code above, I suddenly start to think something is wrong, just can\'t find it out.....
Logged


Martin Wallace
Founders
*

Stars: +1/-0
Offline Offline

Posts: 3602


God is dead, I have surpassed him.


WWW
« Reply #6 on: November 27, 2002, 11:01:00 am »

I use the following bits of code to ensure valid numeric entry in a text box.

[vbcode]Private strTextOnFocus  As String

Private Sub txtFinanceAnnual_GotFocus()
    strTextOnFocus = txtFinanceAnnual.Text
    SafeSetFocus txtFinanceAnnual, True
End Sub

Private Sub txtFinanceAnnual_KeyPress(KeyAscii As Integer)
    RestrictTextNumeric txtFinanceAnnual, KeyAscii
End Sub

Private Sub txtFinanceAnnual_Validate(Cancel As Boolean)
    If Not IsNumeric(txtFinanceAnnual.Text) And txtFinanceAnnual.Text [] \"\" Then
        Beep
        Cancel = True
    End If
End Sub


Private Sub RestrictTextNumeric(txtBox As TextBox, KeyAscii As Integer)
   
    Select Case KeyAscii
        Case vbKeyEscape
            txtBox.Text = strTextOnFocus
        Case vbKey0 To vbKey9
            \'ok
        Case vbKeyDelete
            \'ok
        Case vbKeyBack
            \'ok
        Case Asc(\".\")
            \'ok
        Case 24 \'Cut
            \'ok
        Case 3  \'Copy
            \'ok
        Case 22 \'Paste
            \'Only allow paste if the pasted values are numeric
            If Clipboard.GetFormat(vbCFText) Then
                If Not IsNumeric(Clipboard.GetText) Then
                    Beep
                    KeyAscii = 0
                End If
            End If
        Case Else
            Beep
            KeyAscii = 0
    End Select

End Sub

Private Sub SafeSetFocus(ByVal Ctl As Control, Optional blnTextSelected As Boolean = True)

    If Ctl.Enabled And Ctl.Visible Then
        Ctl.SetFocus
        If blnTextSelected Then
            Ctl.SelStart = 0
            Ctl.SelLength = Len(Ctl.Text)
        End If
    End If
   
End Sub[/vbcode]

This prevents the paste of non-numerics as well as allowing the user to use Del, backspace and enter decimals.

You may need to alter it for your usage but use it as you wish.
Logged

Every time god kills an angel, I masturbate.

Hack
Master of TMEOotAP
*

Stars: +0/-0
Offline Offline

Posts: 10349



WWW
« Reply #7 on: November 27, 2002, 08:33:45 pm »

[vbcode]Private Const GWL_STYLE = (-16)
Private Const ES_NUMBER = &H2000
Private Declare Function GetWindowLong Lib \"user32\" Alias \"GetWindowLongA\" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib \"user32\" Alias \"SetWindowLongA\" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Private Sub Form_Load()
Dim gretval As Long
Dim sretval As Long
gretval = GetWindowLong(Text1.hwnd, GWL_STYLE)
sretval = SetWindowLong(Text1.hwnd, GWL_STYLE, gretval Or ES_NUMBER)
End Sub

Private Sub Text1_Change()
If Not IsNumeric(Text1.Text) Then
   MsgBox \"Only Numbers Are Allowed\"
   Text1.Text = \"\"
   Exit Sub
End If
End Sub
[/vbcode]
Logged


welcome

Martin Wallace
Founders
*

Stars: +1/-0
Offline Offline

Posts: 3602


God is dead, I have surpassed him.


WWW
« Reply #8 on: November 27, 2002, 09:33:54 pm »

Nice one hack - thats neat and simple - gonna add that too my arsenal
Logged

Every time god kills an angel, I masturbate.

Slowhand
Supporters
*

Stars: +0/-0
Offline Offline

Gender: Male
Awards:
Topic Necromancer
Posts: 4668


I code C#!


WWW
« Reply #9 on: November 27, 2002, 10:41:24 pm »

 It\'s Hack!  

 

     
Logged


amitabh
Old Timer
*

Stars: +0/-0
Offline Offline

Posts: 2764


WWW
« Reply #10 on: November 28, 2002, 06:53:06 am »

But the only problem with Hack\'s code is that it won\'t allow you decimal values. And no backspaces too.
Logged

Pages: [1]   Go Up
Print
 
Jump to: