question

ankitgoel-6643 avatar image
0 Votes"
ankitgoel-6643 asked reza-aghaei answered

Setting value of label inside parent panel and Acessing it

I am showing the windows form inside a panel named panel1 using the below code

directionform dr = new directionform();
dr.TopLevel = false;
panel1.Controls.Add(dr);
dr.BringToFront();
dr.Show();

my requirement is that there is also a panel name panel4 inside the panel1 and inside panel 4 there is a label name label 1 . I want to first set the value of the label1 whenever the directionform loads inside the panel4 and when it shows , i want to access the value and use it further in my mode.

dotnet-csharpwindows-forms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cooldadtx avatar image
0 Votes"
cooldadtx answered

Personally I would recommend you move your data into a standalone type. Then bind each of your UIs to that so you don't have to do this manually. However given your very specific example I assume you have a label1 field in your parent form (the one that owns panel1). I'm not clear on what you're setting this label to but let's assume some random value. It also sounds like you want access to that value in directionform. In that case you should expose a property in the form that holds the value you care about. Then set it when you set the label1 value.

public class directionform
{
   public string SomeLabel { get; set; }
}


var labelText = "Some value to set label to"
directionform dr = new directionform();
...
label1.Text = dr.SomeLabel = labelText;
dr.Show();


Now both the label and your form have the same value. Of course if you want to change that value programmatically then you'll need to ensure you keep both of them in sync. That is where data binding would be a better solution. However that requires more setup so read about data binding here.


An alternative approach is to have that label text be in a separate data class that you probably already have (a view model in other UI frameworks). Have your data class (model) implement INotifyPropertyChanged. Have the parent form and directionform both handle the change notification of the property in the data class and update themselves accordingly.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

reza-aghaei avatar image
0 Votes"
reza-aghaei answered

There are several different solutions to update the parent form's control, from the child non-top-level form. For example, you can use either of the following options:

  1. In the child form, you can find the label1 like this: var label = (Label)this.FindForm().Controls.Find("label1", true)[0];

  2. You can make the label1 Modifiers property to public and GenerateMember to true. Then find the label like this: var label = ((Form1)this.FindForm()).label1;

  3. Just define a Label property for the child form, and after you instantiated the child form, set its Label property to this.lable1.

  4. Whenever you want to set the value of label1, you can raise an event, and pass the value to the event arguments, then in the event handler in the main form, set the value of label1.

  5. Define a property of type of Action<string> for your child form, and then after you instantiate the child form, set it to (x)=> label1.Text = x;, later whenever you want to set the value, just call the action and pass the value to it.

Wanna see more example? Take a look at the following posts:

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.