实用的一个文本框类,只能输入ip地址
如题。欢迎暴力测试,找出bug!10/12再次更新代码如下:
解决selstart值不正确的问题
弃用inputmask,它限制了输入格式

************************************************** *-- 类: txt_ip (d:\documents\visual foxpro 项目\myclass.vcx) *-- 父类: textbox *-- 基类: textbox *-- 时间戳: 10/12/25 04:33:07 PM * DEFINE CLASS txt_ip AS textbox Value = 0.0.0.0 Height = 20 Left = 12 Top = 48 Width = 110 Name = "txt_ip" PROCEDURE KeyPress LPARAMETERS nKeyCode, nShiftAltCtrl ndotpos1=AT(".",this.Value,1) ndotpos2=AT(".",this.Value,2) ndotpos3=AT(".",this.Value,3) ncursor=this.SelStart *SET MESSAGE TO TRANSFORM(ncursor) IF ("." $ this.SelText) NODEFAULT && 当选择的字符里有“.”号时,不允许键盘操作 ELSE DO CASE CASE nkeycode=127 IF SUBSTR(this.Value,ncursor,1)="." NODEFAULT ENDIF CASE nkeycode=7 IF SUBSTR(this.Value,ncursor+1,1)="." NODEFAULT ENDIF CASE nkeycode=9 OR nkeycode=46 OR nkeycode=13 IF ncursor<ndotpos3 &&在前三个ip段,改变tab,“.”,回车的默认行为为向后移一个ip段,并全选该段 NODEFAULT DO CASE CASE BETWEEN(ncursor,ndotpos2,ndotpos3) this.SelStart=ndotpos3 this.SelLength=LEN(this.Value)-ndotpos3-1 CASE BETWEEN(ncursor,ndotpos1,ndotpos2) this.SelStart=ndotpos2 this.SelLength=ndotpos3-ndotpos2-1 CASE BETWEEN(ncursor,0,ndotpos1) this.SelStart=ndotpos1 this.SelLength=ndotpos2-ndotpos1-1 OTHERWISE ENDCASE ELSE IF nkeycode=46 NODEFAULT KEYBOARD "{tab}" && 在第四个ip段屏蔽“.”以tab键代替 ENDIF ENDIF CASE !(BETWEEN(nkeycode,48,57) OR INLIST(nkeycode,4,7,9,13,19,46,127)) NODEFAULT &&只允许数字键和控制键 OTHERWISE ENDCASE ENDIF ENDPROC PROCEDURE GotFocus this.SelStart=0 ndotpos1=AT(".",this.Value) this.SelLength=ndotpos1-1 ENDPROC PROCEDURE Valid ndots=OCCURS(".",this.Value) nsections=ALINES(atemp,this.Value,5,".") IF nsections<>4 &&说明有某个段没有输入数字,定位到没有数字的ip段 this.Selstart=AT("..",this.Value) RETURN .f. ELSE FOR i=1 TO 4 IF !BETWEEN(EVALUATE(atemp(i)),0,255) &&ip值有效检查,定位到错误的ip段 this.Selstart=IIF(i=1,0,AT(".",this.Value,i-1)) this.SelLength=LEN(atemp(i)) RETURN .f. ENDIF ENDFOR ENDIF ENDPROC ENDDEFINE * *-- EndDefine: txt_ip **************************************************
[此贴子已经被作者于2025-10-12 17:02编辑过]