Results 1 to 7 of 7

Thread: Merging a Button and a Textbox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    171

    Merging a Button and a Textbox

    Hey guys,

    It's been a while.

    I'm looking to create a textbox that has a button attached to it at the end of the textbox, like the modern day login screens.

    Has anyone have any idea how to do this in VB.NET?

    Cheers

  2. #2
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    196

    Re: Merging a Button and a Textbox

    a usercontrol?
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Merging a Button and a Textbox

    Essentially you want a Button addon where like this. Your best option is to use WPF since you can create the XAML markup to get the desired results, but since I'm assuming that you're wanting a Windows Form Application solution, an alternative would be to use a TableLayoutPanel to force a Button to the left/right of your TextBox.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,990

    Re: Merging a Button and a Textbox

    I'd also suggest a user control. I built something like this (with two buttons) for a project. It's a difficult thing to post...especially since I'm in a meeting, at the moment, but it's the way to go, in my opinion.

    Since I have a bit of time, the designer file looks like this:

    Code:
    <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
    Partial Class MortalityTB
        Inherits System.Windows.Forms.UserControl
    
        'UserControl overrides dispose to clean up the component list.
        <System.Diagnostics.DebuggerNonUserCode()> _
        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            Try
                If disposing AndAlso components IsNot Nothing Then
                    components.Dispose()
                End If
            Finally
                MyBase.Dispose(disposing)
            End Try
        End Sub
    
        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer
    
        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        <System.Diagnostics.DebuggerStepThrough()> _
        Private Sub InitializeComponent()
            Me.tbEntry = New System.Windows.Forms.TextBox()
            Me.bMark = New System.Windows.Forms.Button()
            Me.bComment = New System.Windows.Forms.Button()
            Me.SuspendLayout()
            '
            'tbEntry
            '
            Me.tbEntry.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
            Me.tbEntry.Location = New System.Drawing.Point(0, 0)
            Me.tbEntry.MaxLength = 7
            Me.tbEntry.Name = "tbEntry"
            Me.tbEntry.Size = New System.Drawing.Size(64, 24)
            Me.tbEntry.TabIndex = 0
            '
            'bMark
            '
            Me.bMark.BackColor = System.Drawing.SystemColors.ControlLight
            Me.bMark.Location = New System.Drawing.Point(64, 0)
            Me.bMark.Name = "bMark"
            Me.bMark.Size = New System.Drawing.Size(24, 24)
            Me.bMark.TabIndex = 1
            Me.bMark.TabStop = False
            Me.bMark.Text = "M"
            Me.bMark.UseVisualStyleBackColor = False
            '
            'bComment
            '
            Me.bComment.BackColor = System.Drawing.SystemColors.ControlLight
            Me.bComment.Location = New System.Drawing.Point(88, 0)
            Me.bComment.Name = "bComment"
            Me.bComment.Size = New System.Drawing.Size(24, 24)
            Me.bComment.TabIndex = 3
            Me.bComment.TabStop = False
            Me.bComment.Text = "C"
            Me.bComment.UseVisualStyleBackColor = False
            '
            'MortalityTB
            '
            Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
            Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
            Me.Controls.Add(Me.bMark)
            Me.Controls.Add(Me.bComment)
            Me.Controls.Add(Me.tbEntry)
            Me.Name = "MortalityTB"
            Me.Size = New System.Drawing.Size(113, 24)
            Me.ResumeLayout(False)
            Me.PerformLayout()
    
        End Sub
        Friend WithEvents tbEntry As System.Windows.Forms.TextBox
        Friend WithEvents bMark As System.Windows.Forms.Button
        Friend WithEvents bComment As System.Windows.Forms.Button
    
    End Class
    The rest of the code looks like this:

    Code:
    Friend Class MortalityTB
    
        Friend Sub New()
    
            ' This call is required by the designer.
            InitializeComponent()
    
            ' Add any initialization after the InitializeComponent() call.
            mIntOnly = True
            mRUID = Guid.Empty
        End Sub
    
    #Region "Properties"
    
        Private mIntOnly As Boolean
        Private mRUID As Guid
        Private mSupressChangeEvent As Boolean
        Private mSelected As Boolean
        Private mCurrentData As String
        Private mDayShown As Integer
        Private mComment As String = String.Empty
        Private mHasMarks As Boolean
        Private mMarkOverridesEntry As Boolean
        Public Property EventDate As Date
    
        Friend Property RUID As Guid
            Get
                Return mRUID
            End Get
            Set(value As Guid)
                mRUID = value
            End Set
        End Property
    
        Friend Property MarkVisible As Boolean
            Get
                Return bMark.Visible
            End Get
            Set(value As Boolean)
                bMark.Visible = value
                If bMark.Visible Then
                    Me.tbEntry.Width = Me.Width - Me.bMark.Width - Me.bComment.Width
                Else
                    Me.tbEntry.Width = Me.Width - Me.bComment.Width
                End If
            End Set
        End Property
    
        Friend Property IntegerOnly As Boolean
            Get
                Return mIntOnly
            End Get
            Set(value As Boolean)
                mIntOnly = value
            End Set
        End Property
    
        Friend Property MarkEnabled As Boolean
            Get
                Return Me.bMark.Enabled
            End Get
            Set(value As Boolean)
                Me.bMark.Enabled = value
            End Set
        End Property
    
        Friend Property CommentEnabled As Boolean
            Get
                Return Me.bComment.Enabled
            End Get
            Set(value As Boolean)
                Me.bComment.Enabled = value
            End Set
        End Property
    
        Public Overrides Property Text As String
            Get
                If Me.tbEntry.Text = String.Empty Then
                    Return "0"
                Else
                    Return Me.tbEntry.Text
                End If
            End Get
            Set(value As String)
                mSupressChangeEvent = True
                If value <> "" Then
                    If mIntOnly Then
                        Dim n As Integer
                        If Integer.TryParse(value, n) Then
                            If n = 0 Then
                                Me.tbEntry.Text = String.Empty
                            Else
                                Me.tbEntry.Text = value
                            End If
                        Else
                            Me.tbEntry.Text = String.Empty
                            RaiseEvent TextAdditionFailed(Me, EventArgs.Empty)
                        End If
                    Else
                        Dim d As Decimal
                        If Decimal.TryParse(value, d) Then
                            If d = 0 Then
                                Me.tbEntry.Text = String.Empty
                            Else
                                Me.tbEntry.Text = value
                            End If
                        Else
                            Me.tbEntry.Text = String.Empty
                            RaiseEvent TextAdditionFailed(Me, EventArgs.Empty)
                        End If
                    End If
                Else
                    Me.tbEntry.Text = String.Empty
                End If
                mCurrentData = Me.tbEntry.Text
                mSupressChangeEvent = False
            End Set
        End Property
    
        Public Overrides Property BackColor As System.Drawing.Color
            Get
                Return tbEntry.BackColor
            End Get
            Set(value As System.Drawing.Color)
                tbEntry.BackColor = value
            End Set
        End Property
    
        Friend Property Selected As Boolean
            Get
                Return mSelected
            End Get
            Set(value As Boolean)
                mSelected = value
                If Me.tbEntry.Enabled Then
                    SetSelected()
                End If
            End Set
        End Property
    
        Friend Property PseudoEnable As Boolean
            Get
                Return Me.tbEntry.Enabled
            End Get
            Set(value As Boolean)
                Me.tbEntry.Enabled = value
                Me.bComment.Enabled = value
                Me.bMark.Enabled = value
            End Set
        End Property
    
        Friend Property DayShown As Integer
            Get
                Return mDayShown
            End Get
            Set(value As Integer)
                mDayShown = value
            End Set
        End Property
    
        Friend Property Comment As String
            Get
                Return mComment
            End Get
            Set(value As String)
                If value Is Nothing Then
                    mComment = String.Empty
                Else
                    mComment = value
                End If
                If mComment <> String.Empty AndAlso Me.tbEntry.Enabled Then
                    Me.bComment.BackColor = Drawing.Color.LightYellow
                ElseIf Me.tbEntry.Enabled Then
                    Me.bComment.BackColor = Drawing.SystemColors.Control
                Else
                    Me.bComment.BackColor = Nothing
                End If
            End Set
        End Property
    
        Friend Property HasMarks As Boolean
            Get
                Return mHasMarks
            End Get
            Set(value As Boolean)
                mHasMarks = value
                If mHasMarks AndAlso Me.tbEntry.Enabled Then
                    Me.bMark.BackColor = Drawing.Color.LightPink
                ElseIf Me.tbEntry.Enabled Then
                    Me.bMark.BackColor = Drawing.SystemColors.Control
                Else
                    Me.bMark.BackColor = Nothing
                End If
            End Set
        End Property
    
        Friend Property MarkOverridesEntry As Boolean
            Get
                Return mMarkOverridesEntry
            End Get
            Set(value As Boolean)
                mMarkOverridesEntry = value
            End Set
        End Property
    
        Friend Property MarkCharacter As String
            Get
                Return Me.bMark.Text
            End Get
            Set(value As String)
                If value.Length > 1 Then
                    Me.bMark.Text = value.Substring(0, 1)
                Else
                    Me.bMark.Text = value
                End If
            End Set
        End Property
    
    #End Region
    
    #Region "Custom Class Events and Event Args"
    
        Friend Event MarkClicked(sender As Object, e As EventArgs)
        Friend Event CommentClicked(sender As Object, e As EventArgs)
        Friend Event TextAdditionFailed(sender As Object, e As EventArgs)
        Friend Event KeyFailure(sender As Object, e As EventArgs)
        Friend Event DataChanged(sender As Object, e As DataChangeEvent)
    
    #End Region
    
    #Region "Control Events"
    
        Private Sub tbEntry_GotFocus(sender As Object, e As System.EventArgs) Handles tbEntry.GotFocus
            If Me.tbEntry.Text <> String.Empty Then
                Me.tbEntry.SelectAll()
            End If
        End Sub
    
        Private Sub tbEntry_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles tbEntry.KeyPress
            If mMarkOverridesEntry Then
                'If there are marks, then don't even let them mess with this.
                e.Handled = True
                RaiseEvent MarkClicked(Me, EventArgs.Empty)
            Else
                If mIntOnly Then
                    If Not Char.IsDigit(e.KeyChar) AndAlso e.KeyChar <> (ControlChars.Back) Then
                        e.Handled = True
                    End If
                Else
                    If Not Char.IsDigit(e.KeyChar) AndAlso e.KeyChar <> (ControlChars.Back) AndAlso e.KeyChar <> CChar(".") Then
                        e.Handled = True
                    End If
                End If
            End If
        End Sub
    
        Private Sub tbEntry_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles tbEntry.Validating
            If (Not mSupressChangeEvent) AndAlso mCurrentData <> Me.tbEntry.Text Then
                If mIntOnly Then
                    Dim i As Integer
                    If Integer.TryParse(Me.tbEntry.Text, i) Then
                        RaiseEvent DataChanged(Me, New DataChangeEvent(mCurrentData))
                        mCurrentData = Me.tbEntry.Text
                    Else
                        Me.tbEntry.Text = mCurrentData
                        RaiseEvent TextAdditionFailed(Me, EventArgs.Empty)
                    End If
                Else
                    Dim d As Decimal
                    If Decimal.TryParse(Me.tbEntry.Text, d) Then
                        RaiseEvent DataChanged(Me, New DataChangeEvent(mCurrentData))
                        mCurrentData = Me.tbEntry.Text
                    Else
                        Me.tbEntry.Text = mCurrentData
                        RaiseEvent TextAdditionFailed(Me, EventArgs.Empty)
                    End If
                End If
            End If
        End Sub
    
        Private Sub bMark_Click(sender As System.Object, e As System.EventArgs) Handles bMark.Click
            RaiseEvent MarkClicked(Me, EventArgs.Empty)
        End Sub
    
        Private Sub bMark_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles bMark.MouseDown
            OnMouseDown(e)
        End Sub
    
        Private Sub bMark_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles bMark.MouseMove
            OnMouseMove(e)
        End Sub
    
        Private Sub bMark_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles bMark.MouseUp
            OnMouseUp(e)
        End Sub
    
        Private Sub bComment_Click(sender As System.Object, e As System.EventArgs) Handles bComment.Click
            RaiseEvent CommentClicked(Me, EventArgs.Empty)
        End Sub
    
        Private Sub bComment_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles bComment.MouseDown
            OnMouseDown(e)
        End Sub
    
        Private Sub bComment_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles bComment.MouseMove
            OnMouseMove(e)
        End Sub
    
        Private Sub bComment_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles bComment.MouseUp
            OnMouseUp(e)
        End Sub
    
        Private Sub tbEntry_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles tbEntry.MouseDown
            OnMouseDown(e)
        End Sub
    
        Private Sub tbEntry_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles tbEntry.MouseMove
            OnMouseMove(e)
        End Sub
    
        Private Sub tbEntry_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles tbEntry.MouseUp
            OnMouseUp(e)
        End Sub
    
    #End Region
    
    #Region "Other"
    
        Public Overrides Sub ResetText()
            MyBase.ResetText()
            Me.tbEntry.Text = String.Empty
        End Sub
    
        Private Sub SetSelected()
            If mSelected Then
                Me.BackColor = Drawing.Color.FromArgb(220, 255, 220)
            Else
                Me.BackColor = Nothing
            End If
        End Sub
    
    #End Region
    
    End Class
    My usual boring signature: Nothing

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Merging a Button and a Textbox

    Quote Originally Posted by Shaggy Hiker View Post
    ...I'm in a meeting, at the moment ... Since I have a bit of time...
    Great meeting, huh?

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,990

    Re: Merging a Button and a Textbox

    Ohhhhhh yeah!
    My usual boring signature: Nothing

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Merging a Button and a Textbox

    Reminds me of a meeting I was in, with some number of people around a long conference table with the conference phone setup with people at one or more remote sites. Since I was near the far end of the table and not directly involved with the discussion at the moment, I was checking out the site, and decided to reply to a thread. I was busily typing away when an IM popped-up on my screen. It was from the program manager at the other end of the table asking me to type quieter. I wasn't thinking about how the typing on my laptop was drumming through the table and picked up quite readily by the conference phone's extension mics, making it hard on the remote participants. One of them must have IM'd the program manager.

    At least it keeps you awake during those long meetings, which I figured looks like a good thing.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width