我需要添加一个“slushbucket”(ServiceNow术语)到C#Windows窗体,如下图所示,但无法弄清楚它们实际调用了什么,因此不能看看他们是如何创建的。C#Windows从添加/删除列表
有谁知道他们叫什么,或者更好,但如何实现一个窗口窗体?
我需要它列出左侧Table1的值,然后当它们被添加到右侧列表并单击保存按钮时,这些值将被写入到Table2中。如果有的话,它还需要显示Table2的现有值。
我需要添加一个“slushbucket”(ServiceNow术语)到C#Windows窗体,如下图所示,但无法弄清楚它们实际调用了什么,因此不能看看他们是如何创建的。C#Windows从添加/删除列表
有谁知道他们叫什么,或者更好,但如何实现一个窗口窗体?
我需要它列出左侧Table1的值,然后当它们被添加到右侧列表并单击保存按钮时,这些值将被写入到Table2中。如果有的话,它还需要显示Table2的现有值。
假设这是形式的样子: Form1.Png 这里是实现你的想法如下一个简单的代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
for (int i = 6; i <= 10; i++)
{
listBox1.Items.Add("item" + i);
}
for (int i = 1; i <= 5; i++)
{
listBox2.Items.Add("item" + i);
}
}
private void btnSave_Click(object sender, EventArgs e)
{`
if (listBox2.Items.Count >0)
{
string message = "";
foreach (var item in listBox2.Items)
{
message += item + "\n";
}
MessageBox.Show(message);
}`
/// write here the code that save listbox1 and listbox2 items
/// to a text file or a database table and load this text file or
/// the database table inside these two listboxes you could write the
/// code that load data to the two list boxes in the constructor instead
/// of the tow for loops thats i've provided.
}
private void btnAdd_Click(object sender, EventArgs e)
{
try
{
if (!listBox2.Items.Contains(listBox1.SelectedItem))
{
listBox2.Items.Add(listBox1.SelectedItem);
listBox1.Items.Remove(listBox1.SelectedItem);
}
else
{
MessageBox.Show("Item already exists");
}
}
catch (ArgumentNullException exc)
{
MessageBox.Show("Nothing selected to add");
}
}
private void btnRemove_Click(object sender, EventArgs e)
{
try
{
if (!listBox1.Items.Contains(listBox2.SelectedItem))
{
listBox1.Items.Add(listBox2.SelectedItem);
listBox2.Items.Remove(listBox2.SelectedItem);
}
else
{
MessageBox.Show("Item already exists");
}
}
catch (ArgumentNullException exc)
{
MessageBox.Show("Nothing selected to remove");
}
}
}
希望这就是你要找的人:) 谢谢。
这是图像输出:
,你的代码如下。我希望这会对你有所帮助。
public partial class Form1 : Form
{
ArrayList Table1;
ArrayList Table2;
bool isSaved = false;
public Form1()
{
InitializeComponent();
Table1 = new ArrayList();
Table2 = new ArrayList();
Table1.Add("Value1");
Table1.Add("Value2");
Table1.Add("Value3");
Table1.Add("Value4");
Table1.Add("Value5");
}
private void populateListBox1() {
for (int i = 0; i < Table1.Count;i++)
{
listBox1.Items.Add(Table1[i]);
}
}
private void Form1_Load(object sender, EventArgs e)
{
populateListBox1();
}
private void refreshListBox1() {
listBox1.Items.Clear();
populateListBox1();
}
private void addToRight() {
try
{
listBox2.Items.Add(Table1[listBox1.SelectedIndex]);
Table1.RemoveAt(listBox1.SelectedIndex);
}
catch
{
MessageBox.Show("You have to select an item first !");
}
refreshListBox1();
}
private void removeFromRight(){
if (isSaved)
{
try
{
listBox1.Items.Add(Table2[listBox2.SelectedIndex]);
Table1.Add(Table2[listBox2.SelectedIndex]);
isSaved = false;
}
catch
{
MessageBox.Show("You have to select an item first !");
isSaved = true;
}
try
{
Table2.RemoveAt(listBox2.SelectedIndex);
listBox2.Items.RemoveAt(listBox2.SelectedIndex);
isSaved = false;
}
catch
{
if(listBox2.Items.Count==0)
MessageBox.Show("This list is an empty list");
isSaved = true;
}
}
else {
MessageBox.Show("You have to click save button first !");
}
}
private void saveToTable2() {
for (int i = 0; i < listBox2.Items.Count;i++)
{
Table2.Add(listBox2.Items[i].ToString());
}
MessageBox.Show("Saved !");
isSaved=true;
}
private void btn_add_Click(object sender, EventArgs e)
{
addToRight();
}
private void btn_save_Click(object sender, EventArgs e)
{
saveToTable2();
}
private void btn_remove_Click(object sender, EventArgs e)
{
removeFromRight();
}
}
}
只是这个代码自己使用两个ListBox或两个列表视图和一些按钮。 – NineBerry
创建一个新表单,为其添加2个列表框以及添加和删除按钮。尝试做一些编码(填写左边的列表框,...),如果你有一个具体的问题,你可以再次问这里。 – mnieto
它的写作非常微不足道,按钮处理程序只是从框中选择的值并将它们添加到另一边 – Sayse