imports system.text
public class mytextbox
inherits system.windows.forms.textbox
dim withevents btn as button
dim withevents btn as button
public sub new()
mybase.new()
该调用是 windows 窗体设计器所必需的。
该调用是 windows 窗体设计器所必需的。
initializecomponent()
在 initializecomponent() 调用之后添加任何初始化
在 initializecomponent() 调用之后添加任何初始化
btn = new button()
btn.size = new size(23, 23)
me.controls.add(btn)
btn.dock = dockstyle.right
btn.backcolor = systemcolors.control
me.text = “”
end sub
public event myclick(byval sender as object, byval e as eventargs)
public event myclick(byval sender as object, byval e as eventargs)
private sub btn_click(byval sender as object, byval e as system.eventargs) handles btn.click
raiseevent myclick(me, e)
end sub
private sub btn_mouseenter(byval sender as object, byval e as system.eventargs) handles btn.mouseenter
private sub btn_mouseenter(byval sender as object, byval e as system.eventargs) handles btn.mouseenter
cursor = cursors.default
end sub
private sub btn_mouseleave(byval sender as object, byval e as system.eventargs) handles btn.mouseleave
private sub btn_mouseleave(byval sender as object, byval e as system.eventargs) handles btn.mouseleave
cursor = cursors.ibeam
end sub
protected overrides sub wndproc(byref m as system.windows.forms.message)
protected overrides sub wndproc(byref m as system.windows.forms.message)
dim keyasc as integer = m.wparam.toint32
select case m.msg
case &h102
if checklength() then
if keyasc <> 8 then
return
end if
else
me.maxlength = 0
end if
case &h302
if checklength() then
return
end if
end select
mybase.wndproc(m)
end sub
检查函数
private function checklength(optional byval str as string = “”) as boolean
if str = “” then
dim leng as integer = cint(me.creategraphics.measurestring(me.text, me.font).width)
dim m as integer = me.width
dim p as integer = btn.width
if leng >= m – p – 5 then
return true
end if
else
dim leng as integer = cint(me.creategraphics.measurestring(str, me.font).width)
dim m as integer = me.width
dim p as integer = btn.width
if leng + 1 >= m – p then
return true
end if
end if
return false
end function
修改text属性时检查
public overrides property text() as string
get
return mybase.text
end get
set(byval value as string)
if checklength(value) then
throw new exception(“超出可以显示的范围!”)
end if
mybase.text = value
end set
end property
end class
end class