PDA

View Full Version : سوال: محدود کردن کاربر برای وارد کردن عدد



erfan12
یک شنبه 22 بهمن 1391, 13:54 عصر
چطوری میتونم کاری کنم که در یک TextBox بشه فقط عدد نوشت و کلید backspace هم فعال باشه؟
البته در TextBox بشه از علامت منفی - و نقطه . هم استفاده کرد

ferdin
یک شنبه 22 بهمن 1391, 14:15 عصر
سلام


Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case vbKey0 To vbKey9
Case vbKeyBack, vbKeyClear, vbKeyDelete
Case vbKeyLeft, vbKeyRight, vbKeyUp, vbKeyDown, vbKeyTab
Case 45
Case Else
KeyAscii = 0
Beep
End Select
End Sub


فکر کنم این کد مشکلتو حل می کنه .

AbbasVB
یک شنبه 22 بهمن 1391, 14:26 عصر
Option Explicit
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

Const GWL_STYLE = (-16)

Const ES_NUMBER = &H2000&

Public Sub SetNumber(NumberText As TextBox, Flag As Boolean)

Dim curstyle As Long, newstyle As Long

curstyle = GetWindowLong(NumberText.hwnd, GWL_STYLE)

If Flag Then
curstyle = curstyle Or ES_NUMBER
Else
curstyle = curstyle And (Not ES_NUMBER)

End If

newstyle = SetWindowLong(NumberText.hwnd, GWL_STYLE, curstyle)
NumberText.Refresh

End Sub

Private Sub Form_Load()
SetNumber Text1, True

End Sub