Winforms Change Label Properties in Usercontrol from Parent Form

Multi tool use
Multi tool use


Winforms Change Label Properties in Usercontrol from Parent Form



I am trying to change the label and text of a label which is inside a UserControl from within a Parent Form method at runtime.


label


text


UserControl


Form



So within the method in parent Form I do the following to change the label properties which is inside a UserControl


Form


UserControl


public partial class Form : Form
{
public void Form_Method()
{
UserControl uc = new UserControl();
uc.UpdateLabel(true);
}
}



And the custom method inside my UserControl


UserControl


public partial Class UserControl : UserControl
{
public void UpdateLabel(bool value)
{
if (value)
{
lbl.Text = "This";
lbl.Forecolor = Color.Green;
}

if (value == false)
{
lbl.Text = "That";
lbl.Forcolor = Color.Red;
}
}
}



However when I navigated to the UserControl the label properties have not changed as I was creating a new instance of the usercontrol on the fly which
technically disappears after the method ends.


UserControl


new



So I tried creating a public property of the actual UserControl as follows


UserControl


public partial class Form : Form
{
public UserControl _uc;

public void Form_Method()
{
UserControl uc2 = new UserControl();
uc2.UpdateLabel(true);
_uc = uc2;
}
}



However it has no effect whatsoever? I have come across info of using Events or Delegates am not sure if they are the correct process for what am trying to do?


Events


Delegates





why make a new copy of the control?
– BugFinder
Jul 2 at 11:09





You forget to add the control to Controls collection of the form. Also if you are going to update an existing control, you don't need to create a new one, just find it and update it, for example this.userControl1.UpdateLabel(true);
– Reza Aghaei
Jul 2 at 11:17



Controls


this.userControl1.UpdateLabel(true);





Well if you made it _uc on the form, then _uc.UpdateLabel(true) should be enough
– BugFinder
Jul 2 at 11:20





@Kevin So get a reference to that form if you don't have one, and either get the usercontrol reference from that form, or route the update call to that form.
– Rotem
Jul 2 at 11:52





if the control is on another form, say Form2, then just do form2.UserControl1.UpdateLabel(true); Just stop creating new usercontrols in your code to change the label, because then you dont change the label from the existing usercontrol but the new created one and that is not what you need
– GuidoG
Jul 2 at 12:05



form2.UserControl1.UpdateLabel(true);









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

4UGdb DYeD6 22 sPYZy4ZaBJnNLEQVPIK kbaTf0o4EV 3 oRVbf
L7eeYaMcKHc2gJ77OdQKUAZ pJwVEjw38IfqoEo17WUDsRzOm1 Q8QBCBv,iuhos,V230IH6 d

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications