I have Window Forms application and I'm using ToolStripDropDown as context menu for my form.
public partial class Form1 : Form
{
ToolStripDropDownMenu _formContextMenu = null;
public Form1()
{
InitializeComponent();
_formContextMenu = new ToolStripDropDownMenu();
_formContextMenu.Items.Add("Item1");
_formContextMenu.Items.Add("Item2");
}
protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
_formContextMenu.Show(e.Location);
}
else
base.OnMouseDown(e);
}
protected override void OnResize(EventArgs e)
{
_formContextMenu.Close();
base.OnResize(e);
}
}
First I click on form with Right Mouse button to show context menu. After that I press Win+D (minimize all windows) and then open my application again. Context menu remains opened and also it doesn't close when I click on form. I even can move form to other place but it will still remains opened.
I also tried to close it with different reasons.
_formContextMenu.Close(ToolStripDropDownCloseReason.AppClicked);
_formContextMenu.Close(ToolStripDropDownCloseReason.AppFocusChange);
_formContextMenu.Close(ToolStripDropDownCloseReason.CloseCalled);
_formContextMenu.Close(ToolStripDropDownCloseReason.ItemClicked);
_formContextMenu.Close(ToolStripDropDownCloseReason.Keyboard);
Doesn't help.
Can someone help with this issue?
I used this workaround for the problem and it works properly:
protected override void OnSizeChanged(EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
//_formContextMenu or this.contextMenuStrip1
this.contextMenuStrip1.Visible = true;
this.contextMenuStrip1.Close();
}
base.OnSizeChanged(e);
}
While this problem also occurs for ContextMenuStrip but if there is not specific goal for using ToolStripDropDownMenu you can use ContextMenuStrip component and Set ContextMenuStrip property of form. This way you don't need to write code to show context menu.
But if in any reason you preferred showing in code, consider showing the context menu/ dropdown menu this way: yourContextMenu.Show(this,e.Location);
Related
I cannot work out where to put nor get the code to trigger when my main form windows are resized (ie minimize button clicked)
I am trying to trigger this code when ANY resize of the DigiDocketMain window is minimized etc, or also how I can specifically code the minimize button to do something - the ideal goal is to get the program - n minimize button click to hide the taskbar icon and show a tray icon.
I have tried placing this is the main code body and the designer code but nothing triggers it. any help would be appreciated.
private void DigiDocketMain_Resize(object sender, System.EventArgs e)
{
MessageBox.Show("You are in the Form.ResizeEnd event.");
if (this.WindowState == System.Windows.Forms.FormWindowState.Minimized)
{
this.Hide();
mainTrayIcon.Visible = true;
}
}
In your code behind add the following to the Form_Load Event
this.SizeChanged += Form1_SizeChanged;
Then implement the function, autocomplete may do this for you.
private void Form1_SizeChanged(object sender, EventArgs e)
{
// Add the code that will be called on resize events.
}
According to your description, when clicking the minimize button, you want to hide the
taskbar icon and display the tray icon.
I suggest that you set the Visible of notifyIcon1 to false in the property bar, and select a icon format image as the icon, then try the following code.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Deactivate(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.notifyIcon1.Visible = true;
this.Hide();
this.ShowInTaskbar = false;
}
}
private void notifyIcon1_Click(object sender, EventArgs e)
{
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.notifyIcon1.Visible = false;
this.ShowInTaskbar = true;
}
}
I got 2 forms for a application and I am using a system tray to get it as a popup. But when I enter my username at my first form and open my second form and click at the popup icon in the bottom it doesn't work for the second form anymore.
I also have add this code at my second form.
here is the code;
private void Form1_Resize(object sender, EventArgs e)
{
if (FormWindowState.Minimized == this.WindowState)
{
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
else if (FormWindowState.Normal == this.WindowState)
{
notifyIcon1.Visible = false;
}
}
Maybe because you have the name Form1 and Form2's resize event is not pointing to the correct function.
In design time, open the Form, press F4 to bring up the Properties window, click the Lightning Bolt icon to see the Forms events, then scroll down to Resize and choose Form2_Resize:
private void Form2_Resize(object sender, EventArgs e)
I am trying to build a total new window acts as Context Menu.
the only problem i have is: when I am pressing the mouse buttons outside the window (ContextMenu), the window does not close. I can't find the event that can catch this action.
this is the code i am using now:
public partial class ContextMenu : Window
{
public ContextMenu()
{
InitializeComponent();
this.ShowInTaskbar = false;
this.Deactivated += new EventHandler(ContextMenu_Deactivated);
}
void ContextMenu_Deactivated(object sender, EventArgs e)
{
this.Hide();
}
protected override void OnDeactivated(EventArgs e)
{
base.OnDeactivated(e);
this.Hide();
}
protected override void OnMouseDown(MouseButtonEventArgs e)
{
base.OnMouseDown(e);
this.Hide();
}
protected override void OnKeyDown(System.Windows.Input.KeyEventArgs e)
{
base.OnKeyDown(e);
this.Hide();
}
protected override void OnLostFocus(RoutedEventArgs e)
{
base.OnLostFocus(e);
this.Hide();
}
}
non of the functions above catches the mouse press outside the window (ContextMenu).
I have tried to use http://www.hardcodet.net/taskbar, but the examples I found are not something like what i am looking for.
Looks like you need processing of global mouse hooks.
Here is nice solution to this issue
http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C
A control cannot detect mouse clicks that are outside their bounding Rectangles. However, the Window can detect a mouse click anywhere within its border. Therefore, all you need to do is to handle a PreviewMouseDown event in the MainWindow.xaml.cs file and then pass a message to the relevant control each time the event is raised.
I believe you'll want to use Mouse.Capture to detect a click away from your window.
This question+answer may lead you in the right direction:
How do I use CaptureMouse or Mouse.Capture in my C# WPF application?
I'm working on a Windows Forms app and I'm wanting to remove the close button from the top. I'm aware of the ControlBox option, but I'm wanting to provide a help button. Is there a way to have the Close button not visible while maintaining the help button?
Your best bet may be to subcribe to the FormClosing event of the form like so and cancel the closing action:
// In your code somewhere subscribe to this event
Form1.FormClosing += Form1_FormClosing;
void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}
The benefit of doing this is that it prevents the user from closing the application from the close button and the taskbar.
Obviously you don't want to ALWAYS cancel the form from closing. So you will want to set some type of boolean flag that you will check in the event listener as to whether you want the form to be allowed to close or not. Example:
void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (BlockClosing)
e.Cancel = true;
}
EDIT: If you don't want to approach the problem that way, and you really do intend to completely remove the close button, then your best bet is to create your own custom title bar. In that case, you set the form's FormBorderStyle property to None. And you then dock your custom title bar to the top of the form. Here is some sample code from one I made a while back:
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace Spectrum.UI
{
public partial class TitleBar : UserControl
{
public delegate void EventHandler(object sender, EventArgs e);
public event EventHandler MinButtonClick;
public event EventHandler MaxButtonClick;
public event EventHandler CloseButtonClick;
#region Properties
[Category("Appearance")]
public string Title
{
get { return TitleLabel.Text; }
set { TitleLabel.Text = value; }
}
[Category("Appearance")]
public bool MinimizeEnabled
{
get
{
return minButton.Visible;
}
set
{
minButton.Visible = value;
}
}
[Category("Appearance")]
public bool MaximizeEnabled
{
get
{
return maxButton.Visible;
}
set
{
maxButton.Visible = value;
}
}
#endregion
public TitleBar()
{
InitializeComponent();
ShowTitleBarImage = false;
}
#region Mouse Events
private void TitleBar_MouseDown(object sender, MouseEventArgs e)
{
this.OnMouseDown(e);
}
private void TitleBar_MouseUp(object sender, MouseEventArgs e)
{
this.OnMouseUp(e);
}
private void TitleBar_MouseMove(object sender, MouseEventArgs e)
{
this.OnMouseMove(e);
}
#endregion
#region Button Click Events
private void minButton_Click(object sender, EventArgs e)
{
if (MinButtonClick != null)
this.MinButtonClick.Invoke(this, e);
}
private void maxButton_Click(object sender, EventArgs e)
{
if (MaxButtonClick != null)
this.MaxButtonClick.Invoke(this, e);
}
private void closeButton_Click(object sender, EventArgs e)
{
if (CloseButtonClick != null)
this.CloseButtonClick.Invoke(this, e);
}
#endregion
}
}
As you can see from the image, I also added a background image to the control. Depending on your patience and your requirements, you can use images and PictureBox controls to make this look as much like a standard title bar as you need.
In the above example I placed three buttons on the control with images I found online to represent minimize, maximize, and close. in your case you would simply exclude a close button. I also placed a string on the control with an appropriate font to serve as the title of the window.
Adding the custom title bar to your form is easy.
public TitleBar titleBar = new TitleBar();
titleBar.Dock = DockStyle.Top;
titleBar.MaximizeEnabled = true;
titleBar.MinimizeEnabled = true;
titleBar.Size = new System.Drawing.Size(10, 40); // Width doesn't matter - I wanted it 40 pixels tall
titleBar.Title = "Title Example";
titleBar.MinButtonClick += titleBar_MinButtonClick;
titleBar.Max ButtonClick += titleBar_MaxButtonClick;
this.Controls.Add(this.TitleBar);
And then last step is to set up your event listeners for the min and max button clicks:
private void titleBar_MinButtonClick(object sender, EventArgs e)
{
WindowState = FormWindowState.Minimized;
}
private void titleBar_MaxButtonClick(object sender, EventArgs e)
{
WindowState = FormWindowState.Maximized;
}
You may also note that I included events for mouse down, up and move in my title bar. This was so that I could create listeners in my form to move the form when the user clicked and dragged the title bar. This is optional and depends on if you need the user to be able to move your application window.
The added benefit of doing this is that can use the title bar for additional controls. For example, my application was custom written for use on a toughbook style tablet computer with a small touchscreen display. In my application, utilization of the limited space was extremely important. I was able to further modify what I've described here to also include menu bar style control directly on the title bar. In addition, I added more buttons to the left of the stand minimize, maximize, and close buttons. Really helped me utilize every square inch of the screen in my application. Couldn't have done it with the standard title bar.
Can you simply use Form.ControlBox = false (or via the designer as you point out rather negatively in your comment) and then add a custom help button on the form?
EDIT: A colleague of mine wrote an Excel add in and had a requirement to remove the X from certain forms (e.g. a Progress Bar that shouldn't be closed). He found a function written by Stephen Bullen that did just that. I've only seen this function used in VB, but perhaps you can get some ideas or direction out of his approach of using Windows API to solve your issue.
This code will disable the Close button. I am not sure if you can actually make it invisible. http://www.codeproject.com/Articles/20379/Disabling-Close-Button-on-Forms
//
// source code
// Code Snippet
private const int CP_NOCLOSE_BUTTON = 0x200;
protected override CreateParams CreateParams
{
get
{
CreateParams myCp = base.CreateParams;
myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON ;
return myCp;
}
}
Good luck!
Please try this.ControlBox = false.
Basically I am having two problems with C#.NET MDI. You can download VS2010 solution which reproduces bugs here.
1) When programmatically hiding and showing again a maximized child form, it is not maximized properly again and becomes neither maximized or in normal state.
childForm = new Form();
childForm.Text = "Child Form";
childForm.MdiParent = this;
...
private void showButton_Click(object sender, EventArgs e)
{
childForm.Visible = true;
}
...
private void hideButton_Click(object sender, EventArgs e)
{
childForm.Visible = false;
}
When child form is maximized, then programicaly hidden and shown again, it becomes something like this (please notice the menu bar - child form's control box appears, but child form is not maximized):
At this stage, child form cannot be moved around. However, I found a workaround for that, simply by showing and hiding a dummy child form, which forces the actual child form to become properly maximized. But this makes MDI area to flicker. Tried Invalidate, Refresh, Update methods, but they don't help. Maybe there are other workarounds to overcome this bug and not to make MDI area flicker with dummy child form?
private void workaround1Button_Click(object sender, EventArgs e)
{
dummyForm.Visible = true;
dummyForm.Visible = false;
}
2) When child form is maximized, the icon of the child form is displayed on menu bar. However, if you have to change the icon while the child form is maximized, the icon on the menu bar is not being refreshed (see the image above). I found a workaround for that too, which basically hides and shows menu bar. Icon gets refreshed, but it makes everything below menu bar to flicker. Tried Invalidate, Refresh, Update methods, but they don't help. Is there any other way to make menu bar to refresh the child form's icon?
private void workaround2Button_Click(object sender, EventArgs e)
{
menuStrip.Visible = false;
menuStrip.Visible = true;
}
Also I noticed that when parent form is in normal window state mode (not maximized) and you change the width or height of the form by 1 pixel, child form becomes maximized as it should be and child form's icon on menu bar gets refreshed properly and you don't need other workaround I described above. If I change the size of the form programicaly, form flickers by 1 pixel and I cannot do that, when parent form is maximized. Is there any way how I could invoke the repaint/refresh functionality which is called when you resize a form and which makes child form become maximized properly and the icon on the menu bar refreshed?
There's a bug in the implementation of the internal MdiControlStrip class, the control that displays the icon and the min/max/restore glyphs in the parent window. I haven't characterized it as yet, the code isn't that easy. A classic side effect of the bug is that the glyphs get doubled up, you found some other side-effects. The fix is simple though, delay creating the child windows until after the constructor is completed. Like this:
public MainForm()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e) {
childForm = new Form();
childForm.Text = "Child Form";
childForm.MdiParent = this;
dummyForm = new Form();
dummyForm.MdiParent = this;
dummyForm.WindowState = FormWindowState.Maximized;
base.OnLoad(e);
}
Have you tired using Hide/Show instead of setting visible to true/false?
Try:
private void showButton_Click(object sender, EventArgs e)
{
childForm.Show();
}
private void hideButton_Click(object sender, EventArgs e)
{
childForm.Hide();
}
How about this workaround?
private void showButton_Click(object sender, EventArgs e)
{
childForm.Visible = true;
childForm.WindowState = (FormWindowState)childForm.Tag;
}
private void hideButton_Click(object sender, EventArgs e)
{
childForm.Visible = false;
childForm.Tag = childForm.WindowState;
childForm.WindowState = FormWindowState.Normal;
}
UPDATE
I just gave you the idea how you could do. A better solution using the same idea as above would be a new base form which saves the windows state. See below. Derive your forms from FixedForm instead of Form:
public partial class FixedForm : Form
{
private FormWindowState lastWindowState;
public FixedForm()
{
InitializeComponent();
}
protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged(e);
if (Visible)
{
WindowState = lastWindowState;
}
else
{
lastWindowState = WindowState;
WindowState = FormWindowState.Normal;
}
}
}
Found a way how to come around those bugs.
First of all you need to suspend painting for a form and its children. I found a very helpful thread here, which describes how to do it.
After suspending painting, you need call UpdateBounds method of the control and increase ClientRectangle Width or Height by one and then decrease it back to the same value it was before. This invokes layout functionality which makes everything to update/repaint. Last step is to enable painting. Not very nice solution I guess, but it works.
StopDrawing();
UpdateBounds(Location.X, Location.Y, Width, Height, ClientRectangle.Width, ClientRectangle.Height + 1);
UpdateBounds(Location.X, Location.Y, Width, Height, ClientRectangle.Width, ClientRectangle.Height - 1);
StartDrawing();
I find suspending painting very helpful not only for working around those two bugs, but also in general to make GUI work more smoothly. I guess this can help to remove any kind of flickering. However, this solution requires P/Invokes, which should be avoided in general.
Why not just manually reset required icon in menuStrip items, after the creation of the window:
menuStripMain.Items[0].Image = null;