pasing message between forms

Hi,

I am designing an application where I have a main form.Before I show the main Form, the user sees a form for connections, which in turn may load another form for a new connection.

The problem is:

While I am loading the form for connections, I may encounter an exception/or I will need to pass information to the main form. I need to know how I can do this in a simple manner ?

Any help is very much appreciated!

[447 byte] By [akin_l] at [2007-12-22]
# 1

Hi akin_l,

Do you mean something like this?



try
{
MainForm mf = new MainForm();
mf.ShowDialog();
}
catch (Exception ex)
{
//do something
}

Will try-catch solve your problem?

gqlu at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2
akin_l wrote:

The problem is:

While I am loading the form for connections, I may encounter an exception/or I will need to pass information to the main form. I need to know how I can do this in a simple manner ?

Have you tried with custom events?, your mainform holds a ref to your childforms and your childform raising events that are consumed by your mainform

RemcoJVG at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3

Hi,

The try catch will be helpful in case of errors but when everything is going fine then the second form needs to inform the main form of the various connections and display of controls on the form. I am still sorting that part out :-)

Thanks for the help !

akin_l at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 4

Hi akin_l,

Or you want the use of event handler. Here's the quote from the post.

CBDenver wrote:

I believe the most elegent solution is to pass values using event notification. I found this solution on another site and tried it to pass data between MDI child and parent -- it works fine.

1. In the child form (the form that you want to send information from), first create the event handler:

public event EventHandler NotifyParent;

2. Also in the child form, create the event handler code. Note that here we are sending the value of "textBox1.text" as data when we publish this event.

protected void OnNotifyParent()

{

NotifyParent(textBox1.text, EventArgs.Empty);

}

3. The final task in the child form is to create the code to actually fire this event we have created. Here we are firing the event when a button is clicked:

private void btnOK_Click(object sender, EventArgs e)

{

this.OnNotifyParent();


}

4. Next we need to add code to the MDI parent window to subscribe to this new event that the MDI child window will be firing when the "OK" button is clicked. First we wire up the event by adding the EventHandler to the code that creates and shows the child form:

private void addChildForm()

{

childForm = new childForm();

childForm.MdiParent = this;

childForm.NotifyParent += new EventHandler(childForm_NotifyParent); //<=== add this EventHandler

childForm.Show();

}

5. Finally, we add a method to process the event when it is received by the parent form. Here we are taking the text value sent from the child form and displaying it in a text box on the parent form. The "object sender" is the "textBox1.Text" value sent in the "OnNotifyParent" method in the child form above:

private void childForm_NotifyParent(object sender, System.EventArgs e)

{

txtParentForm.Text = sender.ToString();

}

gqlu at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 5

Hi gqlu,

Your suggestion was very helpful and I guess I will be able to solve my problem

Cheers!

akin_l at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 6

I got to know about something I was missing !

Thanks

akin_l at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 7
Glad to hear that. :-)
gqlu at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms General...