英文:
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:
- X-200 Y+130 R0 FAUTO
- IZ+20 R0 FMAX
- X+200 Y+160 R0 FMAX
- IZ-20 R0 FMAX
- X-200 Y+160 R0 FAUTO
- IZ+20 R0 FMAX
- X+200 Y+190 R0 FMAX
- IZ-20 R0 FMAX
- X-200 Y+1-0 R0 FAUTO
- 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:
- X-200 Y+130 R0 FAUTO
- IZ+20 R0 FMAX
- X+200 Y+160 R0FMAX
- IZ-20 R0 FMAX
- X-200 Y+160 R0 FAUTO
- IZ+20 R0 FMAX
- X+200 Y+190 R0FMAX
- IZ-20 R0 FMAX
- X-200 Y+1-0 R0 FAUTO
- 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<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();
}
}
at the second time the if-clause is entered the queue will yield the second element: "IZ-20 R0 FMAX"
which will be appended again at the end. This will alternate the insertion elements
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论