none
how to make a textbox with a x button on the side of it... in vb.net im using visual studio 2012...

    Question

  • how to make a textbox with a x button on the side of it... in vb.net im using visual studio 2012...
    Tuesday, August 28, 2012 6:33 PM

Answers

  • I have not gone into the fullest possible extent with this, but you are going to want to make a custom user control, here is a basic example.

    Option Strict On
    Public Class Form1
        WithEvents SpecialTB As New SpecialTB
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            SpecialTB.SearchIcon = My.Resources.Glass
            Me.Controls.Add(SpecialTB)
        End Sub
        Sub SpecialTB_SearchClicked(ByVal sender As Object, ByVal e As EventArgs) Handles SpecialTB.SearchClicked
            MsgBox("Search Button Clicked")
        End Sub
        Sub SpecilaTB_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles SpecialTB.TextChanged
            'text changed events
        End Sub
    End Class
    Class SpecialTB
        Inherits UserControl
        WithEvents TextBox As New TextBox
        WithEvents PictureBox As New PictureBox
        'To make this a 'one thing', you must convert all of the individual events
        'into events for this control
        Public Event SearchClicked(ByVal sender As Object, ByVal e As EventArgs)
        Public Shadows Event TextChanged(ByVal sender As Object, ByVal e As EventArgs)
        Private Property _Text As String
        Private Property _SearchIcon As Image
        Public Property SearchIcon As Image
            Get
                Return _SearchIcon
            End Get
            Set(ByVal value As Image)
                PictureBox.BackgroundImage = value
                PictureBox.BackgroundImageLayout = ImageLayout.Stretch
                _SearchIcon = value
            End Set
        End Property
        Public Shadows Property Text As String
            Get
                Return _Text
            End Get
            Set(ByVal value As String)
                _Text = value
            End Set
        End Property
        Sub New()
            Me.MinimumSize = New Size(New Point(122, 22))
            PictureBox.Left = Me.Width - 22
            PictureBox.Size = CType(New Point(22, 22), Drawing.Size)
            PictureBox.BackColor = Color.White
            PictureBox.Cursor = Cursors.Hand
            Me.BackColor = Color.White
            TextBox.BorderStyle = Windows.Forms.BorderStyle.None
            TextBox.Width = Me.ClientRectangle.Width - PictureBox.ClientRectangle.Width
            TextBox.Font = New Font("verdana", 12, FontStyle.Regular)
            Me.BorderStyle = Windows.Forms.BorderStyle.FixedSingle
            Me.Height = PictureBox.Height
            Me.Controls.Add(TextBox)
            Me.Controls.Add(PictureBox)
        End Sub
        'Add whatever event handlers for the individual components
        Sub PBClick(ByVal sender As Object, ByVal e As EventArgs) Handles PictureBox.Click
            RaiseEvent SearchClicked(sender, e)
        End Sub
        Sub TxtChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox.TextChanged
            Me.Text = TextBox.Text
            RaiseEvent TextChanged(sender, e)
        End Sub
    End Class


    If you want something you've never had, you need to do something you've never done.

    Wednesday, August 29, 2012 3:55 AM

All replies

  • I don't see an x button in your image.

    You've taught me everything I know but not everything you know.

    Wednesday, August 29, 2012 2:21 AM
  • I have not gone into the fullest possible extent with this, but you are going to want to make a custom user control, here is a basic example.

    Option Strict On
    Public Class Form1
        WithEvents SpecialTB As New SpecialTB
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            SpecialTB.SearchIcon = My.Resources.Glass
            Me.Controls.Add(SpecialTB)
        End Sub
        Sub SpecialTB_SearchClicked(ByVal sender As Object, ByVal e As EventArgs) Handles SpecialTB.SearchClicked
            MsgBox("Search Button Clicked")
        End Sub
        Sub SpecilaTB_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles SpecialTB.TextChanged
            'text changed events
        End Sub
    End Class
    Class SpecialTB
        Inherits UserControl
        WithEvents TextBox As New TextBox
        WithEvents PictureBox As New PictureBox
        'To make this a 'one thing', you must convert all of the individual events
        'into events for this control
        Public Event SearchClicked(ByVal sender As Object, ByVal e As EventArgs)
        Public Shadows Event TextChanged(ByVal sender As Object, ByVal e As EventArgs)
        Private Property _Text As String
        Private Property _SearchIcon As Image
        Public Property SearchIcon As Image
            Get
                Return _SearchIcon
            End Get
            Set(ByVal value As Image)
                PictureBox.BackgroundImage = value
                PictureBox.BackgroundImageLayout = ImageLayout.Stretch
                _SearchIcon = value
            End Set
        End Property
        Public Shadows Property Text As String
            Get
                Return _Text
            End Get
            Set(ByVal value As String)
                _Text = value
            End Set
        End Property
        Sub New()
            Me.MinimumSize = New Size(New Point(122, 22))
            PictureBox.Left = Me.Width - 22
            PictureBox.Size = CType(New Point(22, 22), Drawing.Size)
            PictureBox.BackColor = Color.White
            PictureBox.Cursor = Cursors.Hand
            Me.BackColor = Color.White
            TextBox.BorderStyle = Windows.Forms.BorderStyle.None
            TextBox.Width = Me.ClientRectangle.Width - PictureBox.ClientRectangle.Width
            TextBox.Font = New Font("verdana", 12, FontStyle.Regular)
            Me.BorderStyle = Windows.Forms.BorderStyle.FixedSingle
            Me.Height = PictureBox.Height
            Me.Controls.Add(TextBox)
            Me.Controls.Add(PictureBox)
        End Sub
        'Add whatever event handlers for the individual components
        Sub PBClick(ByVal sender As Object, ByVal e As EventArgs) Handles PictureBox.Click
            RaiseEvent SearchClicked(sender, e)
        End Sub
        Sub TxtChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox.TextChanged
            Me.Text = TextBox.Text
            RaiseEvent TextChanged(sender, e)
        End Sub
    End Class


    If you want something you've never had, you need to do something you've never done.

    Wednesday, August 29, 2012 3:55 AM
  • Hi hartjo,

    Welcome to the MSDN forum.

    The style of the office 2013 is win8 project. So hardly can you use Winfrom controls to make it, using WPF or win8 project seems to be a good choose.

    If you insist to use winform application, you need to customer the textbox by yourself. Draw a “X” at the end of the textbox, or insert a picture into textbox.

    Hope this helps.


    Mark Liu-lxf [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, August 29, 2012 4:32 AM