简体   繁体   English

在 vb.net 中使用组合框更改设置

[英]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'. CS0266 无法将类型“对象”隐式转换为“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 .您必须转换为所需的类型,因为SelectedItem属性返回非特定类型Object

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

Note there is also the CType function.请注意,还有CType函数。 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.如果 ComboBox 中的项目是所需类型,我们就不需要在这里进行转换。 DirectCast just means, okay I know that the item with the runtime type Object is of the required type, just take it as such. DirectCast只是意味着,好吧,我知道运行时类型为Object的项目是所需的类型,就这样吧。

See also:也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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