Click here to Skip to main content
16,020,249 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,
I have a listBox , i set the property DrawMode to OwnerDrawVariable and i change the height of the item according to the number of lines in the item the problem is that the last item is not displayed completely , can somebody help me please :s

C#
        StreamReader fileRead = new StreamReader(myFilePath);

        string line = fileRead.ReadLine();

        string LookFor = textBox3.Text;

        string bloc;

        while (line != null)
        {
            if (line.Contains(LookFor))
            {
                bloc = line + "\r\n";

                line = fileRead.ReadLine();

                 do
                 {
                     bloc = bloc + line + "\r\n";

                     line = fileRead.ReadLine();
                 }
                 while ((line != null) && !((line.EndsWith("Something")) ||(line.EndsWith("Somethingelse"))));

                 if ((line != null) && ((line.EndsWith("Something")) || (line.EndsWith("Somethingelse"))))
                 {
                     bloc = bloc + line + "\r\n" ;

                     listBox1.Items.Add(bloc);

                 }

             }
                line = fileRead.ReadLine();
            }


        }

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    e.DrawBackground();
    e.DrawFocusRectangle();
    e.Graphics.DrawString(
         (string)listBox1.Items[e.Index],
         e.Font,
         new SolidBrush(e.ForeColor),
         e.Bounds);
}

private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
    e.ItemHeight = 13 * GetLinesNumber((string)listBox1.Items[e.Index]);
}

private int GetLinesNumber(string text)
{
    int count = 1;
    int pos = 0;
    while ((pos = text.IndexOf("\r\n", pos)) != -1) { count++; pos += 2; }
    return count;
}




what this is suppose to do is to look for "lookFor" in a text file if it finds it , it should put everething that is between the line that contains "lookFor" and the line that ends with "Somthing" or the one that ends with "Somthingelse" in a bloc of lines that will be an item of the listbox
now the number of lines of this items is not fixed so the itemheught varies from an item to anthor that's why i used the MeasureItem but the problem i'm facin is that the last item of this listBox is not displayed correctely ther is some lines missing
hope that's clear now
thank you
Posted
Updated 3-Apr-13 1:41am
v2
Comments
Richard MacCutchan 3-Apr-13 7:07am    
Help with what? We have no idea what your code is doing or why it may not be working correctly.
ZurdoDev 3-Apr-13 7:16am    
Please post relevant code.

1 solution

There is no simple answer...

Please, debug your code and check for "errors". Probably the part of code where you read and load data into ListBox has errors in algorithm. You need to fix it by yourself.

[EDIT #1]
Please, follow these links:
ListBox Class[^]
Code: Adding Items to a ListBox[^]
How to: Add and Clear Items in a ListBox[^]

CP articles:
Multi Column List Box in C#[^]
An auto-resize C# ListBox[^]


[EDIT #2]
Another useful resources:
An editable multi-line listbox for .NET[^]
listbox-with-variable-height-items[^]
 
Share this answer
 
v3
Comments
Member 9937929 3-Apr-13 18:05pm    
have already checked , i tried to display the last item il a label and it worked properly but whene i use the listBox it doesn't
i tried with the itemHeight itemWidth the drawitem everything
it's working with a RichTextBox but not with the listbox an i just don't know why
Maciej Los 3-Apr-13 18:12pm    
Please, see my answer after update. I've added few links. I think the last one is very interesting ;)
Member 9937929 3-Apr-13 18:20pm    
i've already visited this links and the items are added in the listBox i know it beceause whene i displayed the content of the listBox in a lablel everething was good something is wrong withe the height or width or don't know
Maciej Los 3-Apr-13 18:17pm    
Did you try to set ColumnWidth[^], IntegralHeight[^]?
Member 9937929 3-Apr-13 18:22pm    
no i'll try it right know thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900