Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.


C#
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.
Posted
Updated 8-Jun-16 8:36am

1 solution

Maybe you should have a look at this article: A Detailed Data Binding Tutorial
This shows how to use BindingList to synchronize grids and listboxes
A Detailed Data Binding Tutorial[^]
 
Share this answer
 
Comments
Robert_Carlson 8-Jun-16 14:39pm    
So instead using system.properties.Settings, I should just use SQL?
Thank you. I will give that a try.
RickZeeland 8-Jun-16 14:44pm    
It's not limited to just SQL, you can use about any object that has properties.
I often use a class with properties that are serialized to and from an XML file, this can be done in just a few lines of code :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900