C# Winform Application Case Switch Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • somacore
    New Member
    • Apr 2008
    • 24

    C# Winform Application Case Switch Problem

    Hi, I have some code, below, that takes input from a combobox and is supposed to parse it and display it. However, it doesn't work. I can't figure out why.

    The combobox is the one for the resolution code, which specifies how the ticket was closed.

    The code compiles fine, but the box persists with the number instead of the actual code. Thoughts?

    if (comboBox12.Tex t != "")
    {
    int ResCode = Convert.ToInt32 (comboBox12.Tex t);
    string Resolution = "";
    switch (ResCode)
    {
    case 0:
    Resolution = "Initial Contact";
    break;
    case 1:
    Resolution = "Remote";
    break;
    case 2:
    Resolution = "On-Site";
    break;
    case 3:
    Resolution = "User Resolved";
    break;
    case 4:
    Resolution = "Cancelled" ;
    break;
    case 5:
    Resolution = "Code";
    break;
    case 6:
    Resolution = "Duplicate" ;
    break;
    }
    comboBox12.Text = Resolution;
    }
    else
    {
    comboBox12.Text = "";
    }
  • rjvrnjn
    New Member
    • Apr 2007
    • 26

    #2
    Can you post a sample of what is contained in the combo box? Like, the user sees "Resolution Name" and the value property stores 3.

    Comment

    • mikeyeli
      New Member
      • Oct 2007
      • 63

      #3
      Originally posted by somacore
      Hi, I have some code, below, that takes input from a combobox and is supposed to parse it and display it. However, it doesn't work. I can't figure out why.

      The combobox is the one for the resolution code, which specifies how the ticket was closed.

      The code compiles fine, but the box persists with the number instead of the actual code. Thoughts?

      if (comboBox12.Tex t != "")
      {
      int ResCode = Convert.ToInt32 (comboBox12.Tex t);
      string Resolution = "";
      switch (ResCode)
      {
      case 0:
      Resolution = "Initial Contact";
      break;
      case 1:
      Resolution = "Remote";
      break;
      case 2:
      Resolution = "On-Site";
      break;
      case 3:
      Resolution = "User Resolved";
      break;
      case 4:
      Resolution = "Cancelled" ;
      break;
      case 5:
      Resolution = "Code";
      break;
      case 6:
      Resolution = "Duplicate" ;
      break;
      }
      comboBox12.Text = Resolution;
      }
      else
      {
      comboBox12.Text = "";
      }
      i assume you want the value of the item selected on the combobox...
      for that use :

      Code:
      this.comboBox1.SelectedValue
      if what you really want is the text, then you should debug the app see if the text contains some unwanted character or spaces in it.

      Comment

      • somacore
        New Member
        • Apr 2008
        • 24

        #4
        Originally posted by mikeyeli
        i assume you want the value of the item selected on the combobox...
        for that use :

        Code:
        this.comboBox1.SelectedValue
        if what you really want is the text, then you should debug the app see if the text contains some unwanted character or spaces in it.
        Making headway using this solution. Changed my code to:

        this.comboBox12 .SelectedValue = Resolution;

        gives me the error:

        "The input string is not in the correct format."

        The Database value it's pulling is an INT, but I want to put a string in the box. Is this possible?

        Comment

        • rjvrnjn
          New Member
          • Apr 2007
          • 26

          #5
          That was the reason I asked you if you can provide us with what are you attaching as datasource to the combo box. If you can answer the below questions you'll be helped in the correct direction (it appears from the posts above).
          a.) How are you setting the datasource? Is it a datatable or a list of items?
          b.) What has been set as Text field and what has been set as Value field?

          Also, in the post above it was suggested that you put the code in the CASE statement expression and not to assign it. Something like:
          Switch this.comboBox12 .SelectedValue
          Case 0
          Case 1... yada...yada...y ada..

          Try that.

          Comment

          • somacore
            New Member
            • Apr 2008
            • 24

            #6
            Originally posted by rjvrnjn
            That was the reason I asked you if you can provide us with what are you attaching as datasource to the combo box. If you can answer the below questions you'll be helped in the correct direction (it appears from the posts above).
            a.) How are you setting the datasource? Is it a datatable or a list of items?
            b.) What has been set as Text field and what has been set as Value field?

            Also, in the post above it was suggested that you put the code in the CASE statement expression and not to assign it. Something like:
            Switch this.comboBox12 .SelectedValue
            Case 0
            Case 1... yada...yada...y ada..

            Try that.

            The combobox datasource is a datatable, it's the field for the resolution code for that ticket. The textfield has been set to to the value in the datatable for that ticket, like 1, 2, 3, etc. The value field is blank. That's why I figured I could do combobox55.text etc...

            I don't quite understand your case statement example. Could you elaborate?

            Comment

            • rjvrnjn
              New Member
              • Apr 2007
              • 26

              #7
              Originally posted by somacore
              I don't quite understand your case statement example. Could you elaborate?
              Well in case the only thing bound in your combo box is text field then you should try either
              Combobox.Select edText
              OR
              Combobox.Select edItem.Text

              do the cast to Int as in your previous post. I guess it should work fine.

              Comment

              • rjvrnjn
                New Member
                • Apr 2007
                • 26

                #8
                Oh, I just realised that I'd been hearing what I thought you're saying rather than what you're saying!! Sorry for that.
                Ok, I'll try to explain the problem that you're facing and let's see if that makes sense. You've assigned a datatable as datasource to the combo box and then set the Displaymember as one of the columns of the datatable. Now using the case statement you're trying to change the display text of the combo box which will not happen as the text is not part of the datasource. In fact, the compiler should have thrown a run time exception but I guess they handle it internally (not sure of the hows involved).

                It'll make more sense if in one of the cases (say for code 2) you do something like
                combobox1.text = "3";
                This will change the code that is visible in the combo box as "3" is part of the datasource. Hope this makes sense.

                Comment

                Working...