Click here to Skip to main content
15,885,674 members
Articles / Programming Languages / C#
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
26 Mar 2012CPOL 13.3K   4   3
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

C#
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.

C#
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)


Written By
Software Developer
Sri Lanka Sri Lanka
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionTypo Pin
Andrew Rissing26-Mar-12 8:02
Andrew Rissing26-Mar-12 8:02 
GeneralRe: Typo Pin
Dhanushka Madushan lk12-Apr-12 17:03
Dhanushka Madushan lk12-Apr-12 17:03 
QuestionWhat's the use-case for this? Pin
John Brett26-Mar-12 4:39
John 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.