可以限制中文输入的文本框
2024-07-21 02:21:09
供稿:网友
根据网友提的意见,大家都需要限制中文输入,今天,我试验了一下,找了一个办法限制中文输入,测试已经通过,大家也自己试一下吧,有什么意见继续提,如果愿意加我的qq:36745349
这个是在昨天的基础上做的更改,测试已经满足大家的要求,如果谁有更好的办法也请交流一下
下面是代码:
'sunnyxing2004-04-01最后修改
public class mytextbox
inherits system.windows.forms.textbox
private m_strvalidtext as string = "0123456789.+-" & chr(13).tostring
private m_blneditable as boolean = true
#region " windows 窗体设计器生成的代码 "
public sub new()
mybase.new()
'该调用是 windows 窗体设计器所必需的。
initializecomponent()
'在 initializecomponent() 调用之后添加任何初始化
end sub
'usercontrol1 重写 dispose 以清理组件列表。
protected overloads overrides sub dispose(byval disposing as boolean)
if disposing then
if not (components is nothing) then
components.dispose()
end if
end if
mybase.dispose(disposing)
end sub
'windows 窗体设计器所必需的
private components as system.componentmodel.icontainer
'注意: 以下过程是 windows 窗体设计器所必需的
'可以使用 windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
<system.diagnostics.debuggerstepthrough()> private sub initializecomponent()
components = new system.componentmodel.container
end sub
#end region
private sub mytextbox_keypress(byval sender as object, byval e as system.windows.forms.keypresseventargs) handles mybase.keypress
dim strlocalstring as string
if editable then
strlocalstring = m_strvalidtext & chr(8).tostring
else
strlocalstring = m_strvalidtext
end if
if ucase(strlocalstring).indexof(ucase(e.keychar)) < 0 then
e.handled = true
beep()
else
end if
end sub
public property validtext() as string
get
return m_strvalidtext
end get
set(byval value as string)
m_strvalidtext = value
end set
end property
public property editable() as boolean
get
return m_blneditable
end get
set(byval value as boolean)
m_blneditable = value
end set
end property
public sub checktext(byval sender as object, byval e as system.eventargs) handles mybase.textchanged
dim cha as char
try
cha = ctype(me.text.substring(me.selectionstart - 1, 1), char)
if m_strvalidtext.indexof(cha) < 0 then
me.text = me.text.remove(me.selectionstart - 1, 1)
end if
catch ex as exception
end try
end sub
end class