Welcome to Dream.In.Code
Become a C# Expert!

Join 150,399 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 974 people online right now. Registration is fast and FREE... Join Now!




Accessing items from a ComboBox.

 
Reply to this topicStart new topic

Accessing items from a ComboBox., I get an error

papuccino1
2 Mar, 2008 - 04:50 PM
Post #1

D.I.C Head
**

Joined: 2 Mar, 2008
Posts: 91


My Contributions
CODE
namespace CalculadoraFinal
{
    I THINK I HAVE TO PUT SOMETHING HERE TO DECLARE THE COMBOBOX, BECUASE WHEN I WRITE: "ComboBox" HERE
IT APPEARS IN GREEN LETTERS. I DON'T KNOW WHAT TO PUT THOUGH, THIS IS THE BIG QUESTION.
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button19_Click(object sender, EventArgs e)//BOTON CONVERTIR
        {
            if (this.combobox1.SelectedIndex = 0)
            {
                if (this.combobox2.SelectedIndex = 0)
                {
                }


                if (this.combobox2.SelectedIndex = 1)
                {
                }

                if (this.combobox2.SelectedIndex = 2)
                {
                }

                if (this.combobox2.SelectedIndex = 3)
                {
                }
            }


        }



How do I declare a ComboBox? What should I write and where should I write it?

Quick Edit: Also, is it correct start counting the index number of a combobox from 0,1,2,3.... Or do I start in 1? Is using the index value correct?

Quick edit 2: Here's a picture with my current design to better illustrate my problem.

IPB Image

This post has been edited by papuccino1: 2 Mar, 2008 - 05:09 PM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Accessing Items From A ComboBox.
2 Mar, 2008 - 06:20 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
If you have the 2 controls on your form you don't have to declare them in your code, you just need to use the name of the control. If your controls aren't named combobox1 and combobox2 then you're going to get that error.

Indexes always start at 0, so you are correct in that matter.
User is offlineProfile CardPM
+Quote Post

killnine
RE: Accessing Items From A ComboBox.
2 Mar, 2008 - 06:24 PM
Post #3

D.I.C Head
**

Joined: 12 Feb, 2007
Posts: 114



Thanked: 5 times
My Contributions
EDIT: Yeah, if you use the Drag-N-Drop in visual studio to create your controls, don't redeclare them in your code. Rather, just look at their names in the [Design] pane and call them in your code. Nice catch, Psycho.


To create a combobox in C#, you must declare and initialize the combobox:

CODE

ComboBox myNewComboBox = new ComboBox();


OR you could do this in two separate lines:

CODE

ComboBox myNewComboBox;

.
.
.
.

myNewComboBox = new ComboBox();



You might want to do this in instances where you don't need to initialize the combo box until a certain part of your code is reached. But I think in your case you can just do the first thing.


Also, just as a suggestion, you might want to take a look at the Switch statement rather than a collection of If statements. It is a bit faster and would probably be better suited for your application:

CODE


(this would go in your button19_Click method)

       switch(myNewComboBox.SelectedIndex)
       {
           case 1:
               //Do something here if the index selected is 1
               break;

           case 2:
               //Do something here if the index selected is 2
               break;

           case 3:
               //Do something here if the index selected is 3
               break;

           default:
               //If none of the cases are correct, default to this case.
               break;

       }



Isn't that cleaner than before? cool.gif


Or, if you are REALLY hellbent on using if statements, at least use if and then else-if. This prevents the application from checking EVERY case, even though only one can be fulfilled:

CODE

        private void button19_Click(object sender, EventArgs e)
        {

                if (this.combobox2.SelectedIndex = 0)
                {
                }


                else if (this.combobox2.SelectedIndex = 1)
                {
                }

                else if (this.combobox2.SelectedIndex = 2)
                {
                }

                else if (this.combobox2.SelectedIndex = 3)
                {
                }
            }




Hope this helps.

This post has been edited by killnine: 2 Mar, 2008 - 06:25 PM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Accessing Items From A ComboBox.
2 Mar, 2008 - 06:33 PM
Post #4

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Remember, if you declare them in code you're going to have to attach your events to them manually, like so (this is attaching the SelectedIndexChanged Event):


csharp

ComboBox1.Click += new EventHandler(ComboBox1_SelectedIndexChanged)


Then you would write your SelectedIndexChanged event to what you want it to do when that event is fired.
User is offlineProfile CardPM
+Quote Post

papuccino1
RE: Accessing Items From A ComboBox.
2 Mar, 2008 - 06:59 PM
Post #5

D.I.C Head
**

Joined: 2 Mar, 2008
Posts: 91


My Contributions
I'll stick to using the switch, I wasn't aware of them ph34r.gif Hahahaha

Thank you very much the help.

ps. I think it's a VERY nice idea to have people show they made a little effort in trying at least. Keeps the people who are just lazy off. biggrin.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 06:48PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month