2

The scenario is this: I want to set the formborderstyle using a combobox.

I can set the borderstyle to "None" successfully with these methods:

Form1.FormBorderStyle = 0

Or

Form1.FormBorderStyle = Windows.Forms.FormBorderStyle.None

How could I do this with a string?

Dim formstyle As String
formstyle = "Windows.Forms.FormBorderStyle." & ComboBox1.Text
Form1.FormBorderStyle = formstyle

I get this error: Conversion from string "Windows.Forms.FormBorderStyle.No" to type 'Integer' is not valid.


It seems like Form1.formborderstyle only takes integers. Without actually using the integers, is there a way i can convert the string to the integer counterpart...sort of like an eval?

Ultimately I was hoping it looked something like:

Dim formstyle As String
formstyle = "Windows.Forms.FormBorderStyle." & ComboBox1.Text
Form1.FormBorderStyle = eval(formstyle)
MPelletier
  • 16,256
  • 15
  • 86
  • 137
user1950278
  • 1,685
  • 2
  • 10
  • 15

1 Answers1

3

Use Enum.(Try)Parse:

Enum.Parse

Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.

The documentation and this answer has an example of the syntax. You're probably after something like:

Form1.FormBorderStyle = CType([Enum].Parse(GetType(FormBorderStyle), ComboBox1.Text), FormBorderStyle)
Community
  • 1
  • 1
ta.speot.is
  • 26,914
  • 8
  • 68
  • 96
  • For some reason im getting: Requested value 'Windows.Forms.FormBorderStyle.None' was not found. – user1950278 Jul 28 '13 at 06:59
  • 1
    Try using just `None`. enum's don't know their namespace. They just know their values. – Cole Tobin Jul 28 '13 at 07:00
  • 2
    @user1950278 Look at the example I wrote and compare it to yours. I don't concatenate `Windows.Forms.FormBorderStyle.` to the start of the enum name. – ta.speot.is Jul 28 '13 at 07:06