Jump to content

combobox question

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
11 replies to this topic

#1
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
Hello,

I have a form with a combobox, i have added 3 items in which is the code below:


 public Inventory()

        {

            InitializeComponent();

            lstMain.GridLines = Enabled;

            cmbchoose.Items.Add("Add New Item");

            cmbchoose.Items.Add("Delete an Item");

            cmbchoose.Items.Add("Edit Item");

        }

and i am wondering, how do i make it to where if the user selects from the combobox and chooses --> Add new item, if choose combobox add new item, then open up form, if choose delete an item, then open up this form, do I get it to do that?

hopefully it is simple, thanks in advance.

#2
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
Sorry everyone, ignore this post but then again, this post might be helpful, here is the answer:


private void cmbchoose_SelectedIndexChanged(object sender, EventArgs e)

        {

            addforminventory addform = new addforminventory();

            if (cmbchoose.SelectedItem.ToString().CompareTo("Add New Item") == 0)

                addform.Show();


        }

what this is saying is if "add new item" does not = 0 then show form pretty much. Hope this helps everyone, someone explained it to me just now.

#3
pb_ce85

pb_ce85

    Newbie

  • Members
  • PipPip
  • 21 posts
Yes, It works for you correctly but you can use enum:

        Dictionary<string, Values> cmbItems = new Dictionary<string, Values>();

        enum Values

        {

            Add,

            Delete,

            Edit

        }


        public Form1()

        {

            InitializeComponent();

            

            cmbItems.Add("Add New Item", Values.Add);

            cmbItems.Add("Delete an Item", Values.Delete);

            cmbItems.Add("Edit Item", Values.Edit);


            foreach (string key in cmbItems.Keys)

                comboBox1.Items.Add(key);

        }


        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

        {

            Values selectedValue;

            cmbItems.TryGetValue(comboBox1.SelectedItem.ToString(), out selectedValue);

            DoWork(selectedValue);


        }


        void DoWork(Values val)

        {

            switch (val)

            {

                case Values.Add:

                    // Code for Add

                    break;

                case Values.Delete:

                    // Code for Delete

                    break;

                case Values.Edit:

                    // Code for Edit

                    break;

            }

        }
It's less messy and more readable.

#4
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
Less messy? but atleast it gets the job done : )

#5
pb_ce85

pb_ce85

    Newbie

  • Members
  • PipPip
  • 21 posts
Yes, it does as I said. But "Writing A Code" is not the end. The goal is "Writing The Code":rolleyes:

#6
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
lol

Enums are fun, if you know exactly which values the data is going to be.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#7
pb_ce85

pb_ce85

    Newbie

  • Members
  • PipPip
  • 21 posts
Yep, I love Enums:cool:

#8
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
ShowSmiley(SmileyType.Smile).

:lol:
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#9
pb_ce85

pb_ce85

    Newbie

  • Members
  • PipPip
  • 21 posts
lol, Nice.
Enums in one word:laugh:

#10
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
What do you mean "in one word"?
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#11
pb_ce85

pb_ce85

    Newbie

  • Members
  • PipPip
  • 21 posts
Nothing Xav,
I just want to say, you introduced Enums well, by "ShowSmiley(SmileyType.Smile)".
Nice work:)

#12
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
[HIGHLIGHT=csharp]
Message.Post(MessageType.Thanks);
[/HIGHLIGHT]

lol
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums