VB.net global actions

Viewed 29

I am writing an application in VB.net (using Visual Studio 2022) which has a lot of text boxes. I am wondering if there is a way to write a routine so any time any of the textboxes have the "enter" action called it will highlight the text contained in the textbox, as opposed to writing the code for each individual textbox "enter" routine?

1 Answers

Assuming that "have the "enter" action called" means when it receives focus, the simplest option would be to create your own custom control that includes that functionality, then use that control instead of regular TextBoxes. Creating custom controls is pretty simple when you want to add to an existing control rather than start from srcatch.

Public Class TextBoxEx
    Inherits TextBox

    Protected Overrides Sub OnEnter(e As EventArgs)
        MyBase.OnEnter(e)
        SelectAll()
    End Sub

End Class

Once you have that class in your project, it's just a matter replacing existing TextBox refrerences in the designer code with that class. If your project targets .NET Framework, you'll have to Show All Files in the Soluion Explorer to access the designer code file. The designer code for a form with a TextBox will look something like this:

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form 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.TextBox1 = New System.Windows.Forms.TextBox()
        Me.SuspendLayout()
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(12, 12)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(100, 20)
        Me.TextBox1.TabIndex = 0
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(800, 450)
        Me.Controls.Add(Me.TextBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

    Friend WithEvents TextBox1 As TextBox
End Class

You just need to replace this:

Friend WithEvents TextBox1 As TextBox

with this:

Friend WithEvents TextBox1 As TextBoxEx

and this:

Me.TextBox1 = New System.Windows.Forms.TextBox()

with this:

Me.TextBox1 = New TextBoxEx()

You can even use the Find & Replace functionality in VS to find all the instances you need to replace. Once that's done, everything else is automatic.

Just be sure to have backed up your code before messing with the designer files, in case you break them. Of course, you should be using source control so you should just be able to undo changes to go back to your last good code.

Related