In the top of the Form i have:
string[] data;
Color []color;
In the constructor:
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;
}
}
}
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;
}
how can i add all the lines from the StreamReader instead using Items.Add but using DataSource ?
How can i color in Red only the word "Url: " i have this line:
data = new string[1] { "Url: " + tokens[0] + " --- " + "Localy KeyWord: " + tokens[1]};
And i want it to add all the lines from the text file and each line to color only the word "Url: "
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;
}
For the other part of the question, since you are always drawing the same word "Url:", than you can change your code this way:
EDIT: 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();
}
}
Set the data source outside of the while loop. Or add items to the listbox like this
listBox1.Items.Add("Url: " + tokens[0] + " --- " + "Localy KeyWord: " + tokens[1]);