如何在某些行中将相同的字符串添加到ListBox? [英] How do i add same string to a ListBox in some lines?

查看:122
本文介绍了如何在某些行中将相同的字符串添加到ListBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在表格的顶部,我有:

string[] data;
Color []color;

在构造函数中:

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

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

Then i have a function i load in the constructor:

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;
        }           
    }        
}

但是它仅一次添加到列表框一次. 我希望它像listbox1.Items.Add,然后将其从文本文件添加到listBox的所有行.

But its adding to the ListBox only once one lines. I want it to be like listbox1.Items.Add then its adding all the lines from the text file to the listBox.

然后用红色将线条着色:

Then im coloring the lines in Red:

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?

  1. how can i add all the lines from the StreamReader instead using Items.Add but using DataSource ?

我如何只用"Url:"一词涂成红色?我有这行:

How can i color in Red only the word "Url: " i have this line:

data =新字符串[1] {网址:" +令牌[0] +"---" +本地关键字:" +令牌[1]};

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

我希望它添加文本文件中的所有行,并且每行仅对单词"Url:"进行着色

And i want it to add all the lines from the text file and each line to color only the word "Url: "

推荐答案

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

First you should read all the lines and fill data and then bind it to the ListBox control. I suggest List rather than Array because you don't know how many lines in text you will have. Here is the code for that:

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:",因此您可以通过以下方式更改代码:

For the other part of the question, since you are always drawing the same word "Url:", than you can change your code this way:

编辑:这是我在短时间内能得到的最好的结果.我希望这是您要寻找的.请记住,绘图控件本身是一项棘手的事情,并且您需要具有WinForms和GDI +的大量经验.从您的帖子中,我可以得出结论,您是一个初学者,所以要当心,还有很多东西要学习.我对这些项目进行了双缓冲绘制,因此您不会遇到任何闪烁,并且这些项目不会因新的重绘而结石.但是,这不是最佳代码,但可以满足您的目的.如果需要在列表框中绘制选定的项目,建议您检查此链接.在这种情况下,根据状态,您可以Clear用不同颜色的位图,从而更改项目的背景色.

This is the best I could get in a short time. I hope this is what you are looking for. Remember that drawing controls yourself is a tricky business and that you need to have a lot of experience with both WinForms and GDI+. From your posts I could conclude that you are a beginner so watch out, there is a lot to learn. I made a double buffered drawing of the items so you will not experience any flickering and the items wont get bolder with new redraws. Nevertheless this is not optimal code but it will do the trick for your purpose. In case you need to draw selected items in the listbox, I advise you to check this link. In that case, depending on the state, you could Clear the bitmap with different colors, hence change the background color of the item.

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();
        }
    }

这篇关于如何在某些行中将相同的字符串添加到ListBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆