Windows Forms Button Issues

C# Code
0 0
Read Time:2 Minute, 25 Second

It’s been a while that I actually coded something in this particular Visual Studio project of mine (“Virus Total Download Checker”), but once I got into it again, I was hooked.

For a start, here’s a quick summary of my somewhere between half-finished and complete application:

  • Runs on Windows
  • Run in the background and only interact with the user when necessary
  • Hide from ALT-TAB when running in the tray
  • Configure a download folder where all your downloads from your browser(s) end up
  • Set up a FileSystemWatcher to monitor that folder
  • Whenever a new file is downloaded, send this to the VirusTotal API
  • Display a warning in the Windows Notifications when there are antivirus tools that indicate malicious software in your download

So basically, this all works more or less (there are some glitches…), but what didn’t work were the maximize and minimize buttons on the form itself (which is shown when you double-click the tray icon):

The form when shown
The form when shown to the user

Despite the internet I couldn’t find out why the two buttons weren’t shown, and the usual suggestions like

  • Set the correct FormBorderStyle
  • Make sure that MaximizeBox/MinimizeBox are set to True

didn’t apply to me, as they were set to the correct values:

Form properties
Excerpt of the main form properties

So after some more digging and going through the code, I finally found out why the buttons didn’t show up:

C# Code
C# Code (overriding CreateParams)

In my former attempts (see above, I hadn’t been working on the code for a while) I somehow seem to have had the idea of overriding CreateParams and setting the Params.ExStyle to 0x80 (WS_EX_TOOLWINDOW). This is documented as follows:

The window is intended to be used as a floating toolbar. A tool window has a title bar that is shorter than a normal title bar, and the window title is drawn using a smaller font. A tool window does not appear in the taskbar or in the dialog that appears when the user presses ALT+TAB. If a tool window has a system menu, its icon is not displayed on the title bar. However, you can display the system menu by right-clicking or by typing ALT+SPACE.

So I must have read what I wanted to read back then and the window also didn’t show up during ALT-TAB when running minimised in the tray. But if I’d read to the end I would also have seen that this hides the buttons I was missing.

So after removing this code and just setting the ShowInTaskbar property to false, the form behaves as expected. To support others that might run into the same issue, I also posted an answer on stackoverflow.com. Happy coding!

About Post Author

tim

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.