删除所选列表框项的某些部分

时间:2014-05-08 11:22:30

标签: c# winforms listbox selecteditem

我想删除C#中所选列表框项的某些部分(字符串或整数值)。

如图所示,我在listbox1上显示了一些随机坐标。然后我随机选择这些点以通过ADD按钮添加Listbox2。我想在Listbox2上做出新的安排,这意味着它应该是1.选择点2.3.4.selected points ..(不是随机的3.rd 6.th 7.th ..)

其次我想转移到Listbox3的坐标(只是数字)。我该怎么办?

Image

添加所选坐标

 private void add()
        {
            int c = listBox1.Items.Count - 1;

            for (int i = c; i >= 0; i--)
            {
                if (listBox1.GetSelected(i))
                {
                    int a = listBox2.Items.Count + 1;
                    listBox2.Items.Add(a+" "+listBox1.Items[i]);
                 //   listBox1.Items.RemoveAt(i);

                }
            }
        }

1 个答案:

答案 0 :(得分:0)

问题不是很清楚,但我会尽力给你一个答案,就像我理解这个问题一样好。所以从我得到的你想要它,所以数字是ListBox2中的“1,2,3 ......”而不是随机一次,你也想要相同的结果,但只有ListBox3中的数字。

希望这个帮助至少这是我理解你要做的事情。顺便说一下,假设您的所有数据都基于您提供的示例!

//We are going to read the string from the first box and find the first 
//occurence of "." and get the rest of the string after that we are going to add it to ListBox2
        string for_lisbox2 = listBox1.Items[i].ToString();
        for_lisbox2 = for_lisbox2.Substring(for_lisbox2.IndexOf('.'));
        int current_index = listBox2.Items.Count + 1;
        listBox2.Items.Add(current_index + for_lisbox2);

//Then we do something similar but this time we are finding last empty space " " so we can find the start of the second coordinates and we mark it as Y
//then we do the same with X and finaly we add them to Lisbox3

        string for_listbox3 = for_lisbox2;

        //finding second set of coordinates by finding the last occurence of ' ' and using the result for a start index of Substring method this will return the second number
        string Y = for_listbox3.Substring(for_listbox3.LastIndexOf(' '));

        // this is just to remove the numbers that we just got in the previous line so they are not in your way for the next operation
        for_listbox3 = for_listbox3.Substring(0, for_listbox3.LastIndexOf(' '));

        //this is just like geting the other number
        string X = for_listbox3.Substring(for_listbox3.LastIndexOf(' '));

        for_listbox3 = X + " " + Y;//displaying the results in ListBox3 as "72 134"

        listBox3.Items.Add(for_listbox3);
.