Link to home
Create AccountLog in
Avatar of Artic
ArticFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How do you remove the caret and prevent selection of text in a textbox?

How do you remove the caret and prevent selection of text for the same textbox?

Thanks in anticipation.
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

>> remove the caret
What exactly do you mean with this ?

To prevent select: textBox1.ReadOnly = true;
Better use Enabled = false. Readonly will still allow selection of text.
textBox1.Enabled = false; to be exact.
ASKER CERTIFIED SOLUTION
Avatar of Chuck Yetter
Chuck Yetter
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Artic

ASKER

asimnazir123:

I don't want to disable the text box as I want to be able to change the font colour.
Avatar of Artic

ASKER

Dhaest:

By caret I mean hide the blinking cursor that appears in the text box on focus or when the user selects text.
I just created this small function.
This will set the focus to the next control, so you can't select anything
private void textBox1_Enter(object sender, EventArgs e)
        {
            Control x = GetNextControl(sender as Control, true);
            if (x == null)
                if ((sender as Control).Parent.Controls[0] != sender as Control)
                    (sender as Control).Parent.Controls[0].Focus();
                else
                    (sender as Control).Parent.Controls[1].Focus();
            else
                x.Focus();
               
        }

Open in new window

Otherwise, label is the best option - as suggest by Axshun
To me this will do:

private void textBox1_Enter(object sender, EventArgs e)
        {
            textBox1.TabStop = false;
            this.SelectNextControl(this.GetNextControl(textBox1, true), true, false, true, true);
        }

Avatar of Artic

ASKER

Dhaest, asimnazir123

I shall try the functions and get back to you.