简体   繁体   English

通过VB.NET中的字符串变量设置控件属性值

[英]Set control property value via a string variable in VB.NET

I am very new to vb.net and the finding the terminology/concepts a little daunting at the moment, so forgive the newbieness of this question. 我是vb.net的新手,目前发现术语/概念有些令人生畏,因此请原谅此问题的新颖性。

I'm wanting to set some control properties via variables. 我想通过变量设置一些控件属性。 For instance: 例如:

Dim f As DateTimePicker
Dim g As String = "Short"
f.Format = DateTimePickerFormat.g

This doesn't work. 这行不通。 What am I doing wrong and how can I correct it? 我在做什么错,我该如何纠正?

If you are trying to set it the system short format, you would just do it like this: 如果您尝试将其设置为系统短格式,则可以这样操作:

Dim f As New DateTimePicker
f.Format = DateTimePickerFormat.Short

Your idea was correct, but the Format property accepts an enumerated value which are predefined Enum values . 您的想法是正确的,但是Format属性接受一个枚举值,该值是预定义的Enum Without going into reflection (which is a more advanced topic) you need to use a predefined value instead of trying to set it dynamically with a string value. 无需深入思考(这是一个更高级的主题),您需要使用预定义的值,而不是尝试使用字符串值动态设置它。

Edit 编辑

If you really want to use a string value to set the Format , you can do it this way: 如果您确实想使用字符串值来设置Format ,则可以通过以下方式进行操作:

Dim f As New DateTimePicker

' This must be set to a valid DateTimePickerFormat name.
Dim formatType As String = "Short"

f.Format = DirectCast([Enum].Parse(GetType(DateTimePickerFormat), formatType), DateTimePickerFormat)

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

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