locked
C# closing a form from another form RRS feed

  • Question

  • Hi guys, is there any way to close a Form from another form. I've Form1, I fill all the text box. I use the button click to open the Form2 which retrieve the last row inserted (from Form1). The user can not confirm so I use this.close in the Form2 and so the user can amend the Form1. The problem comes when the user confirm. In this case I'd like to close the Form1 with the data inserted. I can open a new Form1 but the previous Form would be available. So what I am trying to do is to close the Form1 using the button confirm in the Form2.

    Is it possible?

    Thanks

     
    Thursday, December 29, 2016 3:49 PM

Answers

  • Perhaps a conceptual example might help that shows a simple example to pass data and confirm user input. I may be off base here so let me know.

    https://1drv.ms/u/s!AtGAgKKpqdWjiBfTDwe6apftan3Q


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by Bob Ding Friday, December 30, 2016 5:06 AM
    • Marked as answer by DIEGOCTN Friday, December 30, 2016 10:29 AM
    Thursday, December 29, 2016 5:32 PM

All replies

  • Hello,

    Is form1 the main form that opened the application?


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, December 29, 2016 4:46 PM
  • At the moment yes (I am developing) but it is not supposed to be. I can change it.
    Thursday, December 29, 2016 4:53 PM
  • The reason for asking is that whatever form starts the application, when closed will close the application. If you examine Program.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }
    }
    

    Application.Run fires up Form1 then when you close Form1 a call is made ExecuteThread which closes the app.

    With that you might consider a different approach. My question now is why can't the user confirm from Form2?


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, December 29, 2016 5:08 PM
  • Thanks Karen. I will put Form3 as main. Hence which code should I use in order to achieve the schema previously mentioned?

    Thursday, December 29, 2016 5:10 PM
  • Perhaps a conceptual example might help that shows a simple example to pass data and confirm user input. I may be off base here so let me know.

    https://1drv.ms/u/s!AtGAgKKpqdWjiBfTDwe6apftan3Q


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by Bob Ding Friday, December 30, 2016 5:06 AM
    • Marked as answer by DIEGOCTN Friday, December 30, 2016 10:29 AM
    Thursday, December 29, 2016 5:32 PM
  • Thanks, Karen. I'll have a look. However I thought was simpler to close a form from another form but apparently is not.
    Friday, December 30, 2016 10:29 AM