Ogilvy or someone else please advise me!!!, KeyDown event

H

halimnurikhwan

Hi Ogilvy and else,

I've made this :
Private Declare Function GetKeyState Lib "user32" _
(ByVal nVirtKey As Long) As Integer
Private Const VK_SHIFT = &H10

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _
ByVal Shift As Integer)

'if Shift key was pressed
If GetKeyState(VK_SHIFT) < 0 Then KeyCode = 0

'if there's a decimal or dot on the first then fill 0 before it
If Left(TextBox1.Value, 1) = "." Then _
TextBox1.Value = Application.Substitute(TextBox1.Value, ".",
"0.", 1)

'to pass if ESC, Enter, Delete, Arrow Down or up, "." (dot) was
pressed
If KeyCode = 13 Or KeyCode = 27 Or KeyCode = 40 Or KeyCode = 38 Or
_
KeyCode = 46 Or KeyCode = 8 Or Chr(KeyCode) Like "[0-9]" Or _
(KeyCode >= 96 And KeyCode <= 105) Or _
(KeyCode = 190 Or KeyCode = 110) And _
InStr(1, TextBox1.Value, ".") = 0 Then Exit Sub

If Chr(KeyCode) Like "[A-Z]" Or _
KeyCode = 32 Or (KeyCode >= 106 And KeyCode <= 221) _
Or KeyCode = 190 Then KeyCode = 0
End Sub

'then :
'change format of Textbox value after we fill it by (0 to 9)
Private Sub TextBox1_AfterUpdate()
If InStr(1, TextBox1.Value, ".") > 0 Then
TextBox1.Value = FormatNumber(TextBox1.Value,
Len(TextBox1.Value) - InStr(1, TextBox1.Value, "."))
Else
TextBox1.Value = FormatNumber(TextBox1.Value, 2)
End If
End Sub


by that code all I want to achieve is disabled keyboard press unless
the keyboard pressed was
0 to 9 ...

Is my code efficient enough ?, or do you can make it more simple ?!


Regards,

Halim
 
H

halimnurikhwan

HI SHAKA,

I use this code to input numeric only in a textbox and return the value
to nothing if
the value inputed is not zero to nine (0 to 9) or space and etc that I
don't need

try this in your userform and use a Textbox to do this !

That's work!
Regards,

Halim



(e-mail address removed) menuliskan:
Just woundering...what exactly does this code do?

Hi Ogilvy and else,

I've made this :
Private Declare Function GetKeyState Lib "user32" _
(ByVal nVirtKey As Long) As Integer
Private Const VK_SHIFT = &H10

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _
ByVal Shift As Integer)

'if Shift key was pressed
If GetKeyState(VK_SHIFT) < 0 Then KeyCode = 0

'if there's a decimal or dot on the first then fill 0 before it
If Left(TextBox1.Value, 1) = "." Then _
TextBox1.Value = Application.Substitute(TextBox1.Value, ".",
"0.", 1)

'to pass if ESC, Enter, Delete, Arrow Down or up, "." (dot) was
pressed
If KeyCode = 13 Or KeyCode = 27 Or KeyCode = 40 Or KeyCode = 38 Or
_
KeyCode = 46 Or KeyCode = 8 Or Chr(KeyCode) Like "[0-9]" Or _
(KeyCode >= 96 And KeyCode <= 105) Or _
(KeyCode = 190 Or KeyCode = 110) And _
InStr(1, TextBox1.Value, ".") = 0 Then Exit Sub

If Chr(KeyCode) Like "[A-Z]" Or _
KeyCode = 32 Or (KeyCode >= 106 And KeyCode <= 221) _
Or KeyCode = 190 Then KeyCode = 0
End Sub

'then :
'change format of Textbox value after we fill it by (0 to 9)
Private Sub TextBox1_AfterUpdate()
If InStr(1, TextBox1.Value, ".") > 0 Then
TextBox1.Value = FormatNumber(TextBox1.Value,
Len(TextBox1.Value) - InStr(1, TextBox1.Value, "."))
Else
TextBox1.Value = FormatNumber(TextBox1.Value, 2)
End If
End Sub


by that code all I want to achieve is disabled keyboard press unless
the keyboard pressed was
0 to 9 ...

Is my code efficient enough ?, or do you can make it more simple ?!


Regards,

Halim
 
G

Guest

You might try

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

If Not (KeyAscii >= Asc("0") And KeyAscii _
<= Asc("9") Or KeyAscii = Asc(".")) Then
KeyAscii = 0
End If


End Sub
 
H

halimnurikhwan

Hi Tom,
Cheerssssss....
That's work... thanks ...

But I have to modify to reject "." if already there ...
I use "." for indicating value after decimal And not
permit the user press "." if already pressed after decimal

And also I want :
If the first left Textbox.value is "." so textbox1.value= "0." +
blablabla is numeric

But I already finish it...

Thanks for your great and simple advise ....

Best Regards,

Halim


Tom Ogilvy menuliskan:
You might try

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

If Not (KeyAscii >= Asc("0") And KeyAscii _
<= Asc("9") Or KeyAscii = Asc(".")) Then
KeyAscii = 0
End If


End Sub

--
Regards,
Tom Ogilvy


Hi Ogilvy and else,

I've made this :
Private Declare Function GetKeyState Lib "user32" _
(ByVal nVirtKey As Long) As Integer
Private Const VK_SHIFT = &H10

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _
ByVal Shift As Integer)

'if Shift key was pressed
If GetKeyState(VK_SHIFT) < 0 Then KeyCode = 0

'if there's a decimal or dot on the first then fill 0 before it
If Left(TextBox1.Value, 1) = "." Then _
TextBox1.Value = Application.Substitute(TextBox1.Value, ".",
"0.", 1)

'to pass if ESC, Enter, Delete, Arrow Down or up, "." (dot) was
pressed
If KeyCode = 13 Or KeyCode = 27 Or KeyCode = 40 Or KeyCode = 38 Or
_
KeyCode = 46 Or KeyCode = 8 Or Chr(KeyCode) Like "[0-9]" Or _
(KeyCode >= 96 And KeyCode <= 105) Or _
(KeyCode = 190 Or KeyCode = 110) And _
InStr(1, TextBox1.Value, ".") = 0 Then Exit Sub

If Chr(KeyCode) Like "[A-Z]" Or _
KeyCode = 32 Or (KeyCode >= 106 And KeyCode <= 221) _
Or KeyCode = 190 Then KeyCode = 0
End Sub

'then :
'change format of Textbox value after we fill it by (0 to 9)
Private Sub TextBox1_AfterUpdate()
If InStr(1, TextBox1.Value, ".") > 0 Then
TextBox1.Value = FormatNumber(TextBox1.Value,
Len(TextBox1.Value) - InStr(1, TextBox1.Value, "."))
Else
TextBox1.Value = FormatNumber(TextBox1.Value, 2)
End If
End Sub


by that code all I want to achieve is disabled keyboard press unless
the keyboard pressed was
0 to 9 ...

Is my code efficient enough ?, or do you can make it more simple ?!


Regards,

Halim
 
H

halimnurikhwan

Hi Tom,

I'm now using these following code for my textbox:

In my past post, I want to enabled CTRL+S in my TextBox1
I'm no longer using API calls to detect CTRL keydown, I use:

Private Sub TextBox1_KeyDown(ByVal KeyCode As _
MSForms.ReturnInteger, ByVal Shift As Integer)
If Shift = 2 And KeyCode = 83 Then
KeyCode = 0
MsgBox "YOU PRESSED: CTRL + S"
End If
End Sub


For disabling unless 0-9 char and enabling only "." char I use and
directly
format TEXTBOX1.value to decimal and after decimal I use:

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If InStr(1, TextBox1.Value, ".") <> 0 Then
If Not (KeyAscii >= Asc("0") And KeyAscii _
<= Asc("9") And Not KeyAscii = Asc(".")) Then KeyAscii = 0
If Left(TextBox1.Value, 1) = "." Then _
TextBox1.Value = Application.Substitute(TextBox1.Value,
".", "0.", 1)
Else
If Not (KeyAscii >= Asc("0") And KeyAscii _
<= Asc("9") Or KeyAscii = Asc(".")) Then KeyAscii = 0
End If
End Sub

Thanks a lot for your suggestions.

Regards,

Halim

(e-mail address removed) menuliskan:
Hi Tom,
Cheerssssss....
That's work... thanks ...

But I have to modify to reject "." if already there ...
I use "." for indicating value after decimal And not
permit the user press "." if already pressed after decimal

And also I want :
If the first left Textbox.value is "." so textbox1.value= "0." +
blablabla is numeric

But I already finish it...

Thanks for your great and simple advise ....

Best Regards,

Halim


Tom Ogilvy menuliskan:
You might try

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

If Not (KeyAscii >= Asc("0") And KeyAscii _
<= Asc("9") Or KeyAscii = Asc(".")) Then
KeyAscii = 0
End If


End Sub

--
Regards,
Tom Ogilvy


Hi Ogilvy and else,

I've made this :
Private Declare Function GetKeyState Lib "user32" _
(ByVal nVirtKey As Long) As Integer
Private Const VK_SHIFT = &H10

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _
ByVal Shift As Integer)

'if Shift key was pressed
If GetKeyState(VK_SHIFT) < 0 Then KeyCode = 0

'if there's a decimal or dot on the first then fill 0 before it
If Left(TextBox1.Value, 1) = "." Then _
TextBox1.Value = Application.Substitute(TextBox1.Value, ".",
"0.", 1)

'to pass if ESC, Enter, Delete, Arrow Down or up, "." (dot) was
pressed
If KeyCode = 13 Or KeyCode = 27 Or KeyCode = 40 Or KeyCode = 38 Or
_
KeyCode = 46 Or KeyCode = 8 Or Chr(KeyCode) Like "[0-9]" Or _
(KeyCode >= 96 And KeyCode <= 105) Or _
(KeyCode = 190 Or KeyCode = 110) And _
InStr(1, TextBox1.Value, ".") = 0 Then Exit Sub

If Chr(KeyCode) Like "[A-Z]" Or _
KeyCode = 32 Or (KeyCode >= 106 And KeyCode <= 221) _
Or KeyCode = 190 Then KeyCode = 0
End Sub

'then :
'change format of Textbox value after we fill it by (0 to 9)
Private Sub TextBox1_AfterUpdate()
If InStr(1, TextBox1.Value, ".") > 0 Then
TextBox1.Value = FormatNumber(TextBox1.Value,
Len(TextBox1.Value) - InStr(1, TextBox1.Value, "."))
Else
TextBox1.Value = FormatNumber(TextBox1.Value, 2)
End If
End Sub


by that code all I want to achieve is disabled keyboard press unless
the keyboard pressed was
0 to 9 ...

Is my code efficient enough ?, or do you can make it more simple ?!


Regards,

Halim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top