Click here to Skip to main content
12,253,371 members (50,963 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

6.6K views
4 bookmarked
Posted

Get reference to a control object on a form by passing its name

, 26 Mar 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Get reference to a control object on a form by passing its name..

Introduction

This is about how to get the control object on a form by passing the object's name. This can be useful in a situation where you want to perform a action on object by getting the reference to the object using object's name. I used this method to get the control object after getting the object's name according to a algorithm.

Using the code

private void ClickPublicButton(string name)
{
    try
    {
        if (string.IsNullOrEmpty(name))
        {
            throw new ArgumentNullException("ClickPublicButton: Needs a button name");
        }
        else
        {
            System.Reflection.FieldInfo field = this.GetType().GetField(name);
            if (field == null)
            {
                throw new ArgumentException(string.Format(
                  "ClickPublicButton: Button \"{0}\" not found", name));
            }
            else
            {
                Button bnew = field.GetValue(this) as Button;
                if (bnew == null)
                {
                    throw new ArgumentException(string.Format(
                      "ClickPublicButton: \"{0}\" is not a button", name));
                }
                else
                {
                    bnew.PerformClick();
                }
            }
        }
    }
    catch (ArgumentNullException a)
    {
        MessageBox.Show(a.Message);
    }
    catch (ArgumentException b)
    {
        MessageBox.Show(b.Message);
    }
}

But remember Type.GetField("objectName") only gets public fields. So the object we are trying to get reference should be declared as public.

If the object is declared as private we have to use the two parameter version in the GetField() method.

private void ClickPrivateButton(string name)
{
    try
    {
        if (string.IsNullOrEmpty(name))
        {
            throw new ArgumentNullException("ClickPrivateButton: Needs a button name");
        }
        else
        {
            System.Reflection.FieldInfo field = this.GetType().GetField(name,
              System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            if (field == null)
            {
                throw new ArgumentException(string.Format(
                  "ClickPrivateButton: Button \"{0}\" not found", name));
            }
            else
            {
                Button bnew = field.GetValue(this) as Button;
                if (bnew == null)
                {
                    throw new ArgumentException(string.Format(
                      "ClickPrivateButton: \"{0}\" is not a button", name));
                }
                else
                {
                    bnew.PerformClick();
                }
            }
        }
    }
    catch (ArgumentNullException a)
    {
        MessageBox.Show(a.Message);
    }
    catch (ArgumentException b)
    {
        MessageBox.Show(b.Message);
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Dhanushka Madushan lk
Software Developer
Sri Lanka Sri Lanka
No Biography provided

You may also be interested in...

Comments and Discussions

 
SuggestionTypo Pin
Andrew Rissing26-Mar-12 8:02
memberAndrew Rissing26-Mar-12 8:02 
GeneralRe: Typo Pin
Dhanushka Madushan lk12-Apr-12 17:03
memberDhanushka Madushan lk12-Apr-12 17:03 
QuestionWhat's the use-case for this? Pin
John Brett26-Mar-12 4:39
memberJohn Brett26-Mar-12 4:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 26 Mar 2012
Article Copyright 2012 by Dhanushka Madushan lk
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid