我正在尝试创建一个包含3个listBox的程序,我需要按字母顺序排列其中的一个但是其他2个列表框信息仍然需要匹配第一个listBox。截至目前,我没有任何代码可以显示,但我确实有我的添加项目代码,以便大致了解我在寻找什么。
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 (quanityListBox.Text == "")
// MessageBox.Show("Please enter an item to add");
//else
quanityListBox.Items.Add(quanityTextBox.Text);
quanityTextBox.Clear();
// countTextBox.Text = quanityListBox.Items.Count.ToString();
//if (priceListBox.Text == "")
// MessageBox.Show("Please enter an item to 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();
}
任何帮助将不胜感激。谢谢。
编辑....我想出了如何发布截图....
At least they allow me a link for now, I guess until my reputation goes up. :-)
这就是我想要它做的,如果我将一个项目添加到数量和价格的列表中,并且我在列表中有多个项目,我仍然希望数量和价格与该项目相匹配。
这可能也有帮助.....
With the items matching the price and quantity.
答案 0 :(得分:0)
如果我正确地阅读您的问题,您是否正在寻找一种在多个ListBox之间显示相同信息的简单方法?如果是这样,您可以尝试以下方法:
private List<string> testList = new List<string>();
private void BindTestListData()
{
listBox1.DataSource = null;
listBox1.DataSource = testList;
listBox2.DataSource = null;
listBox2.DataSource = testList;
}
private void button1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(InputTextBox.Text))
{
testList.Add(InputTextBox.Text);
InputTextBox.Clear();
BindTestListData();
}
}
将文本框中的所有文本都添加到列表中,而不是直接将其添加到ListBox中。然后将列表绑定到ListBox的数据源。
修改强>
抓我以前的样子。我想我现在更能理解你的问题了。尝试这样的事情:
private struct Item
{
public string Name { get; set; }
public string Quantity { get; set; }
public string Price { get; set; }
}
private List<Item> items = new List<Item>();
private void BindTestListData()
{
listBox1.DataSource = null;
listBox1.DataSource = items;
listBox1.DisplayMember = "Name";
listBox2.DataSource = null;
listBox2.DataSource = items;
listBox2.DisplayMember = "Quantity";
}
private void button1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(InputTextBox.Text))
{
Item newItem = new Item()
{
Name = InputTextBox.Text,
Quantity = "1",
Price = "$10"
};
items.Add(newItem);
InputTextBox.Clear();
BindTestListData();
}
}
显然,button1_click()方法中的数字只是占位符,但是这样的东西应该有效。