Combobox contains only item Message box pop up

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • faraz321
    New Member
    • Apr 2010
    • 4

    Combobox contains only item Message box pop up

    I am trying to show a Messagebox to come up if the combobox contains only one item and then terminate the program execution when the user dismisses the message box. This is done in Winform.

    Code:
    private void btn_add_Click(object sender, EventArgs e)
            {
                if (combobox.SelectedIndex != -1)
                    ListBox.Items.Add(combobox.SelectedItem);
                combobox.Items.RemoveAt(combobox.SelectedIndex);
                    
            
                else if(combobox.SelectionLength = null) 
    
                MessageBox.Show("ComboBox must have one item left", " Warning", MessageBoxButtons.OKCancel);
            }
    Last edited by tlhintoq; Apr 5 '10, 04:26 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    I think you need to tell us what the problem is; however, I will mention that you need to put braces around your if statements.

    Code:
    private void btn_add_Click(object sender, EventArgs e)
    {
      if (combobox.SelectedIndex != -1)
      {
        ListBox.Items.Add(combobox.SelectedItem);
        combobox.Items.RemoveAt(combobox.SelectedIndex);
      }
      else if(combobox.SelectionLength = null)
      {
        MessageBox.Show("ComboBox must have one item left", " Warning", MessageBoxButtons.OKCancel);
      }
    }

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Originally posted by OriginalPoster
        I have xxx, then do yyy then zzz happens. Here's my code ...
        Ok. You know what you have.
        What you don't have is a question. Nobody here knows why you posted this since you haven't asked anything. What is it you are looking for?
        I recommend you read the FAQ about How to ask a good question so the volunteers will be able to help you.

        Comment

        • faraz321
          New Member
          • Apr 2010
          • 4

          #5
          I am looking for the messagebox to pop up

          Thanks Gary for correcting me. I did however, put the braces in there but somehow did not show up here. My question is still unanswered.
          Question is I want my program to ensure that the ComboBox contains at least one item. If it does not print a message , using a messagebox, then terminate program execution when the user dismisses the message box. Thanks for all your help guys.

          Code:
          private void btn_add_Click(object sender, EventArgs e) 
          { 
            if (combobox.SelectedIndex != -1) 
            { 
              ListBox.Items.Add(combobox.SelectedItem); 
              combobox.Items.RemoveAt(combobox.SelectedIndex); 
            } 
            else if(combobox.SelectionLength = null) 
            { 
              MessageBox.Show("ComboBox must have one item left", " Warning", MessageBoxButtons.OKCancel); 
            } 
          }

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            Question is I want my program to ensure that the ComboBox contains at least one item. If it does not print a message , using a messagebox, then terminate program execution when the user dismisses the message box.
            What Gary is saying is : That isn't a question. Its a list of wanted behavior. There is no mention of what you have already coded to accomplish there. There is no mention of errors or exceptions.

            The way it reads now, it sounds like you are saying "Please write my code / fix my program for me" and I'm sure that isn't your intent.

            Comment

            • faraz321
              New Member
              • Apr 2010
              • 4

              #7
              I have already accomplish adding and removing an item from the combobox and adding it to the listbox. However, I have failed to get a messagebox to work properly when the combobox contains at least one item. Probably my choice of selectionlength = null is not appropriate. I am new to C sharp.

              Comment

              • tlhintoq
                Recognized Expert Specialist
                • Mar 2008
                • 3532

                #8
                Ok... Let me see if I have this correct.
                You want to produce the MessageBox if and only if the combo box text entry area is blank. Is that right?
                Can you simply change the combobox style from DropDown to DropDownList?
                This way there is *no* text area: The user is forced to select one of the .Items from the combo box.

                I generally feel it is better to avoid a problem then try to compensate for one.

                Comment

                • faraz321
                  New Member
                  • Apr 2010
                  • 4

                  #9
                  Combobox must have one last item and a messagebox should pop up indicating to terminate the program if one tries to remove the last item. I wish it would be simple to just use the dropdownlist as you sugggested but its not as simple as that. Thanks for your suggestions

                  Comment

                  • tlhintoq
                    Recognized Expert Specialist
                    • Mar 2008
                    • 3532

                    #10
                    If there must be at least 1 item, then you should be checking the count of items, not the selection.

                    Items.Count > 0

                    Comment

                    Working...