Dinesh Dinesh - 1 year ago 69
C# Question

How to open a new form from another form

I have form which is opened using ShowDialog Method. In this form i have a Button called More.
If we click on More it should open another form and it should close the current form.

on More Button's Click event Handler i have written the following code

MoreActions objUI = new MoreActions ();

But what is happening is, it's not closing the first form. So, i modified this code to

MoreActions objUI = new MoreActions ();

Here, The second form is getting displayed and within seconds both the forms getting closed.

Can anybody please help me to fix issue. What i need to do is, If we click on More Button, it should open another form and close the first form.

Any kind of help will be really helpful to me.

Answer Source

In my opinion the main form should be responsible for opening both child form. Here is some pseudo that explains what I would do:

// MainForm
private ChildForm childForm;
private MoreForm moreForm;

    childForm = CreateTheChildForm();
    childForm.MoreClick += More_Click;

    moreForm = new MoreForm();

You will just need to create a simple event MoreClick in the first child. The main benefit of this approach is that you can replicate it as needed and you can very easily model some sort of basic workflow.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download