Using A Class to Convert a String to A Control....

Posted on 2005-03-01
Medium Priority
Last Modified: 2010-04-23
I have a application that I am needing to convert a string to a Control... It is a bunch of pictures that are draging and droping.....  I have a couple of  examples on how to do it from the form itself but I need to do it from a seperate class. here is the examples that I have so far...
  Public Shared Function GetControl1(ByVal ctrlName As String) As Control

        For Each ctrl As Control In me.Controls
            If ctrl.Name = ctrlName.Trim Then
                Return ctrl
            End If
        Return Nothing

    End Function
This one works fine but only from the form.

  Public Shared Function GetControl(ByVal IString As String) As Control
        GetControl = DirectCast(IString, Control)
    End Function

I haven't gotten this to work yet but I'm guessing it works fine on the original form to.

Question by:Venom1234
LVL 12

Expert Comment

ID: 13431682
Where are you expecting your function to pull the controls from?   In your first example you are pulling existing controls from a forms control collection ... but when you move your function to it's own class, are you still expecting it to pull/return controls from that particular form?
LVL 34

Expert Comment

by:Brian Crowe
ID: 13431820
you cannot DirectCast from a String to a Control

"DirectCast requires the run-time type of an object variable to be the same as the specified type"
LVL 86

Accepted Solution

Mike Tomlinson earned 375 total points
ID: 13432035
You are going to have to pass in a form to your GetControl1() function:

    Public Shared Function GetControl1(ByVal f As Form, ByVal ctrlName As String) As Control

        For Each ctrl As Control In f.Controls
            If ctrl.Name = ctrlName.Trim Then
                Return ctrl
            End If
        Return Nothing

    End Function

Alternatively, you can pass in a Form to your class and then refere to that form (instead of Me.) in your function.


Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.
Suggested Courses

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question