VB.NET中让Textbox只能输入数字
2024-07-10 13:01:08
供稿:网友
public class form1
inherits system.windows.forms.form
#region " windows 窗体设计器生成的代码 "
public sub new()
mybase.new()
'该调用是 windows 窗体设计器所必需的。
initializecomponent()
'在 initializecomponent() 调用之后添加任何初始化
end sub
'窗体重写 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 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
friend withevents textbox1 as system.windows.forms.textbox
<system.diagnostics.debuggerstepthrough()> private sub initializecomponent()
me.textbox1 = new system.windows.forms.textbox
me.suspendlayout()
'
'textbox1
'
me.textbox1.location = new system.drawing.point(112, 96)
me.textbox1.name = "textbox1"
me.textbox1.tabindex = 0
me.textbox1.text = ""
'
'form1
'
me.autoscalebasesize = new system.drawing.size(6, 14)
me.clientsize = new system.drawing.size(292, 266)
me.controls.add(me.textbox1)
me.name = "form1"
me.text = "form1"
me.resumelayout(false)
end sub
#end region
dim str as string = ""
private sub textbox1_keyup(byval sender as object, byval e as system.windows.forms.keyeventargs) handles textbox1.keyup
if (e.keyvalue > 47 and e.keyvalue < 58) or (e.keyvalue > 95 and e.keyvalue < 106) or (e.keyvalue = 8) or (e.keyvalue = 45) or (e.keyvalue = 46) then
str = textbox1.text
else
textbox1.text = str
textbox1.focus()
end if
end sub
end class