[RESOLVED] How to load control from its type string?-VBForums
Results 1 to 7 of 7

Thread: [RESOLVED] How to load control from its type string?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2011
    Posts
    189

    Resolved [RESOLVED] How to load control from its type string?

    I have file like this
    Code:
    System.Windows.Forms.Label
    System.Windows.Forms.Button
    System.Windows.Forms.TextBox
    How to load each control directly from its type string without checking like this

    Code:
            Dim a() As String = IO.File.ReadAllLines("some_file")
            For Each s In a
                Select Case s
                    Case "System.Windows.Forms.Label"
                        Dim ctl As Label = New Label
                        Me.Controls.Add(ctl)
                    Case "System.Windows.Forms.TextBox"
                        Dim ctl As TextBox = New TextBox
                        Me.Controls.Add(ctl)
                    ' A lot of case statement required
                End Select
            Next
    On Error GoTo Hell

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,120

    Re: How to load control from its type string?

    You should look into Reflection. You can create a Type object from a String and then create an instance of that type. Of course, if you want to access members specific to that type then then you would still need some conditional logic. In your case though, you can probably simply treat everything as a Control instance and just access members common to that case type, so you should be fine.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2011
    Posts
    189

    Re: How to load control from its type string?

    I searched the web before asking here but didn't find the answer i want!

    Can you please provide an example?
    On Error GoTo Hell

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,120

    Re: How to load control from its type string?

    But did you search for information on using Reflection specifically? If you learn how to use Reflection then you will inherently learn what you need to know for this specific requirement.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Dec 2011
    Posts
    189

    Re: How to load control from its type string?

    upppppp!
    On Error GoTo Hell

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,509

    Re: How to load control from its type string?

    Try this...

    Code:
    Imports System.Reflection
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim dynamicType As Type = GetType(Control).Assembly.GetType("System.Windows.Forms.TextBox", True)
            ' Create an instance of dynamicType by calling Activator.CreateInstance
            Dim dynamicObject As Control = DirectCast(Activator.CreateInstance(dynamicType), Control)
            dynamicObject.Location = New Point(12, 28)
            dynamicObject.Tag = "First textbox"
            AddHandler dynamicObject.TextChanged, AddressOf ctrls_TextChanged
            Me.Controls.Add(dynamicObject)
    
            Dim dynamicType2 As Type = GetType(Control).Assembly.GetType("System.Windows.Forms.Label", True)
            ' Create an instance of dynamicType2 by calling Activator.CreateInstance
            dynamicObject = DirectCast(Activator.CreateInstance(dynamicType2), Control)
            dynamicObject.Location = New Point(12, 12)
            dynamicObject.Text = "Test"
            Me.Controls.Add(dynamicObject)
    
            Dim dynamicType3 As Type = GetType(Control).Assembly.GetType("System.Windows.Forms.Button", True)
            ' Create an instance of dynamicType3 by calling Activator.CreateInstance
            dynamicObject = DirectCast(Activator.CreateInstance(dynamicType3), Control)
            dynamicObject.Location = New Point(12, 100)
            dynamicObject.Text = "Click"
            dynamicObject.Tag = "First button"
            AddHandler dynamicObject.Click, AddressOf ctrls_Click
            Me.Controls.Add(dynamicObject)
    
        End Sub
    
        Private Sub ctrls_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
            'textbox textchange
        End Sub
    
        Private Sub ctrls_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim tb As TextBox = Me.Controls.OfType(Of TextBox).First(Function(t) t.Tag.ToString = "First textbox")
            MsgBox(DirectCast(sender, Button).Tag.ToString & " clicked" & Environment.NewLine & "TextBox.Text = " & tb.Text)
        End Sub
    
    End Class
    Last edited by .paul.; Oct 5th, 2017 at 09:27 PM.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Dec 2011
    Posts
    189

    Re: How to load control from its type string?

    Quote Originally Posted by .paul. View Post
    Try this...

    Code:
    Imports System.Reflection
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim dynamicType As Type = GetType(Control).Assembly.GetType("System.Windows.Forms.TextBox", True)
            ' Create an instance of dynamicType by calling Activator.CreateInstance
            Dim dynamicObject As Control = DirectCast(Activator.CreateInstance(dynamicType), Control)
            dynamicObject.Location = New Point(12, 28)
            dynamicObject.Tag = "First textbox"
            AddHandler dynamicObject.TextChanged, AddressOf ctrls_TextChanged
            Me.Controls.Add(dynamicObject)
    
            Dim dynamicType2 As Type = GetType(Control).Assembly.GetType("System.Windows.Forms.Label", True)
            ' Create an instance of dynamicType2 by calling Activator.CreateInstance
            dynamicObject = DirectCast(Activator.CreateInstance(dynamicType2), Control)
            dynamicObject.Location = New Point(12, 12)
            dynamicObject.Text = "Test"
            Me.Controls.Add(dynamicObject)
    
            Dim dynamicType3 As Type = GetType(Control).Assembly.GetType("System.Windows.Forms.Button", True)
            ' Create an instance of dynamicType3 by calling Activator.CreateInstance
            dynamicObject = DirectCast(Activator.CreateInstance(dynamicType3), Control)
            dynamicObject.Location = New Point(12, 100)
            dynamicObject.Text = "Click"
            dynamicObject.Tag = "First button"
            AddHandler dynamicObject.Click, AddressOf ctrls_Click
            Me.Controls.Add(dynamicObject)
    
        End Sub
    
        Private Sub ctrls_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
            'textbox textchange
        End Sub
    
        Private Sub ctrls_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim tb As TextBox = Me.Controls.OfType(Of TextBox).First(Function(t) t.Tag.ToString = "First textbox")
            MsgBox(DirectCast(sender, Button).Tag.ToString & " clicked" & Environment.NewLine & "TextBox.Text = " & tb.Text)
        End Sub
    
    End Class
    WOW... worked like a charm, thank you!
    On Error GoTo Hell

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.