I have a question.

I have a listbox. In it is a list of cars, and another listbox has a list of customers.

On another form, I have combox boxes. How do I write the data from the list boxes into the combo boxes, so that whatever data is in the listboes will display in the combo boxes?

I did a simple example of how populate and remove items (selected items) from comboBox and listBox:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Nov23TextCombo
{
    public partial class Form1 : Form
    {
        //
        //ADD LISTBOX AND COMBOBOX ON A FORM!
        //AND TWO EVENTS:
        //1. comboBox1_SelectedIndexChanged
        //2. listBox1_SelectedIndexChanged
        //
        public Form1()
        {
            InitializeComponent();
            PopulatingComboBox();
            PopultingListBox();
        }

        private void PopulatingComboBox()
        {
            this.comboBox1.Items.Add("Single item");
            string[] array = new string[] { "item 1", "item 2", "item 3" };
            for (int i = 0; i < array.Length; i++)
            {
                this.comboBox1.Items.Add(array[i]);
            }
        }

        private void RemovingSelectedItemComboBox()
        {
            if (this.comboBox1.SelectedIndex > -1)
            {
                int itemIndex = this.comboBox1.SelectedIndex;
                this.comboBox1.Items.RemoveAt(itemIndex);
            }
        }

        private void PopultingListBox()
        {
            this.listBox1.Items.Add("Single item");
            string[] array = new string[] { "item 1", "item 2", "item 3" };
            for (int i = 0; i < array.Length; i++)
            {
                this.listBox1.Items.Add(array[i]);
            }
        }

        private void RemovingSelectedItemListBox()
        {
            if (this.listBox1.SelectedIndex > -1)
            {
                int itemIndex = this.listBox1.SelectedIndex;
                this.listBox1.Items.RemoveAt(itemIndex);
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.comboBox1.SelectedIndex > -1)
            {
                if (DialogResult.No == MessageBox.Show("Do you want to remove " + this.comboBox1.Text + " from comboBox?",
                    "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                    return;
                else
                    RemovingSelectedItemComboBox();
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listBox1.SelectedIndex > -1)
            {
                if (DialogResult.No == MessageBox.Show("Do you want to remove " + this.listBox1.Text + " from listBox?",
                    "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                    return;
                else
                    RemovingSelectedItemListBox();
            }
        }
    }
}

I have a question.

I have a listbox. In it is a list of cars, and another listbox has a list of customers.

On another form, I have combox boxes. How do I write the data from the list boxes into the combo boxes, so that whatever data is in the listboes will display in the combo boxes?

Here is your solution:

//FORM1:
 public partial class Form1 : Form
    {
        Form2 form2;
        List<string> list;

        public Form1()
        {
            InitializeComponent();
            form2 = new Form2();
            list = new List<string>();
        }


        private void button1_Click(object sender, EventArgs e)
        {
             if (!FormOpened("Form2"))
                {
                    form2 = new Form2();
                    form2.Show(this);
                }
            if (this.listBox1.SelectedIndex > -1)
            {
                string item = listBox1.SelectedItems[0].ToString();
                if (!list.Contains(item))
                    list.Add(item);
                form2.PopulatingComboBox(list);
            }
        }

        private bool FormOpened(String sFormName)
        {
            bool isFound = false;
            foreach (Form form in this.OwnedForms)
            {
                if (form.Name == sFormName)
                {
                    isFound = true;
                    break;
                }
            }
            return isFound;
        }
    }

//FORM2:
public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public void PopulatingComboBox(List<string> list)
        {
            this.comboBox1.Items.Clear();
            for (int i = 0; i < list.Count; i++)
            {
                this.comboBox1.Items.Add(list[i]);
            }
        }
    }

The code checks if the item in comboBox2 on form2 already exists. If yes, then the selected item in listBox (on form1) is not passed to comboBox.

Hope it helps,
Mitja

Edited 6 Years Ago by Mitja Bonca: n/a

crishjeny:
STOP wtih such unproductive posts! We all know that!
Its better not to post posts like this in here.

I have already warned you...
Mitja

This article has been dead for over six months. Start a new discussion instead.