G
Guest
How do I get a type from a string? I'm retrieving a string value from my
database so that I can set my property values dynamically. In the event
"Form1_Load", you will see that I'm trying to set the button's anchor
property to "System.Windows.Forms.AnchorStyles.Bottom" dynamically from a
string (which I commented out). How can I get this code to work. I also
started to use the variable "fi", but I don't know how to put it to good use.
Please help!
'***************************************************
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load
Dim Anchor As PropertyInfo =
GetType(System.Windows.Forms.Button).GetProperty("Anchor")
Dim fi As FieldInfo =
GetType(System.Windows.Forms.AnchorStyles).GetField("Bottom")
'This line works, but I need the code to get
the type from the string
Anchor.SetValue(Button1, System.Windows.Forms.AnchorStyles.Bottom, Nothing)
'Uncomment to see error
'Anchor.SetValue(Button1,
type.gettype("System.Windows.Forms.AnchorStyles.Bottom"), Nothing)
End Sub
'**********************************************
'Entire VB.net example
Option Strict On
Option Explicit On
Imports System
Imports System.Reflection
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(26, 44)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(120, 50)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 262)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load
Dim Anchor As PropertyInfo =
GetType(System.Windows.Forms.Button).GetProperty("Anchor")
Dim fi As FieldInfo =
GetType(System.Windows.Forms.AnchorStyles).GetField("Bottom")
'This line works, but I need the code to get
the type from the string
Anchor.SetValue(Button1, System.Windows.Forms.AnchorStyles.Bottom, Nothing)
'Uncomment to see error
'Anchor.SetValue(Button1,
type.gettype("System.Windows.Forms.AnchorStyles.Bottom"), Nothing)
End Sub
End Class
database so that I can set my property values dynamically. In the event
"Form1_Load", you will see that I'm trying to set the button's anchor
property to "System.Windows.Forms.AnchorStyles.Bottom" dynamically from a
string (which I commented out). How can I get this code to work. I also
started to use the variable "fi", but I don't know how to put it to good use.
Please help!
'***************************************************
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load
Dim Anchor As PropertyInfo =
GetType(System.Windows.Forms.Button).GetProperty("Anchor")
Dim fi As FieldInfo =
GetType(System.Windows.Forms.AnchorStyles).GetField("Bottom")
'This line works, but I need the code to get
the type from the string
Anchor.SetValue(Button1, System.Windows.Forms.AnchorStyles.Bottom, Nothing)
'Uncomment to see error
'Anchor.SetValue(Button1,
type.gettype("System.Windows.Forms.AnchorStyles.Bottom"), Nothing)
End Sub
'**********************************************
'Entire VB.net example
Option Strict On
Option Explicit On
Imports System
Imports System.Reflection
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(26, 44)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(120, 50)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 262)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load
Dim Anchor As PropertyInfo =
GetType(System.Windows.Forms.Button).GetProperty("Anchor")
Dim fi As FieldInfo =
GetType(System.Windows.Forms.AnchorStyles).GetField("Bottom")
'This line works, but I need the code to get
the type from the string
Anchor.SetValue(Button1, System.Windows.Forms.AnchorStyles.Bottom, Nothing)
'Uncomment to see error
'Anchor.SetValue(Button1,
type.gettype("System.Windows.Forms.AnchorStyles.Bottom"), Nothing)
End Sub
End Class