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.