当前位置:编程学习 > C#/ASP.NET >>

在textbox里放按钮并控制输入字符不能覆盖住按钮(包括设计阶段)

答案:

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

 &nbs

上一个:Grasshopper简介(节选)
下一个:获得光标在多行textbox中的行与列的函数

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,