Is it possible to select text on a Windows form label?

白昼怎懂夜的黑 提交于 2019-12-12 07:31:55

问题


Is it possible to highlight/select part of text in a Windows Form label control? I know its possible with RTFtextbox control but that using that control would be overkill as I need to create many instances of the label.


回答1:


Is it possible to select text on a Windows form label? - NO (At least no easy way without overriding Label.Paint method)

You can easily change a TextBox for this purpose.

TextBox1.Text = "Hello, Select Me";
TextBox1.ReadOnly = true;
TextBox1.BorderStyle = 0;
TextBox1.BackColor = this.BackColor;
TextBox1.TabStop = false;

Don't believe? here is an example for you.




回答2:


Like Bala R answered:

"Use a TextBox with BorderStyle set to None and Readonly set to true and Backcolor to match that of the container.".

If the Text string is very long, and the Width of the TextBox is not enough to display all text, then you can set the Width property of the TextBox to display all it's Text.

If you need to know the correct number for Width, then you can use the MeasureString method of Graphics for this. You can get the instance from CreateGraphics() method of the Control (TextBox in this case).

First parameter is TextBox's Text, and second parameter is TextBox's Font. This function returns SizeF struct. You need only the Width property of it, convert it to integer with (int)size.Width or (int)Math.Round(size.Width).

Don't forget to call the Dispose() method of the graphics instance after, because you won't need it anymore.


You can write your own function that will do all this process:

static void SetText(TextBox textBox, string str)
{
   Graphics graphics = textBox.CreateGraphics();
   SizeF size = graphics.MeasureString(str, textBox.Font);
   graphics.Dispose();
   textBox.Width = (int)Math.Round(size.Width);
   textBox.Text = str;
}



回答3:


No, it's not possible to select text on the Windows Form Label. You can instead use a read only textbox for this.




回答4:


Double clicking on a label will copy the text to the clipboard. This is now the default behavior of Windows Forms labels.




回答5:


Use a TextBox with BorderStyle set to None and Readonly set to true and Backcolor to match that of the container.




回答6:


You will not be able to highlight part of the text on a label. However, you can use an image and set it to the Label.Image property if the text for these labels is static.




回答7:


I know this question is about selecting parts of the text of a label but I assume the text shall ultimately be placed on the clipboard.

So if you don't mind copying the whole text, just set a Click event on the label to copy its text to the clipboard:

myLabel.Click += new System.EventHandler(MyLabel_Click);

// ...

private void MyLabel_Click(object sender, EventArgs e)
{
   Clipboard.SetText(myLabel.Text);
}


来源:https://stackoverflow.com/questions/7748137/is-it-possible-to-select-text-on-a-windows-form-label

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!