当没有选择时,需要组合框返回null。 [英] Need combobox to return null when none selected.

查看:94
本文介绍了当没有选择时,需要组合框返回null。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有组合框,在任何给定的点,不需要选择,所以它留空。



表格加载后,我的所有组合框都是空白的(在选择一个之前不会显示任何内容)。当我绕过cboCaseStatus(未选中)时,不会发生错误,但在保存时,它会默认返回列表中的最后一项。它永远不会留空。



  //  在我的表单中,我有: 
private void btnSave_Click( object sender,EventArgs e)
{
CaseRecord.PersonLast = txtPersonLast.Text;

CaseRecord.PersonFirst = txtPersonFirst.Text;
// 这是我的可空组合的一个例子
CaseRecord.CaseStatusID = cboCaseStatus.SelectedPicklistNullableID();
// 这是一个必需的组合框字段,vaildation工作正常。
CaseRecord .CaseTypeID = cboCaseType.SelectedPicklistID();
}

如何 我的组合框 return null 时没有项 。我不会产生错误,但我希望有一个可以为空的 return 。救命!谢谢!

解决方案

首先,在你想要处理可空索引时,你不应该处理返回的函数 int ,你应该只使用返回 int?的函数。基本上,您的 SelectedPicklistNullableID 的代码是正确的。唯一的问题是:该项目真的是 PickListItem 类型吗?它没有显示在您的代码中,但它是一个好主意。但是,你不应该使用 int?类型。 null值实际上不是 ID ,而是存储在列表项中的整个记录​​。你不需要你的选择* 方法。



这是你可以做的:

  class  PickListItem { //  < span class =code-comment>成为类很好,而不是struct  
internal int ID { get ; set ; }
// 依此类推......
}

// ...

cb.Items.Add ( new PickListItem( / * ... * / < /跨度>));

// ...

btnSave .Click + =(sender,eventArgs)= > {
PickListItem item =(PickListItem)cb.SelectedItem;
if (items == null return ; // 或做某事
CaseRecord.CaseStatusID = item.ID;
// 依此类推
};



你可以做得更好。首先禁用 btnSave 。处理事件 System.Windows.Forms.ComboBox.SelectedIndexChanged ,并且,当选择列表中的某个有效元素时,分配 btnSave.Enabled = true 。当你没有正确选择的元素时,它会简单地阻止保存。



现在,一切都很好,有一个问题:UI会显示一些东西比如My.Namespace.Name.PickListItem。如何展示有用的东西。简单!显示的内容基于 System.Object.ToString 返回的值。覆盖它!它可能类似于

  class  PickListItem {
public 覆盖 string ToString(){
return string .Format( Item {0},ID); // 仅显示ID
// 或更复杂的东西,有几个属性
// 以及与它们进行的任何计算......
}
internal int ID { get ; set ; }
// 依此类推......
}





请看我过去的回答: combobox.selectedvalue在winform中显示{} [ ^ ]。



你明白了吗?



-SA


尝试这是否有效:

  if (cb.SelectedIndex.equals(-1)) return   null  


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.

//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!

解决方案

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:

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

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


try if this works:

if (cb.SelectedIndex.equals(-1)) return null;


这篇关于当没有选择时,需要组合框返回null。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆