【问题标题】:How do i add same string to a ListBox in some lines?如何在某些行中将相同的字符串添加到 ListBox?
【发布时间】:2012-10-17 14:29:30
【问题描述】:

在表格的顶部我有:

string[] data;
Color []color;

在构造函数中:

color = new Color[1] { Color.Red};

然后我在构造函数中加载了一个函数:

private void ListBoxLoadKeys(Dictionary<string,List<string>> dictionary, 
    string FileName)
{
    string line = System.String.Empty;
    using (StreamReader sr = new StreamReader(keywords))
    {
        while ((line = sr.ReadLine()) != null)
        {
            int i = line.Count();
            tokens = line.Split(',');
            dictionary.Add(tokens[0], tokens.Skip(1).ToList());
            // listBox1.Items.Add("Url: " + tokens[0] + " --- " 
                + "Localy KeyWord: " + tokens[1]);     
            data = new string[1] { "Url: " + tokens[0] + " --- " 
                + "Localy KeyWord: " + tokens[1]};
            listBox1.DataSource = data;
        }           
    }        
}

但它只添加到 ListBox 一行中。 我希望它像 listbox1.Items.Add 然后将文本文件中的所有行添加到 listBox。

然后我将线条涂成红色:

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    e.DrawFocusRectangle();
    e.Graphics.DrawString(data[e.Index], new Font(FontFamily.GenericSansSerif, 8,
        FontStyle.Regular),new SolidBrush(color[e.Index]), e.Bounds);
}

private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
    e.ItemHeight = 16;
}
  1. 如何添加 StreamReader 中的所有行,而不是使用 Items.Add 但使用 DataSource ?

  2. 我怎样才能只用“Url:”这个词用红色着色我有这一行:

    data = new string[1] { "Url: " + tokens[0] + " --- " + "Localy KeyWord: " + tokens[1]};

我希望它添加文本文件中的所有行,并且每行只为单词“Url:”着色

【问题讨论】:

    标签: c# winforms listbox


    【解决方案1】:

    首先您应该读取所​​有行并填充数据,然后将其绑定到 ListBox 控件。我建议使用 List 而不是 Array,因为您不知道您将拥有多少行文本。这是代码:

    List<string> data = new List<string>();
    private void ListBoxLoadKeys(Dictionary<string,List<string>> dictionary, string FileName)
            {
    
                   string line = System.String.Empty;
    
                   using (StreamReader sr = new StreamReader(keywords))
                   {
                       while ((line = sr.ReadLine()) != null)
                       {
                           int i = line.Count();
                           tokens = line.Split(',');
                           dictionary.Add(tokens[0], tokens.Skip(1).ToList());
                          // listBox1.Items.Add("Url: " + tokens[0] + " --- " + "Localy KeyWord: " + tokens[1]);
                           data.Add("Url: " + tokens[0] + " --- " + "Localy KeyWord: " + tokens[1]);
    
                       }
                   }
                   listBox1.DataSource = data;
    }
    

    对于问题的另一部分,由于您总是绘制相同的单词“Url:”,因此您可以通过这种方式更改代码:

    编辑:这是我能在短时间内得到的最好结果。我希望这就是你要找的。请记住,自己绘制控件是一项棘手的工作,并且您需要在 WinForms 和 GDI+ 方面拥有丰富的经验。从你的帖子我可以得出结论,你是一个初学者,所以要小心,有很多东西要学。我对项目进行了双缓冲绘图,因此您不会遇到任何闪烁,并且项目不会在重新绘制时变得加粗。尽管如此,这不是最佳代码,但它可以达到您的目的。如果您需要在列表框中绘制选定的项目,我建议您检查此link。在这种情况下,根据状态,您可以Clear 具有不同颜色的位图,从而更改项目的背景颜色。

    private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index >= 0 && e.Index < data.Count)
            {
                e.DrawFocusRectangle();
                Bitmap bmp = new Bitmap(e.Bounds.Width, e.Bounds.Height);
                Graphics g = Graphics.FromImage(bmp);
                g.Clear(listView1.BackColor);
                string url = data[e.Index].Substring(0, 4);
                Font f = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular);
                SizeF size = e.Graphics.MeasureString(url, f);
                RectangleF urlRectF = new RectangleF(e.Bounds.X, 0, size.Width, e.Bounds.Height);
                g.DrawString(url, f, new SolidBrush(Color.Red), urlRectF); 
                RectangleF restRectF = new RectangleF(e.Bounds.X + size.Width, 0, e.Bounds.Width - size.Width, e.Bounds.Height);
                g.DrawString(data[e.Index].Substring(4), f, new SolidBrush(Color.Green), restRectF);
                e.Graphics.DrawImage(bmp,e.Bounds);
                g.Dispose();
            }
        }
    

    【讨论】:

    • Nick 尝试了您的代码,没有任何例外,但我在 listBox 中看不到任何内容。
    • 尼克不,我没有看到任何 listBox1 是空的。
    • 尼克但使用断点我看到数据包含 6 个项目,并且在 listBox1.DataSource = data; 之后我看到 listBox1 和 DataSource 也包含 6 个项目。
    • Nick ok 将其移除到函数之外,将其添加到表单顶部。现在我在 listBox1 中看到一个 Url: 红色,但仅此而已,我看不到该行的其余部分: data.Add("Url: " + tokens[0] + " --- " + "Localy KeyWord: " + tokens [1]);我没有看到令牌[0] --- localy 关键字:而不是令牌[1] 另一件事我只看到一个网址:而数据包含 6 个项目。
    • 尼克现在我看到了在将 DrawItem 事件代码更改为你的之后的所有行,但是它把所有行都涂成红色,而不仅仅是“Url:”(包括一个空格 Url:)顺便说一句:我想要当它可以工作时着色,每行的结尾也是令牌[1]
    【解决方案2】:

    在 while 循环之外设置数据源。或者像这样将项目添加到列表框

    listBox1.Items.Add("Url: " + tokens[0] + " --- " + "Localy KeyWord: " + tokens[1]);
    

    【讨论】:

    • 标记我在行 listBox1.DataSource = data; 上使用了一个断点并看到数据包含 6 个项目,并且在前进时我看到 listBox1.Source 也包含 6 个项目,所以我猜问题可能在 DrawItem 事件中?
    猜你喜欢
    • 1970-01-01
    • 2013-06-10
    • 2016-05-01
    • 2022-08-07
    • 1970-01-01
    • 2012-02-19
    • 2019-03-17
    • 1970-01-01
    • 2021-12-05
    相关资源
    最近更新 更多