none
to open a form from another form after closing the previously open form. RRS feed

  • Question

  • How can I open a form ( the form is already open) from another form.

    I have a form frmMain with a grdiview control in it. When I click the one of the cell in gridview,  the another form 'frmVehicle' would be  opened

    private void grvListVehicle_CellClick(object sender, DataGridViewCellEventArgs e)
            {
     frmVehicle frm = new frmVehicle(dt1);
                    frm.Show();
                }

    After the clicked event of save button in frmVehicle, the form 'frmVehicleList' should be opened. But if this form already open , it should be closed  which has been opened previously and then open again  to get latest changes in the vehicle details in gridview controll in frmVehicleList.

    Any help would be very appreciate

    Pol


    polachan

    Tuesday, January 2, 2018 8:11 AM

All replies

  • Hi!

    What about create Save event in frmVehicle and event handler in frmMain?

    private void vehicleForm_Save(object sender, EventArgs e)
    {
       if(listForm != null)
       {
          listForm.Close();
       }
    
       listForm = new VehicleListForm();
       listForm.Show();
    }

    Tuesday, January 2, 2018 10:02 AM
  • Is Oleg mentions, it is probably best to just save off the related form so you can quickly find it again. But for Winforms forms you can also use the Application.OpenedForms to get the opened forms in the app. You'd have to search this collection for your form.

    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, January 2, 2018 2:46 PM
    Moderator
  • Hi!

    What about create Save event in frmVehicle and event handler in frmMain?

    private void vehicleForm_Save(object sender, EventArgs e)
    {
       if(listForm != null)
       {
          listForm.Close();
       }
    
       listForm = new VehicleListForm();
       listForm.Show();
    }

    You could also use some modern C# language features :)

    private void vehicleForm_Save(object sender, EventArgs e)
    {
       listForm?.Close();
       listForm = new VehicleListForm();
       listForm.Show();
    }

    It sounds like you might want to use a modal form though to prevent this from being a possibility as it seems like you're trying to write a work around for something that you can deal with by using ShowDialog() instead. This way the user would be required to save or cancel the changes on the open form before they could return to the main form.


    Microsoft MVP | Visual Studio and Development Technologies (2012 - Present)

    If a post helps you in any way or solves your particular issue, please remember to use the Propose As Answer option or Vote As Helpful


    • Edited by Troy Garner Tuesday, January 2, 2018 4:14 PM
    Tuesday, January 2, 2018 4:11 PM
  • Hello polachn,

    >>it should be closed  which has been opened previously and then open again  to get latest changes in the vehicle details in gridview controll in frmVehicleList.

    You should tell us where the latest changes come from. If the latest changes comes from another form. It will be useless if you recreate a new instance because it just init some controls. If the latest changes come from database or others. and you load the gridview value in Form_Load method of current form. you just need to create a new instance to reload with your logic.

    According to your description, it sound like the latest changes come from girdview of another form. if so you just need to register a CellValueChanged event to notify other form. I can't assume you are in which situations please give me more detailed information.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 3, 2018 3:00 AM
    Moderator
  • Your logic is not working . I have applied the following script , still I can see two instance of frmVehiclelist, the form before update, and the form after update. I want to close the form before saving and show it again after update. your code is not working

    DialogResult dialogResult = MessageBox.Show("Warning", "Do you want to save the record", MessageBoxButtons.YesNo);
                        if (dialogResult == DialogResult.Yes)
                        {
                            obj.addVehicle();
                            frmVehicleList listForm = null;
                            listForm?.Close();
                            listForm = new frmVehicleList();
                            listForm.Show();
    
    
    
                        }


    polachan

    Wednesday, January 3, 2018 6:58 AM
  • Can you give the sample code how to close the previously opened form and then it open again

    Pol


    polachan

    Wednesday, January 3, 2018 6:59 AM
  • Hello polachan,

    Try the code in the Save Button.

           private void Save_Click(object sender, EventArgs e)
            {
                Application.OpenForms.Cast<Form>().Where(x=>x.Text== "frmVehicleList").ToList().ForEach(z=>z.Close());
    
                //just indicate the listform indeed closed, you could delete the line
                Thread.Sleep(1000);
    
                frmVehicleList listForm = new frmVehicleList();
                listForm.Show();      
            }

    Result

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, January 4, 2018 8:24 AM
    Moderator
  • Hello polchan,

    Is there any update or any other assistance I could provide? You could mark the helpful reply as answer if the issue has been solved. And if you have any concerns, please do not hesitate to let us know.

    Thank you for your understanding and patience!

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, January 28, 2018 12:22 PM
    Moderator