Listview计数c# [英] Listview Counting c#

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

问题描述

大家好,

首先抱歉我的英语不好。

有没有办法在listwiev中计算?



我有一个带有两列ID和数字的列表视图,使用从文本框中搜索。

SEARCH BUTTON

Hello guys,
First of all sorry for my bad english.
Is there any way how to calculate in listwiev?

I have a list view with two columns, ID and Number, using searching from text box.
SEARCH BUTTON

foreach(ListViewItem ID in listView1.Items)

            {
                if (search.Text == ID.Text)

                {



ADD BUTTON


ADD BUTTON

ListViewItem ID;
                    ListViewItem.ListViewSubItem Num;
                    ID = new ListViewItem();
                    ID.Text = textBox1.Text;

                    Num = new ListViewItem.ListViewSubItem();
                    Num.Text = textBox2.Text;
                    ID.SubItems.Add(Num);
                    listView1.Items.Add(ID);





每个ID都有特定号码(列)



如果我发现了ID的数据,我可以用同一行的数字来计算。

例如

ID:Alex,号码:100

ID:John,数量:50



如果我在文本框中键入Alex,我怎么能用Alex号码计算?

非常感谢



Every ID have Specific number(column)

If i found mathes by ID how i can calculate with the number on the same row.
For example
ID: Alex , Number: 100
ID: John, Number: 50

If i type Alex to text box how i can calculate with Alex number?
Thanks so much

推荐答案

您可以轻松搜索列表框中的数字。

看看这个 [ ^ ]示例。
You can easily search the number within the listbox.
Have a look at this[^] example.


这里有一个简单的例子,说明如何在ListView中搜索匹配字符串的ListViewItem,以及如何获取匹配的SubItem,测试它是否可以转换为数字,并将其转换为number。
Here's a simple illustration of how to search a ListView for a ListViewItem that matches a string, and how to get the match's SubItem, test to see if it is convertible to a number, and convert it to a number.
private void button1_Click(object sender, EventArgs e)
{
    ListViewItem foundItem = listView1.FindItemWithText(textBox1.Text, false, 0, false);

    // found a match ?
    if (foundItem != null)
    {
        // get the match's first sub-item
        string numberCandidate = foundItem.SubItems[1].Text;

        int idCandidate;

        // do we have an integer ?
        if (Int32.TryParse(numberCandidate, out idCandidate))
        {
            // there's now a valid integer in 'idCandidate
            MessageBox.Show("ID #" + idCandidate.ToString());
        }
    }
}

如果您专注于了解此代码中发生的情况,您应该能够对其进行调整以满足您的需求。



注意:'只有提供字符串参数的FindItemText将返回部分匹配:,如果您搜索Al,并且您的ListView具有标题为Alex和Alice的项目它将返回ListViewItems集合中的第一个条目。在上面的代码中,我们向'FindItemText提供可选参数(最后一个参数)关闭查找部分匹配。



注意:'FindItemText将忽略您的搜索字符串是大写还是小写。

If you focus on understanding what happens in this code, you should be able to adapt it to meet your needs.

Note: 'FindItemText with only a string parameter supplied will return "partial matches:" if you searched on "Al, and your ListView had Items titled "Alex," and "Alice," it would return whichever entry came first in the ListViewItems collection. In the code above we supply the optional argument (last argument) to 'FindItemText that "turns off" finding partial-matches.

Note: 'FindItemText will ignore whether your search string is upper- or lower- case.


这篇关于Listview计数c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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