【发布时间】:2016-06-06 16:34:46
【问题描述】:
我正在尝试制作一个包含 3 个列表框的程序,我需要将其中的 1 个按字母顺序排列,但其他 2 个列表框信息仍需要与第一个列表框匹配。到目前为止,我没有任何代码可以显示,但我确实有我的添加项目代码来大致了解我在寻找什么。
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.
【问题讨论】:
-
你到底想完成什么?
-
我想同步所有 3 个列表框,比如说我想按字母顺序排列 1 个列表,其他的应该遵循第一个。我有 3 个 listBox 并排制作不同的列,如果我添加它可以工作,但如果我删除它只会带走 1 并且所有内容都未对齐,listBoxes 数量和价格中的 2 个,所以我不能按字母顺序排列它们,此外他们不会与他们所属的项目对齐。上帝,我希望我说得通。 :-)
-
听起来您需要使用主键。当项目被选中或不在第一个列表中时,保存主键,然后您可以遍历其他两个列表并通过该键找到更改的项目。这有意义吗?
标签: c# listboxitems