C# 使用交替方法将项目插入到 ListBox 中

huangapple go评论134阅读模式
英文:

C# Insert Item to ListBox with alternation method

问题

I have for example five items in a ListBox and all five are selected. Now I would like to add a new one to each marked item with the text "IZ+20" and "IZ-20" in alternation. It should look like that:

  1. X-200 Y+130 R0 FAUTO
  2. IZ+20 R0 FMAX
  3. X+200 Y+160 R0 FMAX
  4. IZ-20 R0 FMAX
  5. X-200 Y+160 R0 FAUTO
  6. IZ+20 R0 FMAX
  7. X+200 Y+190 R0 FMAX
  8. IZ-20 R0 FMAX
  9. X-200 Y+1-0 R0 FAUTO
  10. IZ+20 R0 FMAX

How can I make this? Thank you...

My Code:

for (int n = listBox1.Items.Count - 1; n >= 0; --n)
{
    string select = "X";
    if (listBox1.Items[n].ToString().Contains(select))
    {
        listBox1.SetSelected(n, true);
        listBox1.Items.Insert(listBox1.SelectedIndex, "IZ+20");
        //and now "IZ-20 R0 FMAX"???
        listBox1.ClearSelected();
    }
}
英文:

I have for example five items in a ListBox and all five are selected. Now I would like to add a new one to each marked item with the text "IZ+20" and "IZ-20" in alternation. It should look like that:

  1. X-200 Y+130 R0 FAUTO
  2. IZ+20 R0 FMAX
  3. X+200 Y+160 R0FMAX
  4. IZ-20 R0 FMAX
  5. X-200 Y+160 R0 FAUTO
  6. IZ+20 R0 FMAX
  7. X+200 Y+190 R0FMAX
  8. IZ-20 R0 FMAX
  9. X-200 Y+1-0 R0 FAUTO
  10. IZ+20 R0 FMAX

How can I make this? Thank you...

My Code:

for (int n = listBox1.Items.Count - 1; n >= 0; --n)
{
    string select = "X";
    if (listBox1.Items[n].ToString().Contains(select))
    {
        listBox1.SetSelected(n, true);
        listBox1.Items.Insert(listBox1.SelectedIndex, "IZ+20");
        //and now "IZ-20 R0 FMAX"???
        listBox1.ClearSelected();
    }
}

答案1

得分: 0

You could also use a Queue for that job, enque both items in the beginning, then deque insert and enque again the same item, this way they will rotate and you will have the other alternating item at the next insertion attempt

使用一个队列也可以完成这个任务,首先将两个项目入队,然后在插入后再出队并重新入队相同的项目,这样它们会轮换,下次插入尝试时会有另一个交替的项目。

using System.Collections.Generic;

Queue<string> insertionItems = new Queue<string>();
insertionItems.Enqueue("IZ+20 R0 FMAX");
insertionItems.Enqueue("IZ-20 R0 FMAX");

for (int n = listBox1.Items.Count - 1; n >= 0; --n)
{
    string select = "X";
    if (listBox1.Items[n].ToString().Contains(select))
    {
        listBox1.SetSelected(n, true);
        string item = insertionItems.Dequeue(); // removes first element
        listBox1.Items.Insert(listBox1.SelectedIndex, item);
        insertionItems.Enqueue(item); // appends first element at end
        listBox1.ClearSelected();
    }
}
       
在第二次进入if条件的时候,队列将产生第二个元素:`"IZ-20 R0 FMAX"`,然后再次追加到末尾。这将交替插入元素。
英文:

You could also use a Queue for that job, enque both items in the beginning, then deque insert and enque again the same item, this way they will rotate and you will have the other alternating item at the next insertion attempt

using System.Collections.Generic;

Queue&lt;string&gt; insertionItems = new Queue&lt;string&gt;();
insertionItems.Enqueue(&quot;IZ+20 R0 FMAX&quot;);
insertionItems.Enqueue(&quot;IZ-20 R0 FMAX&quot;);


for (int n = listBox1.Items.Count - 1; n &gt;= 0; --n)
{
    string select = &quot;X&quot;;
    if (listBox1.Items[n].ToString().Contains(select))
    {
        listBox1.SetSelected(n, true);
        string item = insertionItems.Dequeue(); // removes first element
        listBox1.Items.Insert(listBox1.SelectedIndex, item);
        insertionItems.Enqueue(item); // appends first element at end
        listBox1.ClearSelected();
    }
}

at the second time the if-clause is entered the queue will yield the second element: &quot;IZ-20 R0 FMAX&quot; which will be appended again at the end. This will alternate the insertion elements

huangapple
  • 本文由 发表于 2023年4月13日 16:47:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003447.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定