Click here to Skip to main content
15,914,419 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have comboboxes where at any given point, no selection is necessary, so it is left blank.

Upon form load, all my comboboxes are blank (nothing displayed until one is selected). When I bypass the cboCaseStatus (unselected), no error occurs, but upon Save, it returns the last item in the list by default. It never leaves it blank.

C#
//In my form I have:
private void btnSave_Click(object sender, EventArgs e)
          {
          CaseRecord.PersonLast = txtPersonLast.Text;

          CaseRecord.PersonFirst = txtPersonFirst.Text;
        //this is an example of my nullable combobx
        CaseRecord.CaseStatusID = cboCaseStatus.SelectedPicklistNullableID();
        //this is a required combobox field, vaildation works fine.
        CaseRecord.CaseTypeID = cboCaseType.SelectedPicklistID();
        }

How do I get my comboboxes to return null, when no item is selected. I do not generate errors, but I would like to have a nullable return. HelP! Thank you!
Posted
Updated 30-Apr-15 12:24pm
v2
Comments
Sergey Alexandrovich Kryukov 29-Apr-15 16:49pm    
To start with, specify what do you mean by "ComboBox". Which one? Full type name, please. What's the problem? You can always get selection and see if something is selected or not.
—SA
Sam 9100 29-Apr-15 17:31pm    
I state in the 3rd paragraph this is for my cboCaseStatus as an example. But it happens for all my comboboxes with nullable ID's.

These obvious statements like this, "You can always get selection and see if something is selected or not.' does not help me at all. I have explained in my question that the problem is when I do NOT select anything, that particular combobx returns the last item in my dropdown list, it should leave it empty.

The reason is my users will be entering case data that are "in progress" so some of my combobox controls may start out empty and change.

There is no issue when I select an item from my combobox dropdown list. It saves what I selected. No issue here.
Sergey Alexandrovich Kryukov 29-Apr-15 19:31pm    
It looks like you are trying to talk to me, but how would you notify me? I opened this page again just by some random reason. You should have post this comment in reply to mine, only then I could receive a notification. Okay, but a chance, but I can can answer you.

Sorry for some obvious statements, but the way you ask your question did not give me any assurance that you understand the obvious. What makes you thinking that every statement in the comments is supposed to help you? It could be helpful enough only because it should have shown you that you probably need to clarify the problem. How anyone could be more helpful for you if you are not helpful enough to yourself. For example, you still did not answer the most important question, the type of "ComboBox". Please, what's the type? Without it, further talks would be about nothing.

Now, your concern is absolutely non clear and your comment strongly suggests that you don't understand the obvious. If nothing is selected, nothing should "return the last item". The control does not "return" anything, but some your method could returns something, and only because it is not written correctly. Please point it out: which method? (The concept "return" is only applicable to methods or property getters.) The problem looks extremely simple.

—SA

Sergey Alexandrovich Kryukov 30-Apr-15 10:19am    
Well, they are similar, so what? Give me the type. Are you trying to prevent getting help, or you still need it. Just tell me the type.
—SA
Sam 9100 30-Apr-15 12:10pm    
I have a normal dropdown combobox, not a dropdownlist box

First of all, in you want to deal with nullable index, you should not deal with function returning int, you should use only the function returning int?. Basically, your code of SelectedPicklistNullableID is correct. The only problem is: will the item really of the type PickListItem? It is not shown in your code, but it would be a good idea. But then, you should not use int? type at all. The null value is actually not the ID, but the whole record you store in the list item. You don't need your Selected* method(s) either.

Here is what you can do:
C#
class PickListItem { //it's good to be the class, not struct
   internal int ID { get; set; }
   // and so on...
}

//...

cb.Items.Add(new PickListItem(/* ... */));

//...

btnSave.Click += (sender, eventArgs) => {
    PickListItem item = (PickListItem)cb.SelectedItem;
    if (items == null) return; // or do something
    CaseRecord.CaseStatusID = item.ID;
    // and so on
};

You can do even better. Make your btnSave disable at first. Handle the event System.Windows.Forms.ComboBox.SelectedIndexChanged and, when some valid element in the list is selected, assign btnSave.Enabled = true. It will simply prevent doing "save" when you don't have properly selected element.

Now, everything is fine here, with one problem: the UI will show something like "My.Namespace.Name.PickListItem". How to show something useful instead. Simple! What is shown is based on the value returned by System.Object.ToString. Override it! It could be something like
C#
class PickListItem {
   public override string ToString() {
       return string.Format("Item {0}", ID); // to show just ID
       // or something more complicated, with several properties
       // and any calculations with them...
   }
   internal int ID { get; set; }   
   // and so on...
}


Please see my past answer: combobox.selectedvalue shows {} in winform[^].

Are you getting the idea?

—SA
 
Share this answer
 
try if this works:
C#
if (cb.SelectedIndex.equals(-1)) return null;
 
Share this answer
 
Comments
Sam 9100 30-Apr-15 11:57am    
I appreciate your professional and courteous answer Unkownwar.

I replace the line 'if(cb.SelectedItem== null) portion, but it still did not work. I add it here because it renders my pick list item whether one is selected or not.
//In my comboboxExtension class
public static int? SelectedPicklistNullableID(this ComboBox cb)
{
//if (cb.SelectedItem == null) return null;

//added here
if (cb.SelectedIndex.Equals(-1)) return null;
return ((PickListItem)cb.SelectedItem).ID;
}
public static int SelectedPicklistID(this ComboBox cb)
{
return cb.SelectedPicklistNullableID().Value;

My combobox still defaults to the last item of the dropdown list. it is getting overriden everytime.
Sergey Alexandrovich Kryukov 30-Apr-15 17:18pm    
All wrong. This line won't even compile, by some apparent reasons. You should be more responsible for your answers.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900