Search code examples
vb.netstrict

Assign FormWindowState with Option Strict On


Form.WindowState uses the FormWindowState enumeration to set the value to "minimized", "maximized", "normal". On closing a form i cast these states to integer and export them. However i can't assign them on reloading the form by using

Me.WindowState = CInt(getMySetting())

because i am using

Option Strict On

which disallows this implicit conversion from int to FormWindowState. FormWindowState does not have any constructors i can use, so i don't know how i can create the matching FormWindowState by using the corresponding Integer. Do you have any suggestions except for turning Option Strict off? Thank you!


Solution

  • This should work:

    Me.WindowState = CType(CInt(getMySetting()), FormWindowState)
    

    You have to cast the Integer to FormWindowState (presuming that it's a valid value)

    But the more robust/readable way would be to use the FowmWindowState-enum in the settings directly. You can do that. You just have to find it in the correct namespace(see the picture below):

    enter image description here

    Now this strongly typed code works:

    Me.WindowState = My.Settings.DefWindowState