欢迎光临
我们一直在努力

在textbox里放按钮并控制输入字符不能覆盖住按钮(包括设计阶段)-.NET教程,评论及其它

建站超值云服务器,限时71元/月

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

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 在textbox里放按钮并控制输入字符不能覆盖住按钮(包括设计阶段)-.NET教程,评论及其它
分享到: 更多 (0)