簡體   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