列表框有问题... !! [英] List box trouble....!

查看:69
本文介绍了列表框有问题... !!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能帮忙?
我有一个带有一些名称的列表框(数据源在代码中分配)

listbox1
格里
汤姆
鲍勃
艾丽莎
麦当娜
德雷尼
梅尼
Geni

因此,当我单击一个仅向我显示列表框中最后四个成员的按钮时,该如何使用另一个列表或用于示例?该列表也应该像MessaggeBox

因此,在消息框或另一个仅是最后四个成员的列表框中,例如:
格里
汤姆
鲍勃
艾丽莎

查找前四个成员,我使用此代码

who can help please?
I have a listbox with some names (datasource is assigned in code)

listbox1
Gery
Tom
Bob
Alisa
Madona
Dreni
Meni
Geni

so how can i use another list or for exampl when I clik in a button that show me only last four members in listbox... that list should be also like MessaggeBox

so in messagge box or another listbox that be only the last four member like:
Gery
Tom
Bob
Alisa

to find first four member I use this code

string msg = string.Empty;
            for (int i = 0; i < 10; i++)
            {
                msg += (string)listBox3.Items[i] + "\n";
            }



最后四个呢......!

thnx



how about last four....!

thnx

推荐答案

该代码没有多大意义-我可以告诉您甚至没有执行它,因为您会得到索引超出范围的错误-您循环播放十次,并且列表框中有八个项目.

您是否意识到每个Collection都有一个Count参数?哪个告诉您集合中有多少项?而且字符串连接效率很低-您应该改用StringBuilder吗?
因此,要访问最后四个项目:
That code doesn''t make much sense - I can tell you haven''t even executed it, because you would get an Index out of range error - you are looping ten times, and have on eight items in your listbox.

You do realize that every Collection has a Count parameter? Which tells you how many items are in the collection? And that string concatenation is very inefficient - you should use a StringBuilder instead?
So to access the last four items:
int itemCount = listbox3.Items.Count;
StringBuilder sb = new StringBuilder();
for (int i = itemCount - 4; i < itemCount; i++)
   {
   sb.AppendFormat("{0}\n", listBox3.Items[i]);
   }
msg = sb.ToString();



使用这个
Hi,
Use this
string msg = string.Empty;
int count=0;
for (int i = listbox1.Items.Count-1; i >= 0; i--)
{
    msg += (string)listBox3.Items[i] + "\n";
    count++;
    if(count == 4)
       break;
}



最好..



All the best..


您的代码可能找到第一个十个(不是四个).

要访问最后四个,您需要类似
的内容
You code possibly finds the first ten (not four).

To access the last four you need something like
int f = listBox3.Items.Count - 4;
if (f < 0 ) f = 0;
for (int i = f; f < listBox3.Items.Count; i++)
{
  msg += (string) listBox3.Items[i] + "\n";
}




BTW listBox3是变量的较差名称.




BTW listBox3 is a poor name for a variable.


这篇关于列表框有问题... !!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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