Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 467319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:34:07+00:00 2026-05-12T23:34:07+00:00

Is it possible to get the property of a class from string and then

  • 0

Is it possible to get the property of a class from string and then set a value?

Example:

string s = "label1.text";
string value = "new value";

label1.text = value; <--and some code that makes this

How to do this?

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T23:34:08+00:00Added an answer on May 12, 2026 at 11:34 pm

    If the given control is an instance variable on your form (if you used the built-in WinForms designer, most are), first get the control, and then set the property on it:

        void Form_SetControlProperty(
            String controlName, String propertyName, object value)
        {
            FieldInfo controlField = this.GetType().GetField(controlName, 
                BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
            object control = controlField.GetValue(this);
            PropertyInfo property = control.GetType().GetProperty(propertyName);
            property.SetValue(control, value, new object[0]);
        }
    

    You may need to tweak BindingFlags to get this to work.

    This must be a method on your form. Call it as:
    SetControlProperty(“myLabel”, “Text”, “my label text”);

    Pay attention to the scope of the method. It any control within the form, but not the form itself (to access the form itself, set control to this).

    Note that this uses reflection and will be slow and brittle (change the name of a control and it will break).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Spring MVC Controller class that needs a property set from a
Is it possible to get a unique identifier of a property from within its
Is it possible to grab an event, hold it in a class property, do
private List<T> newList; public List<T> NewList { get{return newList;} set{newList = value;} } I
I have a class with property say private string fieldSelectedItem; public string FieldSelectedItem {
Is it possible to add some additional attributes to my components which are then
Question I'm writing some code that needs to be able to get the values
Is this possible? I am creating a an object from a JSON string with
I'm wondering if it is possible to access a property of a class dynamically
say I have the attribute: public class Column_Attribute : Attribute { public string DbType

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.