How to highlight the control when it gets focus?
Asked Answered
I

4

4

I can highlight the text in an individual MaskedTextBox when it gets focus using:

this.myTextBox.SelectAll();

But, I want to do it for all MaskedTextBox when a mouse click event occurs. Instead of adding 30 individual event method for each MaskedTextbox, I want to select all MaskedTextBox and have one event method to take care of it, ie:

private void MouseClickedForMaskedTextBox(object sender, MouseEventArgs e)
{
    this.ActiveControl.SelectAll();
}

But SelectAll is not available for this.ActiveControl. Is there a way to get around it?

Imbricate answered 15/3, 2017 at 19:10 Comment(1)
Create a usercontrol that inherits from textbox, put a static method or property on it, proceeed from there?Bashibazouk
T
4

sender will be the target of the event.

You could cast sender:

MaskedTextBox maskedTextBox = sender as MaskedTextBox;
if (maskedTextBox != null) { maskedTextBox.SelectAll(); }

Or in C# 7,

if (sender is MaskedTextBox maskedTextBox) 
{
    maskedTextBox.SelectAll();
} 

Another improvement is to use TextBoxBase and it will work with TextBox and RichTextBox as well.

Taiwan answered 15/3, 2017 at 19:12 Comment(3)
In 1st option, it errors "Cannot implicitly convert type 'System.Windows.Forms.MaskedTextBox' to 'bool'. In 2nd option, it causes 3 errors ") expected" at end of MaskedTextBox, "; expected" at end of maskedTextBox, and "} expected" at end of maskedTextBox.Imbricate
After playing around with your 1st option, I removed the "if" condition, and it works!!!! Thanks a lot!!!!Imbricate
@Imbricate i fixed the condition - my javascript was showing.Taiwan
A
1

Put the following code in the form's constructor:

        foreach (Control c in Controls)
        {
            if (c is TextBox)
            {
                TextBox tb = c as TextBox;
                tb.GotFocus += delegate { tb.SelectAll(); };
            }
        }
Amii answered 16/3, 2017 at 0:52 Comment(0)
B
0

Simply do that:

private void maskedTextBox1_Enter(object sender, EventArgs e)
{
   this.BeginInvoke((MethodInvoker) delegate() {
   maskedTextBox1.SelectAll();
   });
}
Bowser answered 28/7, 2019 at 15:0 Comment(0)
A
0

I found another way by creating or edit your User Control that inherits MaskedTextBox. In the designer you set true the property "OnEnterSelectAll".

public partial class MaskedTextBoxX : MaskedTextBox
{
    public MaskedTextBoxX()
    {
        InitializeComponent();
        Inicializar();
        }

    // ===============================
    // Campos Añadidos
    // ===============================

    public bool OnEnterSelectAll { get; set; } = false;


    // ===============================
    // Metodos
    // ===============================

    private void Inicializar()
    {
        // *** SELECCIONAR TODO el MarkedTextBox
        Click += delegate { if (OnEnterSelectAll) SelectAll(); };
    }
}
Aldarcy answered 31/10, 2022 at 20:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.