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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month11 days, 13 hours left to enroll

752 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