首页 文章

如何在某些行中向ListBox添加相同的字符串?

提问于
浏览
1

在表格的顶部,我有:

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;
}
  • 如何使用Items.Add但使用DataSource添加StreamReader中的所有行?

  • 如何在红色中仅使用“Url:”这个词我有这条线:

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

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

2 回答

  • 0

    首先,您应该读取所有行并填充数据,然后将其绑定到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:”,因此您可以通过以下方式更改代码:

    EDIT: 这是我能在短时间内获得的最好成绩 . 我希望这就是你要找的东西 . 请记住,绘制控件本身是一件棘手的事情,你需要有很多WinForms和GDI的经验 . 从你的帖子我可以得出结论,你是一个初学者,所以要小心,有很多东西需要学习 . 我做了一个双缓冲的项目绘图,所以你不会遇到任何闪烁,项目不会得到 bolder 与新的重绘 . 尽管如此,这不是最佳代码,但它可以满足您的需求 . 如果您需要在列表框中绘制所选项目,我建议您检查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();
            }
        }
    
  • 2

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

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

相关问题