Home > other >  How can I open app using c# winform and close the winform without closing the opened app?
How can I open app using c# winform and close the winform without closing the opened app?

Time:06-17

I'm using this code to open ms access database :

  public partial class Start_Baseet : System.Windows.Forms.Form
    {
        string MyFile = Environment.CurrentDirectory   "\\Baseet.accde";
        Microsoft.Office.Interop.Access.Application AccApp = new Microsoft.Office.Interop.Access.Application();

        public Start_Baseet()
        {
            InitializeComponent();
        }
        public void OpenDb()
        {
            AccApp.Visible = true;
            AccApp.OpenCurrentDatabase(MyFile, false, "017014a");
            AccApp.RunCommand(AcCommand.acCmdAppMaximize);

        }
        private void Start_Basset_Load(object sender, EventArgs e)
        {
            try
            {
                OpenDb();
            }
            catch
            {
                AccApp.Quit();
                MessageBox.Show("Missing Files", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
            finally
            {
                this.Close();
            }
        }
    }

It's working but when the Finally part executed This.Close the opened access database closes , How can I use this code and close the form without closing the opened access database ? Thanks

Edit#1 : I used this but still having the problem

static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Start_Baseet());
            Start_Baseet frm = new Start_Baseet();
            frm.Show();
        }

Edit#2 I discovered another problem , I have two PC's the first running office 2016 32-bit and the other 2016 64-bit. The first one where I developed this app when I debug or release the ms access application opens but the database Baseet.accde don't open . The second PC when I try to run the released app it works fine ! why is this ??!!

CodePudding user response:

Remove the Form from Application.Run(); and open the form explicitly with frm.Show();

Instead of

Application.Run(new Start_Baseet());

Use

var frm = new Start_Baseet();
frm.Show();
Application.Run();

Now you must leave the application with Application.Exit();. Closing the form will not terminate the application any more.

  • Related