列表框项目与其他列表框项目同步 [英] Listbox items in sync with other listbox items
问题描述
我有3个listBox,每个都有来自3个textBox的输入,我希望它们保持彼此同步。所以3个列表框是并排的,我希望每个中的项目相互关联,即使我删除,按字母顺序排列,我确实在此部分中添加了工作。如果我从groceryListBox中删除我也想要删除价格和数量。
如果我只能添加屏幕截图,我不确定我能解释得更好。
使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Windows.Forms;
使用 GroceryList.Properties;
命名空间 GroceryList
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}
私有 void addButton_Click( object sender,EventArgs e)
{
if (itemTextBox.Text == )
MessageBox.Show( 请输入要添加的项目);
else
groceryListBox.Items.Add(itemTextBox.Text);
itemTextBox.Clear();
countTextBox.Text = groceryListBox.Items.Count.ToString();
if (quanityTextBox.Text == )
quanityListBox.Items.Add( ----- ----------跨度>);
else
quanityListBox.Items.Add(quanityTextBox.Text);
quanityTextBox.Clear();
// countTextBox.Text = quanityListBox.Items.Count.ToString();
if (priceTextBox.Text == )
priceListBox.Items.Add( ------- -------------跨度>);
else
priceListBox.Items.Add(priceTextBox.Text);
priceTextBox.Clear();
// countTextBox.Text = priceListBox.Items.Count.ToString();
itemTextBox.Focus();
}
private void removeSingleButton_Click( object sender,EventArgs e)
{
if (groceryListBox.SelectedIndex!= -1)
{
while (groceryListBox.SelectedItems.Count!= 0 )
{
groceryListBox.Items.Remove(groceryListBox.SelectedItems [ 0 ]);
}
}
else
{
MessageBox.Show( 请选择要删除的项目);
}
}
私有 void saveButton_Click( object sender,EventArgs e)
{
Settings.Default [ List1] = groceryListBox.Items.ToString();
Settings.Default.Save();
}
private void Form1_Load( object sender,EventArgs e)
{
groceryListBox.Text = Settings.Default [ 列表1\" 跨度>]的ToString();
}
private void button1_Click( object sender,EventArgs e)
{
groceryListBox.Items.Clear();
}
private void button2_Click( object sender,EventArgs e)
{
groceryListBox.SelectedItems.Clear();
for ( int i = groceryListBox.Items.Count - 1 ; i > = 0 ; i--)
if (groceryListBox.Items [i] .ToString()。ToLower()。Equals(searchTextBox.Text.ToLower()))
groceryListBox.SetSelected (i, true );
}
private void colorButton_Click( object sender,EventArgs e)
{
groceryListBox.BackColor = Color.DeepSkyBlue;
}
private void containsToolStripMenuItem_Click( object sender,EventArgs e)
{
{
groceryListBox.SelectedItems.Clear();
for ( int i = groceryListBox.Items.Count - 1 ; i > = 0 ; i--)
if (groceryListBox.Items [i] .ToString()。ToLower()。Contains(searchTextBox.Text.ToLower()))
groceryListBox.SetSelected (i, true );
}
}
私有 void startsWithToolStripMenuItem_Click( object sender,EventArgs e)
{
{
groceryListBox.SelectedItems.Clear();
for ( int i = groceryListBox.Items.Count - 1 ; i > = 0 ; i--)
if (groceryListBox.Items [i] .ToString()。ToLower()。StartsWith(searchTextBox.Text.ToLower()))
groceryListBox.SetSelected (i, true );
}
}
private void editToolStripMenuItem_Click( object sender,EventArgs e)
{
editTextBox.Text = groceryListBox.SelectedItem.ToString();
}
private void pasteToolStripMenuItem_Click( object sender,EventArgs e)
{
editTextBox.Paste();
}
private void toolsToolStripMenuItem_Click( object sender,EventArgs e)
{
if (editTextBox.Text == )
MessageBox.Show( < span class =code-string>请输入要添加的项目);
else
groceryListBox.Items [groceryListBox.SelectedIndex] = editTextBox.Text;
editTextBox.Clear();
countTextBox.Text = groceryListBox.Items.Count.ToString();
}
}
}
我尝试了什么:
我尝试了add_Button的反向,但是甚至没有编译。
也许您应该看看这篇文章:详细的数据绑定教程
这显示了如何使用BindingList
来同步网格和列表框
详细的数据绑定教程 [ ^ ]
I have 3 listBox each have an input from 3 textBox, I want them to stay in sync with each other. so the 3 listBoxes are side by side, I want the items in each to correlate to each other even it I remove, alphabetize, I do have the add working in this part. If I remove from groceryListBox I want the price and quantity to be removed as well.
Not sure I can explain it any better, if I could only add screen shot.
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;
using GroceryList.Properties;
namespace GroceryList
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void addButton_Click(object sender, EventArgs e)
{
if (itemTextBox.Text == "")
MessageBox.Show("Please enter an item to add");
else
groceryListBox.Items.Add(itemTextBox.Text);
itemTextBox.Clear();
countTextBox.Text = groceryListBox.Items.Count.ToString();
if (quanityTextBox.Text == "")
quanityListBox.Items.Add("---------------");
else
quanityListBox.Items.Add(quanityTextBox.Text);
quanityTextBox.Clear();
// countTextBox.Text = quanityListBox.Items.Count.ToString();
if (priceTextBox.Text == "")
priceListBox.Items.Add("--------------------");
else
priceListBox.Items.Add(priceTextBox.Text);
priceTextBox.Clear();
//countTextBox.Text = priceListBox.Items.Count.ToString();
itemTextBox.Focus();
}
private void removeSingleButton_Click(object sender, EventArgs e)
{
if (groceryListBox.SelectedIndex != -1)
{
while(groceryListBox.SelectedItems.Count != 0)
{
groceryListBox.Items.Remove(groceryListBox.SelectedItems[0]);
}
}
else
{
MessageBox.Show("Please Select an item(s) to remove");
}
}
private void saveButton_Click(object sender, EventArgs e)
{
Settings.Default["List1"] = groceryListBox.Items.ToString();
Settings.Default.Save();
}
private void Form1_Load(object sender, EventArgs e)
{
groceryListBox.Text = Settings.Default["List1"].ToString();
}
private void button1_Click(object sender, EventArgs e)
{
groceryListBox.Items.Clear();
}
private void button2_Click(object sender, EventArgs e)
{
groceryListBox.SelectedItems.Clear();
for (int i = groceryListBox.Items.Count - 1; i >= 0; i--)
if (groceryListBox.Items[i].ToString().ToLower().Equals(searchTextBox.Text.ToLower()))
groceryListBox.SetSelected(i, true);
}
private void colorButton_Click(object sender, EventArgs e)
{
groceryListBox.BackColor = Color.DeepSkyBlue;
}
private void containsToolStripMenuItem_Click(object sender, EventArgs e)
{
{
groceryListBox.SelectedItems.Clear();
for (int i = groceryListBox.Items.Count - 1; i >= 0; i--)
if (groceryListBox.Items[i].ToString().ToLower().Contains(searchTextBox.Text.ToLower()))
groceryListBox.SetSelected(i, true);
}
}
private void startsWithToolStripMenuItem_Click(object sender, EventArgs e)
{
{
groceryListBox.SelectedItems.Clear();
for (int i = groceryListBox.Items.Count - 1; i >= 0; i--)
if (groceryListBox.Items[i].ToString().ToLower().StartsWith(searchTextBox.Text.ToLower()))
groceryListBox.SetSelected(i, true);
}
}
private void editToolStripMenuItem_Click(object sender, EventArgs e)
{
editTextBox.Text = groceryListBox.SelectedItem.ToString();
}
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
editTextBox.Paste();
}
private void toolsToolStripMenuItem_Click(object sender, EventArgs e)
{
if (editTextBox.Text == "")
MessageBox.Show("Please enter an item to add");
else
groceryListBox.Items[groceryListBox.SelectedIndex] = editTextBox.Text;
editTextBox.Clear();
countTextBox.Text = groceryListBox.Items.Count.ToString();
}
}
}
What I have tried:
I have tried the reverse of the add_Button, but that didn't even compile.
Maybe you should have a look at this article: A Detailed Data Binding Tutorial
This shows how to useBindingList
to synchronize grids and listboxes
A Detailed Data Binding Tutorial[^]
这篇关于列表框项目与其他列表框项目同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!