繁体   English   中英

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

[英]Delete some part of selected listbox item

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

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

其次,我想将坐标(仅数字)转移到Listbox3。 我能怎么做?

图片
(来源: hizliresim.com

添加所选坐标

 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);

                }
            }
        }

这个问题还不是很清楚,但是我会尽我所能回答给你一个答案。 所以从我得到的结果来看,您想使它成为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);
.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM