CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2013
    Posts
    15

    dont understand combobox in VS c#

    hey all

    I have some comboboxes
    I have added the collections to them and have written code so that when the selectedindex == 0 etc it will output data to a digital display

    what I want, is for the combobox to recognise that by clicking on one of the options in the collection that - that is the selection...

    at this stage the only way I know my selectedindex== etc works is by using the private void combobox1_selectedindexchanged (object sender eventargs e) thing but for some reason I cannot figure out how to put that in to the piece of code I want... - if I put it in here it invalidates the float values from the measurementspeed.value=value line...

    basically I dont know wht I'm talking about haha

    This is what I have down

    case "Bil_hast":

    _lastVehicleSpeed.UpdateValue(DateTime.Now, value);
    measurementSpeed.Value = value;




    if (comboBox1.SelectedIndex == 0)
    {

    digitalbig1.DigitText = value.ToString("F0");
    biglabel1.Text = "km/h";
    }


    if (comboBox2.SelectedIndex == 0)
    {
    digitalbig2.DigitText = value.ToString("F0");
    biglabel2.Text = "km/h";

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: dont understand combobox in VS c#

    You should make use of a switch statement to determine the selected index, here is an example :

    Code:
    switch (comboBox1.SelectedIndex)
    {
    case 0:
    //do whatever here if first option selected
    break;
    case 1:
    //Do something else here if second option has been clicked
    break;
    }
    The SelectedIndexChanged event is used to determine what option has been selected. You can however, make use of the SelectedIndex property of the combobox from anywhere in your code, like this :

    Code:
    int SelectedIndex;
    SelectedIndex = comboBox1.SelectedIndex
    Hope that helps a bit.

  3. #3
    Join Date
    Aug 2013
    Posts
    15

    Re: dont understand combobox in VS c#

    hey I'm just a rookie and I don't get it

    The code is pre existing for all the data inputs, there is a switch case/break for each data type, the data is output as a float value that gets written.tostring of the digitalgauges.

    I can't put a switch inside a Case can I?

    what I'm trying to do is this

    case "Bil_hast": //this is a vehicle speed input

    _lastVehicleSpeed.UpdateValue(DateTime.Now, value);
    measurementSpeed.Value = value;


    //I want comboboxes for every gauge and the choice to choose what gets displayed on which gauge, hee I want the gauge to display vehicle speed if option 1 of the combobox is chosen.


    if (comboBox1.SelectedIndex == 0)
    {

    digitalbig1.DigitText = value.ToString("F0");
    biglabel1.Text = "km/h";
    }


    if (comboBox2.SelectedIndex == 0)
    {
    digitalbig2.DigitText = value.ToString("F0");
    biglabel2.Text = "km/h";
    }


    if (comboBox3.SelectedIndex == 0)
    {

    digitalbig3.DigitText = value.ToString("F0");
    biglabel3.Text = "km/h";
    }




    //if I use the private void selectedinexchanged within this case/break it closes the code } and screws up all the other cases/breaks, I can't figure out how to make the combobox accept selectedindex as the same command as selectedindexchanged....

    I can't write a switch/casebreak fr the combobox further down as I lose the definitions of the input (aka _lastvehiclespeedfloat etc) as I cannot write the same request to change an input to float twice?

    so it has to be within this case/break

    does this make sense, I know I must be frustrating because I don't know that much, but I've successfully done some simple coding, this is just the biggest hurdle to creating customisable gauges!



    break;

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: dont understand combobox in VS c#

    Quote Originally Posted by s900t8v View Post
    I can't put a switch inside a Case can I?
    Sure you can

    Code:
    switch(outerIndex)
    {
      case 1:
        switch(innerIndex1)
        {
          case 10:
            // Do 1 10
            break;
          case 20:
            // Do 1 20
            break;
        }
        break;
      case 2:
        switch(innerIndex2)
        {
          case 10:
            // Do 2 10
            break;
          case 20:
            // Do 2 20
            break;
        }
        break;
    }
    You might want to step back from your big project and spend a little time learning the basic constructs of C#.

  5. #5
    Join Date
    Aug 2013
    Posts
    15

    Re: dont understand combobox in VS c#

    yeah I know but I struggle to understand all the terms - I learn from doing it, I want to do a course at the end of te year but I get bored and keep trying things lol

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured