【问题标题】:C# ListBox syncing with each otherC# ListBox 相互同步
【发布时间】: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


【解决方案1】:

如果我没看错您的问题,您是否正在寻找一种在多个 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 的数据源。

编辑

刮掉我以前的东西。我想我现在更好地理解了你的问题。试试这样的:

    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() 方法中的数字只是占位符,但这样的东西应该可以工作。

【讨论】:

  • 看起来它可以工作,我是如何使用设置来保存列表的,我是stackoverflow的新手,所以我不知道如何添加屏幕截图。
  • 不需要截图。只需将代码复制并粘贴到评论窗口中即可。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-21
  • 2011-12-26
相关资源
最近更新 更多