Sub HHStart_KeyPress (key)
'input only number
If IsNumber(key) = false AND Asc(key) <> 8 Then HHstart.IgnoreKey
'mouse selected character and replace
If StrLength(HHStart.Text) <=2 AND HHstart.SelectionLength<>0 Then
s = HHStart.SelectionStart
HHstart.Text=StrRemove(HHstart.Text,s,HHstart.SelectionLength)
HHStart.SelectionStart=s
End If
'limiting input length
If StrLength(HHstart.text)>=2 AND Asc(key) > 31 Then HHstart.IgnoreKey
'limiting input number range
????????? from 0 to 24 only
End Sub
Sub HHStart_KeyPress (key)
'process only numbers, Asc("0")=48, AsSc("1")= 50 etc. and backspace = 8
If (IsNumber(key) = false AND Asc(key) <> 8) Then
HHstart.IgnoreKey
Return
End If
If HHstart.SelectionLength <> 0 Then
s = HHStart.SelectionStart
If s = 0 AND Asc(key) > 50 AND StrLength(HHStart.Text) >=2 Then
' only allow first digit to be in range
HHstart.IgnoreKey
Return
End If
HHstart.Text=StrRemove(HHstart.Text,s,HHstart.SelectionLength)
HHStart.SelectionStart=s
End If
'limit input number range
If StrLength(HHStart.Text) > 0 AND IsNumber(key) Then
first = Asc(StrAt(HHStart.Text,0))
If first > 50 Then
HHstart.IgnoreKey ' first digit more than "2"
Else If first = 50 AND Asc(Key) > 52 Then
HHstart.IgnoreKey ' final number would be > 24
Else If StrLength(HHStart.Text) >= 2
HHstart.IgnoreKey ' final number would be more then two digits
End If
End If
End Sub
Or maybe a TrackBar?Maybe you can use a NumUpDown control instead of a TextBox?
Sub HHStart_KeyPress (key)
'process only numbers and backspace
If (IsNumber(key) = false AND Asc(key) <> 8) Then
HHstart.IgnoreKey
Return
End If
End Sub
Sub HHStart_LostFocus (key)
'process only numbers and backspace
If HHStart.Text <0 OR HHStart.Text > 24 Then
Msgbox("Must be between 0 and 24")
HHStart.Focus
End If
End Sub
Maybe you can use a NumUpDown control instead of a TextBox?
:sign0148:difficult for years of not writing code like me, only started again when found Basic4PPC and still refreshing and learning. I could try to do some logic flowcharting then...as it seems I could not do it on the spot anymore.agraham said:The logic is more complicated than it seems
I just love a challenge! Try to break this oneif I have solution for these kind of filters on textbox, I could think of some other positive applications.
Sub HHStart_KeyPress (key)
' I know this takes time but this is slow user input processing and avoids global clutter
MAXNUM = 3456
HHshadow = HHstart.text
If (IsNumber(key) = false AND Asc(key) <> 8) Then
'process only numbers, Asc("0")=48, AsSc("1")= 50 etc. and backspace = 8
HHstart.IgnoreKey
Return
Else If HHstart.SelectionLength <> 0 Then
' part of string selected
ss = HHStart.SelectionStart
sl = HHStart.SelectionLength
hl = StrLength(HHstart.text)
first = SubString(HHshadow,0,ss)
last = SubString(HHshadow,ss+sl,hl-sl)
HHshadow = first & key & last
Else
' no selection so process normally and check limit
HHshadow = HHshadow & key
End If
If IsNumber(key) AND HHshadow > MAXNUM Then
HHstart.IgnoreKey
Return
End If
End Sub
agraham said:I just love a challenge! Try to break this one
Easy!
1. Type 0000 and at the beginning type any number
2. Paste anything Shift-Insert (desktop only, OK doesn't counts)
3. Type 555 and at the beginning try to type any number
Sub HHStart_KeyPress (key)
'maxNum, ControlName,key
textboxFilter01("12345","HHStart",key)
End Sub
Sub textboxFilter01(MaxNum,tb,key)
HHshadow=Control(tb).Text
'process only numbers and backspace
If (IsNumber(key) = false AND Asc(key) <> 8) Then
Control(tb).ignorekey
Return
End If
'mouse selected character and replace
If Control(tb).SelectionLength<>0 AND Asc(key) <> 8 Then
s = Control(tb).SelectionStart
Control(tb).Text=StrRemove(Control(tb).Text,s,Control(tb).SelectionLength)
Control(tb).SelectionStart=s
n=StrInsert(Control(tb).Text,Control(tb).selectionstart,key)
If (Asc(key) <> 8 AND n>maxNum ) Then
Control(tb).Text=HHshadow
Control(tb).SelectionStart=s
Control(tb).SelectionLength=1
End If
End If
'insert input
If StrLength(Control(tb).text)<StrLength(maxnum) Then
n=StrInsert(Control(tb).Text,Control(tb).selectionstart,key)
If (Asc(key) <> 8 AND n>maxNum ) Then
Control(tb).ignoreKey
End If
End If
'limiting input length
If (StrLength(Control(tb).text)>=StrLength(maxnum) AND Asc(key) <> 8) Then
Control(tb).IgnoreKey
Return
End If
End Sub
Yeah. I realised that last night. There is a fundamental problem in that you don't know and can't find where the insertion point is. So you can deal with selections and normal input appended at the end but cannot predict the result of insertions. The only way round this I can think of is to reset the insertion point to disallow insertion and also disallow leading zerosEasy!
Sub HHStart_KeyPress (key)
' I know this takes time but this is slow user input processing and avoids global clutter
MAXNUM = 3456
HHshadow = HHstart.text
If StrLength(HHstart.text) = 0 AND Asc(Key) = 48 Then
'don't allow leading zeros
HHstart.IgnoreKey
Return
End If
If (IsNumber(key) = false AND Asc(key) <> 8) Then
'process only numbers, Asc("0")=48, AsSc("1")= 50 etc. and backspace = 8
HHstart.IgnoreKey
Return
Else If HHstart.SelectionLength <> 0 Then
' part of string selected
ss = HHStart.SelectionStart
sl = HHStart.SelectionLength
hl = StrLength(HHstart.text)
first = SubString(HHshadow,0,ss)
last = SubString(HHshadow,ss+sl,hl-sl)
HHshadow = first & key & last
Else
' no selection so process normally and check limit
HHStart.SelectionStart = StrLength(HHshadow)
HHshadow = HHshadow & key
End If
If IsNumber(key) AND HHshadow > MAXNUM Then
HHstart.IgnoreKey
Return
End If
End Sub
Sub Globals
'Declare the global variables here.
MAXNUM = 3456
Dim AGeventsTextSave
End Sub
Sub App_Start
Form1.Show
' AGeventsX is a ControlEvents object
AGeventsX.New1("AGevents")
End Sub
Sub AGeventsX_TextCHanged()
If AGevents.Text > MAXNUM Then
AGevents.Text = AGeventsTextSave
AGevents.SelectionStart = StrLength(AGevents.text)
Else
AGeventsTextSave = AGevents.Text
End If
End Sub
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?