4

I've been searching for a solution for couple of days now, but can't find a simple answer. I've tried a number of examples found on internet (delegates, properties, even breaking OOP making everything public) but none of these seem to work. Can someone please write a simplest possible code for the following problem:

I have MDI parent form, and a child form. MDI parent form has a status-strip label. Child form has a button. All I want to do is update the MDI label on click of child form button.

Thanks!!!

Alex
  • 398
  • 1
  • 6
  • 18

2 Answers2

7

It is not the best solution. However, it is the easiest one:

1- Change the access modifier of the status-strip label to public.

2- Unbox the parent form to its real type to be able to access the label:

((ActualMdiParentFormType) this.MdiParent).statusStripLabel.Text = "Value";
Akram Shahda
  • 14,655
  • 4
  • 45
  • 65
  • 3
    a "cleaner" solution would be to add a member to the parent form that does change the label.text – DarkSquirrel42 Apr 16 '11 at 09:33
  • 1
    @ DarkSquirrel42: Add your comment as an answer. – Homam Apr 16 '11 at 17:28
  • i didnt got this one first time i read. but now i got it and i would like to write my answer... ((Form1)this.MdiParent).statusStripLabel.Text = "Value"; this is the one i got it. thanks . – Erdinç Sep 12 '14 at 12:16
1

There is another solution which is to create an event in the child window and register the parent window to that event. In case that the event fires, the parent window will be notified and in the corresponding event handler of the parent window we can update OUR control.

This is a more "MVVM" like approach.

Check these links for more information:

Pass value between forms using events

http://www.c-sharpcorner.com/uploadfile/yougerthen/mvvm-implementation-for-windows-forms/

MVVM: Tutorial from start to finish?

Hope that helps,

Community
  • 1
  • 1
Ramon Araujo
  • 1,743
  • 22
  • 31