开发者

using combo box values by using id's

开发者 https://www.devze.com 2023-03-26 23:56 出处:网络
I have a combobox with values like members, payments, visits like.... we can do like this if(combobox.text = \"mem开发者_运维问答bers\")

I have a combobox with values like members, payments, visits like....

we can do like this

        if(combobox.text = "mem开发者_运维问答bers")
        {
                \\do something...
        }



        if(combobox.text = "visits")
        {
                \\do something...
        }

       if(combobox.text = "payments")
        {
                \\do something...
        }

we can do like this but i dont want to hardcode like this is there any other way to use combo box values with their id's not using text like the above mentioned

can any one help on this ....


the alternative is to fill the combo box with items which have value and text and these could be, for example, two columns of a table in the database.

In this way in the loading of the items there is no hard coded data and you can load more items (or simply different ones) by editing the data in that database table.

if the number of items is very limited you can also create an enum and use the enum values to verify what is the currently selected value, with some modifications of the code you have shown above.


See here: http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.items.aspx

You can attach any object to every item, along with the text, and then get the selected item, extract the object, cast it to the type you know it is, and then get your data from there...

Oh, and you might want to consider some advice: populate the combobox in C# code, not manually in the designer.


There are better ways if your values are comming from a datasource or or something. Since these look like static commands which won't change much, there really might not be a much cleaner way to do it.

I agree with @Manatherin's comment, you should do it in a switch statement. I would include and else clause that throws an exception, so you know if you mistyped the value text or something.

0

精彩评论

暂无评论...
验证码 换一张
取 消