0

I have an application with many forms that user can run from tray. But when user runs 2 forms, just the last one will work, until he closes it. After the second form is closed, the first one is working again.

How to make many forms working all the time, like other programs in window (eg. many windows of the same web browser)?

Here is part of code which i found on the internet, and i am using it for showing and hidings forms.

        public MyApplicationContext()
    {

        MenuItem SmsMenuItem = new MenuItem("SMS", new EventHandler(ShowSms));
        MenuItem ExitMenuItem = new MenuItem("Wyjdź", new EventHandler(Exit));
        MenuItem HistoryMenuItem = new MenuItem("Historia", new EventHandler(ShowHistory));


        NotifyIcon notifyIcon = new NotifyIcon();
        notifyIcon.Icon = SMSapp.Properties.Resources.Icon1;
        notifyIcon.ContextMenu = new ContextMenu(new MenuItem[] { SmsMenuItem, HistoryMenuItem, ExitMenuItem });
        notifyIcon.Visible = true;
    }

    void ShowSms(object sender, EventArgs e)
            {
        if (Globals.globals.Set.DBConn)
        {
            UnitOfWork uow = new UnitOfWork();
            using (SmsForm sm = new SmsForm(uow))
            {
                if (sm.Visible)
                    sm.Focus();
                else
                    sm.ShowDialog();
            }
        }
        else
        {

            using (SmsForm sm = new SmsForm())
            {
                if (sm.Visible)
                    sm.Focus();
                else
                    sm.Show();
            }
        }
    }

    void ShowHistory(object sender, EventArgs e)
    {
        if (Globals.globals.Set.DBConn)
        {
            UnitOfWork uow = new UnitOfWork();
            using (HistoryForm sm = new HistoryForm(uow))
            {
                if (sm.Visible)
                    sm.Focus();
                else
                    sm.ShowDialog();
            }
        }
        else
        {
            using (HistoryForm sm = new HistoryForm())
            {
                if (sm.Visible)
                    sm.Focus();
                else
                    sm.ShowDialog();
            }
        }
    }

    void Exit(object sender, EventArgs e)
    {
        notifyIcon.Visible = false;
        Application.Exit();
    }
AnotherSimpleName
  • 151
  • 1
  • 1
  • 9

1 Answers1

1

The Problem is you have to use Show() instead of ShowDialog() so that the parent form and other forms are also clickable.

Also remove the using from the method, because the using disposes of the new form after the statements inside the using are executed, which makes the form disappear.

void ShowSms(object sender, EventArgs e)
{
   SmsForm sm = new SmsForm();                
   if (sm.Visible)
     sm.Focus();
   else
     sm.Show();
}

EDIT: Updated answer on new information

You don't need using for creating the Form, since the using basically does this:

try{
  SmsForm sm = new SmsForm(); 
  //some code
}
finally{
  sm.Dispose(); 
} 

I would make it like something this:

void ShowSms(object sender, EventArgs e)
{
  SmsForm sm; 
  if (Globals.globals.Set.DBConn)
  {
    UnitOfWork uow = new UnitOfWork();
    sm = new SmsForm(uow);                    
  }
  else
    sm = new SmsForm();
  if (sm.Visible)
    sm.Focus();
  else
    sm.Show();
}

Now if there is something in your UnitOfWork class that needs to be disposed, like opening a DB Connection or something than use the using in that class/method where you use Disposable object.

Bojan B
  • 2,091
  • 4
  • 18
  • 26
  • Yes, this way it's working. But what do i have to do if i need "using" statement? – AnotherSimpleName Aug 02 '16 at 07:17
  • You mean to dispose the Form? The From should be disposed when it is closed if the Form was not called as a modal dialog (with `ShowDialog()`), so you should be fine without the using. Look at this answer http://stackoverflow.com/questions/3097364/c-sharp-form-close-vs-form-dispose – Bojan B Aug 02 '16 at 07:20
  • No, i'm using "using" to run form because i have 2 possibilities of forms. If i have Database connection i'm running form with UnitOfWork, if i don't have, i'm running form with base constructor. – AnotherSimpleName Aug 02 '16 at 07:33
  • What do you mean with "2 possibilities of forms"? 2 Constructors for the same form type? Or do you internally on the form check if you have a database connection and then execute them differently? – Bojan B Aug 02 '16 at 07:59
  • I have 2 contructors - one with parameter (UnitOfWork), one without parameters. In MyApplicationContext i'm checking if i have DB connection, and then i'm calling one constructor. So each time user wants to run eg. SmsForm, my program check if i have DB connection. – AnotherSimpleName Aug 02 '16 at 08:03
  • Updated my answer, hope that helps somewhat – Bojan B Aug 02 '16 at 09:18
  • I notice that DialogResult "Cancel" on SimpleButton doesn't work with that way of creating forms. Is a way to fix it? – AnotherSimpleName Aug 02 '16 at 10:39
  • Yes, because it is not a Dialog, therefore it has no DialogResult. If you want to execute some code on the Parent in case a cancel button was pressed, a simple solution would be to add an event listener for closing `sm.Closing += Form_Close;` and set a property on the SmsForm to true in case of canceling, which you then check in the Form_Close method. – Bojan B Aug 02 '16 at 11:16