从组合框中获取所选项目或输入文本的最佳方法 [英] Best way to get selected item OR entered text from combobox

查看:48
本文介绍了从组合框中获取所选项目或输入文本的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个预填充的组合框,其中有很多可能的选择。但我也希望打开该选项,以便用户手动输入不是选项之一的文本。因此,我将DropDownStyle设置为DropDown,这样才有可能。

I have a combobox that I pre-populate with numerous possible choices. But I also want the option open for the user to manually enter text that is not one of the choices. So I leave the DropDownStyle set to DropDown so this is possible.

我的问题是,编写代码以返回值的最有效的方法用户选择还是手动输入?

My question is, what is the most efficient (yet proper) way to write the code to return the value the user either selects, or manually enters?

当前我正在使用以下代码。但是对于这样一个简单的任务似乎有些冗长。是否有更好(更短)的方法来获得相同的结果?

Currently I am using the following code. But it seems a bit verbose for such a simple task. Is there a better (shorter) way to obtain the same result?

        string Code1 = comboBox_Code1.GetItemText(comboBox_Code1.SelectedItem);
        if (Code1.Length == 0) Code1 = comboBox_Code1.Text;


推荐答案

Siva Gopal在评论中发布了答案。它是迄今为止建议的最短和最简单的解决方案。我已经对其进行了测试,当用户选择一个预先填充的值时它就可以使用,并且当用户手动键入一个值时它也可以使用!

Siva Gopal posted the answer in a comment. It is by far the shortest and simplest solution suggested. I have tested it and it works when the user selects a pre-populated value, and it also works when the user manually types in a value!

string Code1 = comboBox_Code1.Text;

这篇关于从组合框中获取所选项目或输入文本的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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