本文介绍了列表框项目与其他列表框项目同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有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( 请输入要添加的项目); 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 TutorialThis shows how to use BindingList to synchronize grids and listboxesA Detailed Data Binding Tutorial[^] 这篇关于列表框项目与其他列表框项目同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-05 06:37