Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / December 2004

Tip: Looking for answers? Try searching our database.

Get a type from a string

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Erol - 15 Dec 2004 11:59 GMT
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

Signature

Erol

Martin Müller - 15 Dec 2004 13:17 GMT
I'm not really sure if I understand what you want, but I think you have the
name of an enum value stored in a database and want to get back the enum
value from its name, correct?

The Enum class can do this. There's a static method "Parse" doing exactly
this:

Anchor = CType([Enum].Parse(GetType(System.Windows.Forms.AnchorStyles),
"Bottom"), System.Windows.Forms.AnchorStyles)

Regards,
Martin Müller

> 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
[quoted text clipped - 106 lines]
>
> End Class
Herfried K. Wagner [MVP] - 15 Dec 2004 13:40 GMT
"Erol" <Erol@discussions.microsoft.com> schrieb:
> [The same as in microsoft.public.dotnet.languages.vb.]

Please don't multipost!

Signature

M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.