user2529573
user2529573

Reputation: 1

How to expose a label in a user control with c#

I have made a user control with a pic box and a blank label. How do I expose the label so I can update the text value from my main .net application. I have not written any c# code in about 10 years, and was just thrown a project. All I have for code is:

namespace RHeader1
{
   public partial class RHeader : UserControl
   {
      public RHeader()
      {
         InitializeComponent();
      }
   }
}

Please for give my stupidity. I know I need to do a get/set but?????

Upvotes: 0

Views: 1478

Answers (4)

Amr
Amr

Reputation: 50

if you haven't delclared the label:

label1 = new label();

then

label1.text = "some text";

where you can replace "Some text" with any string value.

Upvotes: 0

Romaine Carter
Romaine Carter

Reputation: 655

You could change the Label's modifier property to public, this will result in the labels properties been visible from the UC's property window and also allow you to do something like

uc.label.Text = "foo";

Upvotes: 0

NDJ
NDJ

Reputation: 5194

I presume you mean because controls are not public, the proper way to access them are via a property (which I agree with) - so you can just expose a property which updates the label directly - I'm presuming this is winforms

        public string Label
        {
            get { return label1.Text; }
            set { label1.Text = value; }
        }

Upvotes: 1

gericooper
gericooper

Reputation: 252

Use this:

Label lbl= (Label)myUserControl.FindName("yourlabelname");

This way you can find and update your label control settled in the UserControl.

Upvotes: 0

Related Questions