Notify Icon and Minimise Hide/Show Problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ziycon
    Contributor
    • Sep 2008
    • 384

    Notify Icon and Minimise Hide/Show Problems

    I have this code and when i minimise the application should disappear from the taskbar and only be visible in the tray and when i double click on the tray icon it show maximise the app. but neither functions are working, i can display the notifyicon in the tray fine, thats it!??
    Code:
    private void Form_Resize(object sender, System.EventArgs e)
    {
       if (FormWindowState.Minimized == WindowState)
             this.ShowInTaskbar = false;
       if (FormWindowState.Maximized == WindowState)
             this.ShowInTaskbar = true;
    }
    private void notifyIconMain_DoubleClick(object sender, System.EventArgs e)
    {
       Show();
       WindowState = FormWindowState.Normal;
    }
    Also can anyone tell me how to override the 'X' to minimise the app. and not exit it?
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    All you are doing is changing the show in taskbar property. You shouldn't have to change that at all.

    Use the .Visible property instead.

    Code:
    private void Form_Resize(object sender, System.EventArgs e)
    {
       if (WindowState == FormWindowState.Minimized )
       {
             this.Visible=false;
       }
    }
    private void notifyIconMain_DoubleClick(object sender, System.EventArgs e)
    {
       this.Visible=true;
       WindowState = FormWindowState.Normal;
    }

    Comment

    • balabaster
      Recognized Expert Contributor
      • Mar 2007
      • 798

      #3
      To stop the form closing you catch it in the form's FormClosing event...
      Code:
      e.Cancel = True

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        To your second question:

        You will need to handle the FormClosing Event. Use conditions to determine whether you want to close the form or not. If you do, do nothing. Otherwise, change e.Cancel to true.

        Here's an example of a FormClosing event handler
        [code=C#]private void BBCodeTextEdito r_FormClosing(o bject sender, FormClosingEven tArgs e)
        {
        //if windows is shutting down, or the user ends task from the task manager, don't ask questions
        if (e.CloseReason != CloseReason.Win dowsShutDown && e.CloseReason != CloseReason.Tas kManagerClosing )
        {
        //if my dialog is OK, then close
        if (this.DialogRes ult == DialogResult.OK )
        {
        e.Cancel = false;
        return;
        }
        //otherwise, if my cancelled flag is set, close
        if (this.cancelled )
        {
        e.Cancel = false;
        return;
        }
        //otherwise, if my text isn't dirty, close
        if (this.EditorTex t.Equals(this.o riginalText))
        {
        e.Cancel = false;
        return;
        }
        //if we got here, then we aren't sure if we want to close or not
        else
        {
        string message = Properties.Reso urces.sCloseFor mMessage;
        string caption = Properties.Reso urces.sCloseFor mCaption;
        //show a dialog to ask the user
        DialogResult res = MessageBox.Show (message, caption, MessageBoxButto ns.YesNo, MessageBoxIcon. Question, MessageBoxDefau ltButton.Button 2);
        if (res == DialogResult.No )
        //user does not want to close, cancel closing
        e.Cancel = true;
        else
        //user confirmed closing, continue
        e.Cancel = false;
        }
        }
        }
        [/code]

        You can see how you use conditions to determine if you want to close or not, and how to prevent closing. I would suggest you use that first line, you don't want your app holding up Windows shutting down.

        Comment

        • ziycon
          Contributor
          • Sep 2008
          • 384

          #5
          This is the code i have now and none of it works, any i doing something wrong??

          Code:
          private void Form_Resize(object sender, System.EventArgs e)
          {
               if (WindowState == FormWindowState.Minimized)
               {
                   this.Visible = false;
               }
          }
          private void notifyIconMain_DoubleClick(object sender, System.EventArgs e)
          {
              this.Visible = true;
              WindowState = FormWindowState.Normal;
          }
          private void Form_FormClosing(object sender, FormClosingEventArgs e)
          {
              e.Cancel = true;
              this.WindowState = FormWindowState.Minimized;
          }

          Comment

          • Curtis Rutland
            Recognized Expert Specialist
            • Apr 2008
            • 3264

            #6
            Are you sure that you have registered the event handlers?

            Also, as I said in an earlier post, if you don't check to see if the close reason is windows shutdown, you can cause problems.

            Comment

            • ziycon
              Contributor
              • Sep 2008
              • 384

              #7
              I don't know how to set Event handlers so thats a no!?

              Comment

              • Curtis Rutland
                Recognized Expert Specialist
                • Apr 2008
                • 3264

                #8
                Originally posted by ziycon
                I don't know how to set Event handlers so thats a no!?
                OK, make sure that your Form is selected in the designer window. Look in the properties menu, and click the lightning bolt. You should see something like this:


                Now, find the events you want to handle, and click the dropdown, and choose the correct method.

                That should work.

                Comment

                • ziycon
                  Contributor
                  • Sep 2008
                  • 384

                  #9
                  Genius, working now, only thing that i can't get to work is when you click on the tray icon the form wont show again, what event do i use for that 'click' mouseclick' etc??

                  Comment

                  • Curtis Rutland
                    Recognized Expert Specialist
                    • Apr 2008
                    • 3264

                    #10
                    Well, you have to select your NotifyIcon object and do the same thing. Check out the screenshot:

                    You can see that I have my notify icon selected, and that the properties window shows the events for it, instead of the form.

                    Comment

                    • ziycon
                      Contributor
                      • Sep 2008
                      • 384

                      #11
                      I understand you now, got all that work, last thing about his i promise.

                      I have this, how do i say that when the icon in the tray is clicked that the frmMain that was made this.Visible = false on minimise is shown again instead of creating a new form as i can see how the this.Visible = true would apply to the hiden form!?
                      Code:
                       private void frmMain_Resize(object sender, EventArgs e)
                      {
                          if (WindowState == FormWindowState.Minimized)
                          {
                               this.Visible = false;
                          }
                      }
                      private void notifyIconMain_Click(object sender, EventArgs e)
                      {
                           this.Visible = true;
                           WindowState = FormWindowState.Normal;
                      }

                      Comment

                      • Plater
                        Recognized Expert Expert
                        • Apr 2007
                        • 7872

                        #12
                        That should be correct as is, assuming that the handler functions are located withen the form's class structure. (That is the default behavior when using visual studio to attach event handlers, as insert's instructions showed)

                        Comment

                        • Curtis Rutland
                          Recognized Expert Specialist
                          • Apr 2008
                          • 3264

                          #13
                          Sorry, I don't quite understand your question. Can you please phrase it a bit more clearly?

                          Comment

                          • ziycon
                            Contributor
                            • Sep 2008
                            • 384

                            #14
                            When i click the minimise button on Form A it will make Form A 'invisible' and when i click on the icon in the Tray it will make Form A visible again, does the this.Visible = true; effect the notiyIcon or the Form A when i click on the icon in the Tray?
                            Its not making the Form A visible when i click the icon in the Tray with the below code:
                            Code:
                            private void notifyIconMain_Click(object sender, EventArgs e)
                            {
                                this.Visible = true;
                                WindowState = FormWindowState.Normal;
                            }
                            I have mouseClick and mouseDoubleClic k of the notifyIcon set to the above function!??

                            Comment

                            • ziycon
                              Contributor
                              • Sep 2008
                              • 384

                              #15
                              Got it sorted, thanks!

                              Comment

                              Working...