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.
LVL 2
ArticAsked:
Who is Participating?
 
Chuck YetterConnect With a Mentor Commented:
If you only want to display text and prevent it from receiving focus you could just use a label.
0
 
DhaestCommented:
>> remove the caret
What exactly do you mean with this ?

To prevent select: textBox1.ReadOnly = true;
0
 
Asim NazirCommented:
Better use Enabled = false. Readonly will still allow selection of text.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Asim NazirCommented:
textBox1.Enabled = false; to be exact.
0
 
ArticAuthor Commented:
asimnazir123:

I don't want to disable the text box as I want to be able to change the font colour.
0
 
ArticAuthor Commented:
Dhaest:

By caret I mean hide the blinking cursor that appears in the text box on focus or when the user selects text.
0
 
DhaestCommented:
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

0
 
Asim NazirCommented:
Otherwise, label is the best option - as suggest by Axshun
0
 
Asim NazirCommented:
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);
        }

0
 
ArticAuthor Commented:
Dhaest, asimnazir123

I shall try the functions and get back to you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.