473,396 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Notify Icon and Minimise Hide/Show Problems

384 256MB
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!??
Expand|Select|Wrap|Line Numbers
  1. private void Form_Resize(object sender, System.EventArgs e)
  2. {
  3.    if (FormWindowState.Minimized == WindowState)
  4.          this.ShowInTaskbar = false;
  5.    if (FormWindowState.Maximized == WindowState)
  6.          this.ShowInTaskbar = true;
  7. }
  8. private void notifyIconMain_DoubleClick(object sender, System.EventArgs e)
  9. {
  10.    Show();
  11.    WindowState = FormWindowState.Normal;
  12. }
Also can anyone tell me how to override the 'X' to minimise the app. and not exit it?
Sep 19 '08 #1
20 4501
Plater
7,872 Expert 4TB
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.

Expand|Select|Wrap|Line Numbers
  1. private void Form_Resize(object sender, System.EventArgs e)
  2. {
  3.    if (WindowState == FormWindowState.Minimized )
  4.    {
  5.          this.Visible=false;
  6.    }
  7. }
  8. private void notifyIconMain_DoubleClick(object sender, System.EventArgs e)
  9. {
  10.    this.Visible=true;
  11.    WindowState = FormWindowState.Normal;
  12. }
  13.  
Sep 19 '08 #2
balabaster
797 Expert 512MB
To stop the form closing you catch it in the form's FormClosing event...
Expand|Select|Wrap|Line Numbers
  1. e.Cancel = True
Sep 19 '08 #3
Curtis Rutland
3,256 Expert 2GB
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
Expand|Select|Wrap|Line Numbers
  1. private void BBCodeTextEditor_FormClosing(object sender, FormClosingEventArgs e)
  2. {
  3.     //if windows is shutting down, or the user ends task from the task manager, don't ask questions
  4.     if (e.CloseReason != CloseReason.WindowsShutDown && e.CloseReason != CloseReason.TaskManagerClosing)
  5.     {
  6.         //if my dialog is OK, then close
  7.         if (this.DialogResult == DialogResult.OK)
  8.         {
  9.             e.Cancel = false;
  10.             return;
  11.         }
  12.         //otherwise, if my cancelled flag is set, close
  13.         if (this.cancelled)
  14.         {
  15.             e.Cancel = false;
  16.             return;
  17.         }
  18.         //otherwise, if my text isn't dirty, close
  19.         if (this.EditorText.Equals(this.originalText))
  20.         {
  21.             e.Cancel = false;
  22.             return;
  23.         }
  24.         //if we got here, then we aren't sure if we want to close or not
  25.         else
  26.         {
  27.             string message = Properties.Resources.sCloseFormMessage;
  28.             string caption = Properties.Resources.sCloseFormCaption;
  29.             //show a dialog to ask the user
  30.             DialogResult res = MessageBox.Show(message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
  31.             if (res == DialogResult.No)
  32.                 //user does not want to close, cancel closing
  33.                 e.Cancel = true;
  34.             else
  35.                 //user confirmed closing, continue
  36.                 e.Cancel = false;
  37.         }
  38.     }
  39. }
  40.  
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.
Sep 19 '08 #4
ziycon
384 256MB
This is the code i have now and none of it works, any i doing something wrong??

Expand|Select|Wrap|Line Numbers
  1. private void Form_Resize(object sender, System.EventArgs e)
  2. {
  3.      if (WindowState == FormWindowState.Minimized)
  4.      {
  5.          this.Visible = false;
  6.      }
  7. }
  8. private void notifyIconMain_DoubleClick(object sender, System.EventArgs e)
  9. {
  10.     this.Visible = true;
  11.     WindowState = FormWindowState.Normal;
  12. }
  13. private void Form_FormClosing(object sender, FormClosingEventArgs e)
  14. {
  15.     e.Cancel = true;
  16.     this.WindowState = FormWindowState.Minimized;
  17. }
Sep 19 '08 #5
Curtis Rutland
3,256 Expert 2GB
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.
Sep 19 '08 #6
ziycon
384 256MB
I don't know how to set Event handlers so thats a no!?
Sep 19 '08 #7
Curtis Rutland
3,256 Expert 2GB
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.
Sep 19 '08 #8
ziycon
384 256MB
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??
Sep 19 '08 #9
Curtis Rutland
3,256 Expert 2GB
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.
Sep 19 '08 #10
ziycon
384 256MB
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!?
Expand|Select|Wrap|Line Numbers
  1.  private void frmMain_Resize(object sender, EventArgs e)
  2. {
  3.     if (WindowState == FormWindowState.Minimized)
  4.     {
  5.          this.Visible = false;
  6.     }
  7. }
  8. private void notifyIconMain_Click(object sender, EventArgs e)
  9. {
  10.      this.Visible = true;
  11.      WindowState = FormWindowState.Normal;
  12. }
Sep 19 '08 #11
Plater
7,872 Expert 4TB
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)
Sep 19 '08 #12
Curtis Rutland
3,256 Expert 2GB
Sorry, I don't quite understand your question. Can you please phrase it a bit more clearly?
Sep 19 '08 #13
ziycon
384 256MB
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:
Expand|Select|Wrap|Line Numbers
  1. private void notifyIconMain_Click(object sender, EventArgs e)
  2. {
  3.     this.Visible = true;
  4.     WindowState = FormWindowState.Normal;
  5. }
I have mouseClick and mouseDoubleClick of the notifyIcon set to the above function!??
Sep 19 '08 #14
ziycon
384 256MB
Got it sorted, thanks!
Sep 19 '08 #15
Curtis Rutland
3,256 Expert 2GB
Well, I'm not sure what's wrong with that code...
I like to do it the way you first suggested. Using this.ShowInTaskbar.

Here's what my methods for this look like:
Expand|Select|Wrap|Line Numbers
  1. private void BDAMainForm_Resize(object sender, EventArgs e)
  2. {
  3.     if (this.WindowState == FormWindowState.Minimized)
  4.         this.ShowInTaskbar = false;
  5.     else
  6.         this.ShowInTaskbar = true;
  7. }
  8.  
  9. //notify icon click event handler
  10. private void niBda_MouseClick(object sender, MouseEventArgs e)
  11. {
  12.     if (e.Button == MouseButtons.Left)
  13.         this.WindowState = FormWindowState.Normal;
  14. }
The one difference I see is that you use an EventArgs instead of MouseEventArgs, but that shouldn't matter.
Sep 19 '08 #16
ziycon
384 256MB
Expand|Select|Wrap|Line Numbers
  1. private void BBCodeTextEditor_FormClosing(object sender, FormClosingEventArgs e)
  2. {
  3.     //if windows is shutting down, or the user ends task from the task manager, don't ask questions
  4.     if (e.CloseReason != CloseReason.WindowsShutDown && e.CloseReason != CloseReason.TaskManagerClosing)
  5.     {
  6.         //if my dialog is OK, then close
  7.         if (this.DialogResult == DialogResult.OK)
  8.         {
  9.             e.Cancel = false;
  10.             return;
  11.         }
  12.         //otherwise, if my cancelled flag is set, close
  13.         if (this.cancelled)
  14.         {
  15.             e.Cancel = false;
  16.             return;
  17.         }
  18.         //otherwise, if my text isn't dirty, close
  19.         if (this.EditorText.Equals(this.originalText))
  20.         {
  21.             e.Cancel = false;
  22.             return;
  23.         }
  24.         //if we got here, then we aren't sure if we want to close or not
  25.         else
  26.         {
  27.             string message = Properties.Resources.sCloseFormMessage;
  28.             string caption = Properties.Resources.sCloseFormCaption;
  29.             //show a dialog to ask the user
  30.             DialogResult res = MessageBox.Show(message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
  31.             if (res == DialogResult.No)
  32.                 //user does not want to close, cancel closing
  33.                 e.Cancel = true;
  34.             else
  35.                 //user confirmed closing, continue
  36.                 e.Cancel = false;
  37.         }
  38.     }
  39. }
this.cancelled
this.EditorText
Can you explain what these variables mean? They throw errors and can't figure out where there coming from!?
Sep 19 '08 #17
Curtis Rutland
3,256 Expert 2GB
That was just an example from a project I'd been working on. Not meant to be a copy+paste solution, just an example of a real-life application. Those are members of my form.
Sep 19 '08 #18
ziycon
384 256MB
I understand, thanks, I'll work it out myself then, appreciate all your help. Thanks again.
Sep 19 '08 #19
ziycon
384 256MB
Should this not work? When a user clicks on 'X' is that not considered CloseReason.UserClosing?
Expand|Select|Wrap|Line Numbers
  1. private void Form_Closing(object sender, FormClosingEventArgs e)
  2. {
  3.     if (e.CloseReason != CloseReason.WindowsShutDown && e.CloseReason != CloseReason.TaskManagerClosing)
  4.     {
  5.         if (this.DialogResult == DialogResult.OK)
  6.         {
  7.             e.Cancel = false;
  8.             return;
  9.         }
  10.         if ((e.CloseReason == CloseReason.ApplicationExitCall) || (e.CloseReason == CloseReason.UserClosing))
  11.         {
  12.             this.Visible = true;
  13.             WindowState = FormWindowState.Normal;
  14.         }
  15.     }
  16. }
Sep 19 '08 #20
Curtis Rutland
3,256 Expert 2GB
Are you actually setting this.DialogResult somewhere?

I feel like I may have lead you down the wrong path by giving you that sample...
Sep 19 '08 #21

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Lalit | last post by:
Hi Friends, I have developed a Windows service. Now i need icon for this service in systray and context menu fo this icon. Can i do this? With regards, Lalit
2
by: shagshag | last post by:
I know how to place a notify icon in the system tray and how to hide the Form that owe the icon. The taskbar entry button is hidden, ok, but I always get an entry for the Form when I press...
3
by: Claire | last post by:
Windows refuses to close down if my applications main form is hidden and there's a notify icon in the system tray. If I restore the form, then shutting down Windows succeeds. If I comment out the...
4
by: Beenish Sahar Khan | last post by:
I want to start my application so that its notify icon is added to the system tray, i don't want to show any starting form. I want user to interact through the notify icon...just like MSN. Is there...
0
by: sanjana | last post by:
hi i want to write a C# .net code to display a balloon in the task bar i have used notifyicon class to get the icon in the task bar then i have used Shell_NotifyIcon and NotifyIconData structure...
1
by: Rob | last post by:
I have an interesting problem with my Notify Icon. I cannot get it to display when I start a web service. I have no problems when I test the software on a Windows program. But the Icon will not...
3
by: Cylix | last post by:
I am going to make an application in monitor and do something on time. A part of it is an Notify icon(System Tray Icon), Which project type can I be used for Just showing the notify icon without...
1
by: namrataa | last post by:
i have a notify icon for our media player developed in wpf. included files are notify.cs,notify.resx,notify.designer.cs. i have also added a class file called app.cs . now the problem is how...
0
by: namrataa | last post by:
i have the following code for my notify.cs file using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Text; ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.