Setting value of label inside parent panel and Acessing it

ankit goel 746 Reputation points
2022-09-16T13:50:25.163+00:00

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 label1 . 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.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,820 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,194 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 47,711 Reputation points
    2022-09-16T15:28:56.067+00:00

    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.

    0 comments No comments

  2. Reza Aghaei 4,936 Reputation points MVP
    2022-09-19T20:25:34.777+00:00

    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: