listBox的问题,不显示整个项目 [英] issue with listBox , doesn't display the whole item

查看:70
本文介绍了listBox的问题,不显示整个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个listBox,我将属性DrawMode设置为OwnerDrawVariable,我根据项目中的行数更改项目的高度问题是最后一项是没有完全显示,有人可以帮助我:s



 StreamReader fileRead =  new  StreamReader(myFilePath); 

string line = fileRead.ReadLine();

string LookFor = textBox3.Text;

string bloc;

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

line = fileRead.ReadLine();

do
{
bloc = bloc + line + \\\\ 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 + \\\\ 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(()listBox1.Items [e.Index]);
}

private int GetLinesNumber( string text)
{
int count = 1 < /跨度>;
int pos = 0 ;
while ((pos = text.IndexOf( \\ n,pos))!= -1){count ++; pos + = 2 ; }
return count;
}









这是什么我想要做的是在文本文件中查找lookFor,如果它找到它,它应该放在包含lookFor的行和以Somthing结尾的行或以Somthing结尾的行之间的东西。 Somthingelse在一排线条中将成为列表框的一个项目

现在这些项目的行数不固定所以项目因项目而异,这就是我使用的原因MeasureItem,但我遇到的问题是这个listBox的最后一项没有显示,因为缺少一些线条

希望'现在清楚

谢谢

解决方案

没有简单的答案......



请调试你的代码并检查为错误。可能是您在ListBox中读取和加载数据的代码部分在算法中存在错误。你需要自己解决。





请按照以下链接:

ListBox类 [ ^ ]

代码:向列表框添加项目 [ ^ ]

如何:在列表框中添加和清除项目 [ ^ ]



CP文章:

C#中的多列列表框 [<小时ef =http://www.codeproject.com/Articles/2377/Multi-Column-List-Box-in-Ctarget =_ blanktitle =New Window> ^ ]

自动调整大小C#ListBox [ ^ ]







另一个有用的资源:

一个可编辑的.NET多行列表框 [ ^ ]

< a href =http://mariusbancila.ro/blog/2008/01/25/c-listbox-with-variable-height-items/> listbox-with-variable-height-items [ ^ ]

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

        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

解决方案

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[^]


这篇关于listBox的问题,不显示整个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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