Sumitra Paul

Sumitra Paul

  • NA
  • 51
  • 32.8k

ComboBox related question

Mar 5 2013 12:18 PM

Hi,

I have some problem with combo box.
I have done something but its not working properly.

I am adding a list of object, now I want to rename or add new items to the combo box
on click of combobox item.

Please see the code below.

 public class Book
    {
        public Book(string bookName, string bookId)
        {
            BookName = bookName;
            BookId = bookId;
        }
        public string BookName { get; set; }
        public string BookId { get; set; }

        public override string ToString()
        {
            return BookName;
        }
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        ComboBox comboBox1 = new ComboBox();
        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Location = new Point(20, 60);
            comboBox1.Name = "comboBox1";
            comboBox1.Size = new Size(245, 25);

            AddItemsToComboBox();

            this.Controls.Add(comboBox1);
            comboBox1.SelectedValueChanged += OnComboBox1SelectionChanged;
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
        }

        private void AddItemsToComboBox()
        {
            // Get it from database and populate
            comboBox1.Items.Add(new Book("A", "1"));
            comboBox1.Items.Add(new Book("B", "2"));
            comboBox1.Items.Add(new Book("C", "3"));
            comboBox1.Items.Add("Default");
            comboBox1.Items.Add("Add");
        }

        private void OnComboBox1SelectionChanged(object sender, EventArgs e)
        {
            ComboBox rpCombo = (ComboBox)sender;
            if (rpCombo.SelectedItem.ToString().Equals("Default"))
            {
                rpCombo.DropDownStyle = ComboBoxStyle.DropDownList;
            }
            else
            {
                rpCombo.DropDownStyle = ComboBoxStyle.DropDown;
            }
        }

        private void Save_Click(object sender, EventArgs e)
        {
            // Here basically if user clicks any items except default and Add, then I need to rename
            // the item, save to DB and populate again.
            // I just want to rename it by taking the bookid and populate it back to combobox.

            // If user clicks Add, then whatever user types, I have to create new BookId and add the bookname
            // typed by user and populate it back.


            var comboBoxItems = comboBox1.Items;
            if (comboBox1.SelectedItem != null)
            {
                var selectedItemId = ((Book)comboBox1.SelectedItem).BookId;
                foreach (var item in comboBoxItems)
                {
                    var book = item as Book;
                    if (book != null)
                    {
                       string bookId = book.BookId;
                    }
                }
            }

            // Save it to the database
        }

On click of save:

Here basically if user clicks any items except default, then I need to rename the item, save to DB and populate again.I just want to rename it by taking the bookid and populate it back to combobox.

If user clicks Add, then whatever user types, I have to create new BookId and add the bookname
typed by user and populate it back with other allready existing items.

Not getting how I can do that. How to identify, if I have to rename or Add ?? because in both the case the combobox just becomes editable. Now how to recognize, if I have to Add or raname ?

Please help if posible.

Thanks.


Answers (1)