C# - Pass value from non-modal child to parent window

(I'm new to C#, so I'm still learning some of the techniques and differences compared to VB.)

I'm developing a windows application in C#.  I have a 'parent' form with several text fields.  Some of the fields have lookups (via a button) so that a user can lookup a pre-defined value.

The lookup button opens a lookup window with a listview, and the user can select a single value.  That single value should be returned back to the parent window.

Unfortunately, due to a bug in an integration framework that I'm having to use, I am unable to open the lookup as a modal window.  It MUST be opened as a non-modal window.  Because of this, I can't use standard simple techniques of setting a property on the lookup window and having the parent read it after it closes (which works great with a modal window).

I'm thinking instead that I will have the lookup window set a property on the parent window to pass the value back before it is closed.  I've got the property coded on the parent, but here is my question / problem:

How do I reference the 'parent' form from the lookup window?  i.e. how can the lookup window set the properties of the parent window?

Do I need to have the parent instantiate the lookup window in a particular manner?  Is there some simple method by which the lookup window can reference and set the property of the parent window?

Or must I use delegates to pass the value back to the parent, as if it were a sibling window, as described here:  (see "Sibling to Sibling")

http://www.colinmackay.net/tabid/58/Default.aspx

Thanks,

Steve

LVL 18
Steve EndowMicrosoft MVP - Dynamics GPAsked:
Who is Participating?
 
Miguel OzSoftware EngineerCommented:
In you child form define a property that refers to the parent form.
When you create the child form setup the property parent.
On the child when the user user selects an option you set it up in the parent (You can do it better in the OK button - in case user wants to cancel it)
0
 
AshokCommented:
My VS 2008 is currenlty broken.

You could declare
public static Form mainForm;
public static string sStoreMe;

after form is loaded,
SET
mainForm = YourActualMainFormName;

in your Child Form,
try
mainForm.sStoreMe = "Store value before closing the Child Form";

Unfortunately I cannot test because VS 2008 is taking too long to open.

HTH
Ashok
0
 
AshokCommented:
 public partial class Form1 : Form
  {
    public static string sStoreMe;  // Declare public variable here.....
   
    public Form1()
    {
      InitializeComponent();
    }

in your Child Form,
try
Form1.sStoreMe = "Store value before closing the Child Form";

HTH
Ashok
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
BIOGENIUSCommented:
the most efficient and profissional way is to use events.
you put a delegate and event in your child control and subscribe on this event in the main form.
it simply will work so fine and i use this way always.

if you don't know how how to do that i will be here to show you.
0
 
Steve EndowMicrosoft MVP - Dynamics GPAuthor Commented:
Hi ashok and mas,

Thanks for your responses.

@ashok111:  Because the child form has not instantiated the parent, if I attempt to reference Form1 from the child window, it is only referencing the base class and it is not exposing the public properties of the specific instance of the parent window.  The public properties of Form1 are only available if I instantiate the class.  Such as:

MainForm Form1 = new MainForm;
Form1.ItemNumber = "ABC123";

Let me know if I'm missing something.


@mas_oz2003:  It looks like your solution works.  I'm guessing that it's not as technically correct as the delegate approach, but it sure is much simpler, as I was able to get it to work with just a few lines.   I have your approach working, and I also have the delegate solution working, so I'll have to choose one.

Thanks,

Steve
0
 
Steve EndowMicrosoft MVP - Dynamics GPAuthor Commented:
@BIOGENIUS:  Thanks, I agree that delegates and events is probably the most "professional".  I now have the code for the events working, but was just wondering if there was a simpler way.

It does appear that @mas_oz's solution does work, but I can see that the downside is that the child window class is now tightly tied to the interface of the parent window class.

I think the best way would be to have modal windows working and utilize the "using" statement, which would work great, but I'm stuck with a workaround for now.
0
 
Steve EndowMicrosoft MVP - Dynamics GPAuthor Commented:
Thanks all for the input!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.