C# 如何使文本粗体和红色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12599994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:51:25  来源:igfitidea点击:

how to make text BOLD and Red

c#c#-4.0

提问by lara400

How do I make the below exception return BOLD and text font RED? The wordings of "return "WMI Error";"

如何使以下异常返回 BOLD 和文本字体 RED?“返回“ WMI Error”的写法;

This is used later in the code for returning WMI parameters in a textbox...as shown below:

这在稍后用于在文本框中返回 WMI 参数的代码中使用...如下所示:

 private static string GetMOValue(ManagementObject mo, string name)
        {
            try
            {
                object result = mo[name];
                return result == null ? "" : result.ToString();
            }
            catch (Exception)
            {
                return "***WMI Error***";
            }
        }

        private void cmbHdd_SelectedIndexChanged(object sender, EventArgs e)
        {
            //try
            //{
                ManagementObjectSearcher mosDisks = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE Model = '" + cmbHdd.SelectedItem + "'");
                foreach (ManagementObject moDisk in mosDisks.Get())
                {
                    //try
                    //{
                    txtSystemName.Text = GetMOValue(moDisk, "systemname");
                    txtType.Text = GetMOValue(moDisk, "MediaType");
                    txtModel.Text = GetMOValue(moDisk, "Model");
                    txtFirmware.Text = GetMOValue(moDisk, "FirmwareRevision");
.....

采纳答案by Zdeslav Vojkovic

TextBox is not a web browser. It supports showing text with a single font - no decorations, colors, etc. You can change a font for the textbox in runtime.

TextBox 不是网络浏览器。它支持使用单一字体显示文本 - 没有装饰、颜色等。您可以在运行时更改文本框的字体。

You could handle the TextChangedevent of the textbox to achieve this, but this is not an example of nice code:

您可以处理TextChanged文本框的事件来实现这一点,但这不是一个很好的代码示例:

private void textBox1_TextChanged(object sender, EventArgs e)
{
  Font defaultFont = SystemFonts.DefaultFont;

  if(textBox1.Text.StartsWith("**") && textBox1.Text.EndsWith("**"))
  {
    textBox1.Font = new Font(defaultFont.FontFamily, defaultFont.Size, FontStyle.Bold);
    textBox1.ForeColor = Color.Red;
    // note: can't change text here as it will recursively trigger this handler
  }
  else
  {
    textBox1.Font = defaultFont;
    textBox1.ForeColor = SystemColors.ControlText;
  }
}

A better approach would be to have a simple method which will set the text and textbox attributes.

更好的方法是使用一个简单的方法来设置文本和文本框属性。

the soapbox: Please note that I consider this form of checking the text for ** characters to see if it is an error, extremely ugly and unreliable. What you should do is have something like

肥皂盒:请注意,我认为这种检查文本中的 ** 字符以查看它是否是错误的形式,非常丑陋且不可靠。你应该做的是有类似的东西

Font defaultFont = SystemFonts.DefaultFont;
try
{ 
   textBox1.Text = GetMOValue(...);
   textBox1.Font = defaultFont;
   textBox1.ForeColor = SystemColors.ControlText;
}
catch(Exception ex)
{ 
   textBox1.Text = "ERROR";
   textBox1.Font = new Font(defaultFont.FontFamily, defaultFont.Size, FontStyle.Bold);
   textBox1.ForeColor = Color.Red;
}
// the Font and ForeColor properties are repeatedly set in case that previous
// try had the different result (e.g. previous => error, current => OK, so 
// we need to reset the attributes of textbox

You can use RichTextBox, WebBrowser or some custom drawn control to support fancier formatting.

您可以使用 RichTextBox、WebBrowser 或一些自定义绘制控件来支持更漂亮的格式。

回答by cuongle

You should check whether the method return exception or not, if yes, change set font and color of textbox dynamically:

您应该检查该方法是否返回异常,如果是,则动态更改文本框的设置字体和颜色:

textBox.ForeColor = Color.Red;    
textBox.Font = new Font(textBox.Font, FontStyle.Bold);

回答by Jason craig

    private void btnBold_Click(object sender, EventArgs e)
    {
        this.rtxText.SelectionFont = new System.Drawing.Font(rtxText.Font.FontFamily,Convert.ToInt16(nudFontSize.Value), System.Drawing.FontStyle.Bold ^ this.rtxText.SelectionFont.Style);
    }

    //ITALICS CODING
    private void btnItalics_Click(object sender, EventArgs e)
    {
        this.rtxText.SelectionFont = new System.Drawing.Font(rtxText.Font.FontFamily, Convert.ToInt16(nudFontSize.Value), System.Drawing.FontStyle.Italic ^ this.rtxText.SelectionFont.Style);
    }

    //UNDERLINE CODING
    private void btnUnderline_Click(object sender, EventArgs e)
    {
        this.rtxText.SelectionFont = new System.Drawing.Font(rtxText.Font.FontFamily, Convert.ToInt16(nudFontSize.Value), System.Drawing.FontStyle.Underline ^ this.rtxText.SelectionFont.Style);
    }