-2

如何使以下异常返回 BOLD 和文本字体 RED?"return " WMI Error ";"的措辞

这将在后面的代码中用于在文本框中返回 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");
.....
4

3 回答 3

2

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

您可以处理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;
  }
}

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

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

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

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

于 2012-09-26T10:50:52.497 回答
2

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

textBox.ForeColor = Color.Red;    
textBox.Font = new Font(textBox.Font, FontStyle.Bold);
于 2012-09-26T11:00:59.000 回答
-1
    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);
    }
于 2018-12-06T10:46:01.133 回答