如何用VB.NET改变文本框类中图标的位置

问题描述 投票:0回答:1

我正在尝试使用 VB.NET 更改文本框类中图标的位置

也许我的代码不合适,请指导我。

请指导我。

谢谢

以下vb.net中的代码

公共类SearchTextBox 继承文本框 私有常量 EM_SETMARGINS 作为整数 = &Hd3 私有共享函数 SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wp As IntPtr, ByVal lp As IntPtr) As IntPtr 结束功能

    Private searchPictureBox As PictureBox

    Private cancelSearchButton As Button

    Public Sub New()
        cancelSearchButton = New Button()
        cancelSearchButton.Anchor = AnchorStyles.Top Or AnchorStyles.Right
        cancelSearchButton.Size = New Size(16, 16)
        cancelSearchButton.TabIndex = 0
        cancelSearchButton.TabStop = False
        cancelSearchButton.FlatStyle = FlatStyle.Flat
        cancelSearchButton.FlatAppearance.BorderSize = 0
        cancelSearchButton.Text = ""
        cancelSearchButton.Cursor = Cursors.Arrow

        Controls.Add(cancelSearchButton)

        AddHandler cancelSearchButton.Click, Sub()
            Text = ""
            Focus()
        End Sub

        searchPictureBox = New PictureBox()
        searchPictureBox.Anchor = AnchorStyles.Top Or AnchorStyles.Right
        searchPictureBox.Size = New Size(16, 16)
        searchPictureBox.TabIndex = 0
        searchPictureBox.TabStop = False
        Controls.Add(searchPictureBox)

        ' Send EM_SETMARGINS to prevent text from disappearing underneath the button
        SendMessage(Handle, EM_SETMARGINS, New IntPtr(2), New IntPtr(16 << 16))

        UpdateControlsVisibility()
    End Sub

    Protected Overrides Sub OnTextChanged(ByVal e As EventArgs)
        MyBase.OnTextChanged(e)
        UpdateControlsVisibility()
    End Sub

    Private Sub UpdateControlsVisibility()
        If String.IsNullOrEmpty(Text) Then
            cancelSearchButton.Visible = False
            searchPictureBox.Visible = True
        Else
            cancelSearchButton.Visible = True
            searchPictureBox.Visible = False
        End If
    End Sub

    <Browsable(True)>
    Public Property SearchImage As Image
        Set(ByVal value As Image)
            searchPictureBox.Image = value
            searchPictureBox.Left = Width - searchPictureBox.Size.Width - 4
            searchPictureBox.Top = Height - searchPictureBox.Size.Height - 4
        End Set

        Get
            Return searchPictureBox.Image
        End Get
    End Property

    <Browsable(True)>
    Public Property CancelSearchImage As Image
        Set(ByVal value As Image)
            cancelSearchButton.Image = value
            cancelSearchButton.Left = Width - searchPictureBox.Size.Width - 4
            cancelSearchButton.Top = Height - searchPictureBox.Size.Height - 4
        End Set

        Get
            Return cancelSearchButton.Image
        End Get
    End Property
End Class

代码结果

想要的结果

.net vb.net winforms textbox windows-forms-designer
1个回答
0
投票

这是具有预期结果的代码。

Imports System.ComponentModel

Public Class SearchTextBox
    Inherits TextBox

    Private Const EM_SETMARGINS As Integer = &HD3

    <System.Runtime.InteropServices.DllImport("user32.dll")>
    Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wp As IntPtr, ByVal lp As IntPtr) As IntPtr
    End Function

    Private ReadOnly _IconPictureBox As PictureBox

    Private _searchImage As Image

    Private _cancelSearchImage As Image


    Public Sub New()
        _IconPictureBox = New PictureBox With {
            .Anchor = AnchorStyles.Top Or AnchorStyles.Right,
            .Size = New Size(Height - (Margin.Vertical + 2), Height - (Margin.Vertical + 2)),
            .Top = Margin.Top,
            .Left = Margin.Left,
            .TabIndex = 0,
            .TabStop = False,
            .SizeMode = PictureBoxSizeMode.StretchImage
        }
        AddHandler _IconPictureBox.Click, Sub()
                                              Text = ""
                                              Focus()
                                          End Sub
        Controls.Add(_IconPictureBox)

        ' Send EM_SETMARGINS to prevent text from disappearing underneath the button
        SendMessage(Handle, EM_SETMARGINS, New IntPtr(2), New IntPtr(16 << 16))

        UpdateIcon()
    End Sub

    Protected Overrides Sub OnTextChanged(ByVal e As EventArgs)
        MyBase.OnTextChanged(e)
        UpdateIcon()
    End Sub

    Private Sub UpdateIcon()
        If String.IsNullOrEmpty(Text) Then
            _IconPictureBox.Image = _searchImage
            _IconPictureBox.Left = Margin.Left
        Else
            _IconPictureBox.Image = _cancelSearchImage
            _IconPictureBox.Cursor = Cursors.Arrow
            _IconPictureBox.Left = Width - _IconPictureBox.Size.Width - Margin.Horizontal
        End If
    End Sub

    <Browsable(True)>
    Public Property SearchImage As Image
        Set(value As Image)
            _searchImage = value
            UpdateIcon()
        End Set

        Get
            Return _searchImage
        End Get
    End Property

    <Browsable(True)>
    Public Property CancelSearchImage As Image
        Set(value As Image)
            _cancelSearchImage = value
            UpdateIcon()
        End Set

        Get
            Return _cancelSearchImage
        End Get
    End Property
End Class

这是结果:

  • 当文本框为空时:

  • 有内容时:

如有必要,这是我使用的图像:

  • https://i.stack.imgur.com/Zt5jL.png
  • https://i.stack.imgur.com/Su1nA.png
© www.soinside.com 2019 - 2024. All rights reserved.