【问题标题】:Highlight different lines from a textbox with different colors in C#.NET在 C#.NET 中用不同颜色突出显示文本框中的不同行
【发布时间】:2018-03-22 08:17:35
【问题描述】:

我有以下问题:在我的 C# 程序中,我有一个函数可以计算项目并将它们以 Windows 形式显示在 x/y 等文本框中。

我想突出显示整行

  • 如果 x = y 则为绿色
  • 如果 x != y 则为橙色
  • 如果为 0 / y 则为红色

例子:

  • 10/10 Items 应以绿色突出显示
  • 4/10 Items 应以橙色突出显示
  • 0/10 Items 应以红色突出显示

感谢您的帮助 :)

工作代码:
我在 oder 中使用了一个富文本框来突出显示某些行:

            Match search1 = Regex.Match(txt_result.Lines[i], @"(?<= wurde )(.*)(?= \/ )");
            Match search2 = Regex.Match(txt_result.Lines[i], @"(?<= \/ )(.*)(?= in )");
            if (search1.Value == search2.Value && search1.Value!="")
            {
                int c0 = txt_result.GetFirstCharIndexFromLine(i );
                int c1 = txt_result.GetFirstCharIndexFromLine(i + 1);
                if (c1 < 0) c1 = txt_result.Text.Length;
                txt_result.SelectionStart = c0;
                txt_result.SelectionLength = c1 - c0;
                if (txt_result.SelectedText.Contains(search1.Value))
                        txt_result.SelectionColor = Color.Green;

            }

【问题讨论】:

  • ASP.Net?吗?窗体?银光?
  • 你能分享你的C#代码吗?
  • @ZoharPeled C#.Net 文本框采用基本形式
  • @Saadi 我不知道如何开始
  • 控件错误,TextBox无法显示不同颜色的文本。改用 RichTextBox,更改 SelectionColor 属性(不是拼写错误)。请考虑使用 ListView,允许用户编辑此文本不太可能有用。

标签: c# textbox


【解决方案1】:

如果您使用的是 WPF,而不是 winform,请参阅 this solution


如果是Winform,使用下面的sn-p:

public class MyListBoxItem {
    public MyListBoxItem(Color c, string m) { 
        ItemColor = c; 
        Message = m;
    }
    public Color ItemColor { get; set; }
    public string Message { get; set; }
}


listBox1.Items.Add(new MyListBoxItem(Colors.Green, "10/10"));
listBox1.Items.Add(new MyListBoxItem(Colors.Red, "0/10"));

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    // Get the current item and cast it to MyListBoxItem
    MyListBoxItem item = listBox1.Items[e.Index] as MyListBoxItem; 
    if (item != null) 
    {
        e.Graphics.DrawString( // Draw the appropriate text in the ListBox
            item.Message, // The message linked to the item
            listBox1.Font, // Take the font from the listbox
            new SolidBrush(item.ItemColor), // Set the color 
            0, // X pixel coordinate
            e.Index * listBox1.ItemHeight // 
        );
    }
    else 
    {
         // The item isn't a MyListBoxItem, do something about it
    }
}

【讨论】:

  • 我在两行 Items.Add 中都出现 36 个错误,我做错了什么?
  • @ClemensSteinbauer 你必须在设计器部分调用你的列表“listBox1”......
  • @MaxB 我的列表还是我的 ListBox?
  • // listBox1
    this.listBox1.FormattingEnabled = true;
    this.listBox1.Location = new System.Drawing.Point(13, 13);
    this.listBox1. Name = "listBox1";
    this.listBox1.Size = new System.Drawing.Size(259, 173);
    this.listBox1.TabIndex = 0;

    这就是我在设计器中的代码
  • @ClemensSteinbauer 抱歉,我刚刚检查过了,问题是您不能在方法之外拥有这两行 (listBox1.Items.Add...)。我相信大卫只是在那里抚摸它们作为测试。将它们带到其他地方,例如 Main 函数或其他任何东西。
猜你喜欢
  • 2014-02-14
  • 1970-01-01
  • 1970-01-01
  • 2015-05-22
  • 1970-01-01
  • 2012-04-01
  • 2010-09-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多