简体   繁体   中英

Change a setting using a combobox in vb.net

I'm working on a program, and I need to be able to change a setting using a combobox. I'm getting

CS0266 Cannot implicitly convert type 'object' to 'FastColoredTextBoxNS.Language'. An explict conversion exists (are you missing a cast?) Using the code:

        {
            fastColoredTextBox1.Language = comboBox1.SelectedItem
        }

Does anyone know a simple way to fix this? If any more info is needed, I will gladly edit it in.

You must cast to the desired type, since the SelectedItem property returns the unspecific type Object .

fastColoredTextBox1.Language = DirectCast(comboBox1.SelectedItem, FastColoredTextBoxNS.Language)

Note there is also the CType function. In addition to performing type casts it also performs also type conversions. We do not need a conversion here, if the items in the ComboBox are of the required type. DirectCast just means, okay I know that the item with the runtime type Object is of the required type, just take it as such.

See also:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM