我想将 text2 显示为粗体字
public partial class Form2 : Form
{
public Form2(string text1, string text2, string text3, double text4,string t)
{
InitializeComponent();
label1.Text = text1;
label2.Text = "On " + text2 +
" Or When Your Vehicle Runs " + text3 + " KM. ";
label3.Text ="Today "+ t +
" and It Reminds You " + text4 +" Days Earlir.";
}
}
答案 0 :(得分:1)
这是一个轻量级的Label类,支持强调测试。
修改:访问CodeReview后,我更新了我的回答。它现在更强大了。它仍然使用Label子类。您可以创建如下强文本:
This is *bold* and this is an \* asterisk.
你要写的问题:
label2.Text = "On *" + text2 + "* Or When Your Vehicle Runs " + text3 + " KM. ";
只需将类添加到项目中,调整其命名空间,编译,从工具箱中拖动并更改其扩展属性,如普通Label
。
您可以将StrongColor
和StrongFont
设置为您喜欢的任何内容。默认值为ControlText
和Label.FontStyle.Bold
。
您可以将格式字符串Splitter
从星号(*
)更改为anthing,然后您可以通过' \
'或者你想要的任何Escape
字符串,因为它们都是属性。
FmtLabel
将自动调整其宽度,仅用于一行文本。
您可能需要查看扩展版本here on CodeReview。在不到200行的代码中它仍然相当小,将支持多行,最多10个字体甚至多个交互链接。 (即将发布的第2版)
using System;
using System.Drawing;
using System.Windows.Forms;
namespace yourNamespace
{
public class FmtLabel : Label
{
public FmtLabel()
{
this.Paint += FmtLabel_Paint;
Escape = @"\";
Splitter = "*";
AutoSize = true;
}
public string Splitter { get; set; }
public string Escape { get; set; }
public Font StrongFont { get; set; }
public Color StrongColor { get; set; }
private SolidBrush textBrush, backBrush, strongBrush;
protected override void OnPaint(PaintEventArgs e) { FmtLabel_Paint(this, e); }
public void FmtLabel_Paint(object sender, PaintEventArgs e)
{
if (StrongColor == Color.Empty) StrongColor = SystemColors.ControlText;
textBrush = new SolidBrush(ForeColor);
backBrush = new SolidBrush(BackColor);
strongBrush = new SolidBrush(StrongColor);
e.Graphics.FillRectangle(backBrush, this.ClientRectangle);
if (this.Text.Length <= 0) return;
if (Splitter == null) Splitter = "*";
int strongStart = Text.StartsWith(Splitter) ? 0 : 1;
StringFormat SF = new StringFormat(StringFormat.GenericTypographic)
{ FormatFlags = StringFormatFlags.MeasureTrailingSpaces };
float x = 0f; // tracks the write pointer
string text_ = escapes(this.Text, true);
string[] parts = text_.Split(new string[] { Splitter },
StringSplitOptions.RemoveEmptyEntries);
// plain and strong text will alternate
for (int i = 0; i < parts.Length; i++)
{
string p = escapes(parts[i], false);
if (i % 2 == strongStart)
{
e.Graphics.DrawString(p, StrongFont, textBrush, new Point((int)x, 0));
x += e.Graphics.MeasureString(p, StrongFont, Point.Empty, SF).Width + 2;
}
else
{
e.Graphics.DrawString(p, this.Font, textBrush, new Point((int)x, 0));
x += e.Graphics.MeasureString(p, this.Font, Point.Empty, SF).Width + 2;
}
}
this.Width = (int)x;
textBrush.Dispose();
backBrush.Dispose();
strongBrush.Dispose();
}
string escapes(string input, bool escape)
{
if (escape) return input.Replace(Escape + Splitter, ((char)1).ToString());
else return input.Replace(((char)1).ToString(), Splitter); // unescape
}
}
}
答案 1 :(得分:0)
WinForms不允许这样做。
但是
您需要将文本放入RichTextBox,并在段落中将该名称作为单独的Run运行,如本示例中的MSDN所示:
// Create a Run of plain text and some bold text.
Run myRun1 = new Run();
myRun1.Text = "A RichTextBox with ";
Bold myBold = new Bold();
myBold.Inlines.Add("initial content ");
Run myRun2 = new Run();
myRun2.Text = "in it.";
// Create a paragraph and add the Run and Bold to it.
Paragraph myParagraph = new Paragraph();
myParagraph.Inlines.Add(myRun1);
myParagraph.Inlines.Add(myBold);
myParagraph.Inlines.Add(myRun2);
// Add the paragraph to the RichTextBox.
MyRTB.Blocks.Add(myParagraph);
答案 2 :(得分:0)
您无法直接在Windows表单上执行此操作。
试试这个
http://www.codeproject.com/Articles/31237/GMarkupLabel-A-C-Windows-Forms-control-to-display