views:

474

answers:

5

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.

+1  A: 

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.

BlueMonkMN
The only control I can find that allows text selection without a cursor is the WebBrowser control.
BlueMonkMN
A: 

I have done this previously, a couple of years back, I think I used this Win API call (but with a regular text box): http://www.dreamincode.net/forums/showtopic35107.htm

Paul Ruane
+2  A: 

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;
Samuel
A: 

You have the HideCaret function in User32.dll. Use it like this:

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

private void textBox_Enter(object sender, EventArgs e)
{
    HideCaret(textBox.Handle);
}

This will prevent the caret from showing when textbox has focus.

alexn
This doesn't work, it won't hide the Caret.
Samuel
A: 

One thing to consider is to go ahead and use a label, but then programmatically copy content (the Label's text) into the clipboard using:

Clipboard.SetText(yourLabel.Text);
Jay Riggs
I would HATE this as a "feature".
Samuel
Nice generalization. Many times I've found it useful to programmatically set the content of clipboard. Maybe the same is true about the OP's app - without more detail you or I can't say. I'm merely presenting this as an option.
Jay Riggs
The fact you would even suggest this as a option suggests you have zero experience in user interaction.
Samuel
You're wrong there. My experience building UIs for users of varying computer ability has shown me that you can't even count on a user with average ability to be able to select all the text in a TextBox and press Control-C to copy that text. Seriously, even with a Label to provide instructions. -->
Jay Riggs
(continued...)With my suggestion you can have a button (perhaps with the standard Windows Copy icon) next to the Label. When the user clicks it my code guarantees the entire Label contents gets into the clipboard. Nice and familiar to average users, no jerking around with TextBoxes.-->
Jay Riggs
(last part, I promise)Now, I don't know if there's something in the OP's requirements that make my suggestion inappropriate, but I DO know that nothing that you've said makes me think so.
Jay Riggs