[英]How to add/move multiselected items from one listbox to another listbox?
[英]how add items to listbox from another listbox in order?
我有兩個列表框,我想按順序為從listbox1到listbox2的每個項目添加一個。 我的意思是我希望Listbox2顯示listbox1項目的上一個項目值。如何刪除或隱藏重復項,如下所示?
例如;
Listbox1 Listbox2
0 -
1 0
2 1
3 2
4 3
... ...
這是我的代碼;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DistanceExample
{
public partial class Form1 : Form
{
int i;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
i++;
foreach (var item in listBox1.Items)
{
listBox2.Items.Add(item);
}
listBox1.Items.Add(i);
}
}
}
只需使用Clear方法刪除listBox2
中的項目:
private void timer1_Tick(object sender, EventArgs e)
{
i++;
// remove old items
listBox2.Items.Clear();
foreach (var item in listBox1.Items)
{
listBox2.Items.Add(item);
}
listBox1.Items.Add(i);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.