Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlighting labels Windows Forms

Is there any way to make a label on a .NET Windows form to be highlightable to allow for the text to be copied. I have attempted to do this with a text box that was made to look like a label, but this results in a flashing cursor.

like image 414
cweston Avatar asked Mar 31 '09 19:03

cweston


3 Answers

I think this is pretty darn close:

textBox.BackColor = System.Drawing.SystemColors.Control;
textBox.BorderStyle = System.Windows.Forms.BorderStyle.None;
textBox.ReadOnly = true;
textBox.Text = "This is selectable text";
textBox.MouseUp += new MouseEventHandler(
                          delegate(object sender, MouseEventArgs e)
                             { HideCaret((sender as Control).Handle); });

[DllImport("User32.dll")]
static extern Boolean HideCaret(IntPtr hWnd);

And if you need it to span more than one line:

textBox.Multiline = true;
like image 99
Samuel Avatar answered Nov 04 '22 06:11

Samuel


If you want it to be a predictable, well behaved and standard control with all the keyboard and shortcut support you simply need a textbox. And then the flashing cursor is a normal helpful feature, why fight it?

like image 35
Henk Holterman Avatar answered Nov 04 '22 08:11

Henk Holterman


It's not unusual for selectable static text to show a flashing cursor. If you get the properties of any file in Windows Explorer and select any data in that window, you'll also see a flashing cursor.

like image 26
BlueMonkMN Avatar answered Nov 04 '22 07:11

BlueMonkMN