I want to open a WPF ContextMenu when the user clicks the system tray icon. With Windows Forms this is straight-forward, just call notifyIcon.ContextMenu = contextMenu and you're set.
On WPF we can not set the ContextMenu that easily because WPF's ContextMenu class is not related to Forms ContextMenu. The alternative I have been pursuing is to handle NotifyIcon's Click event to open the WPF-style ContextMenu.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// This is intended to be a system tray application so we hide the window
this.Visibility = Visibility.Hidden;
// Winform context menu
// The context menu closes when the user clicks anywhere outside the menu
// The user can navigate the menu with the keyboard arrows and close with ESC
var notifyIcon1 = new System.Windows.Forms.NotifyIcon();
var contextMenu = new System.Windows.Forms.ContextMenu();
var menuItem = new System.Windows.Forms.MenuItem();
menuItem.Text = "WinForm Menu Item";
contextMenu.MenuItems.Add(menuItem);
notifyIcon1.ContextMenu = contextMenu;
notifyIcon1.Icon = Properties.Resources.ico;
notifyIcon1.Visible = true;
// WPF context menu
// The user cannot close the menu by clicking outside its bounds
// Does not detect any keyboard input
var notifyIcon2 = new System.Windows.Forms.NotifyIcon();
notifyIcon2.Icon = Properties.Resources.ico;
notifyIcon2.Visible = true;
notifyIcon2.Click += NotifyIcon2_Click;
}
private void NotifyIcon2_Click(object sender, EventArgs e)
{
var contextMenu = new ContextMenu();
var menuItem = new MenuItem();
menuItem.Header = "WPF Menu Item";
contextMenu.Items.Add(menuItem);
contextMenu.IsOpen = true;
}
}
The issue with this approach is that the WPF ContextMenu never gets any hint that the user has navigated away from the menu and should close (Eg, when the user clicks outside the bounds of the menu). None of the Focus or MouseCapture events are ever triggered and I am unable to close the menu other than by clicking on one of its items.
So the question here, to put slightly different, is: how do I properly emulate the NotifyIcon's ContextMenu closing behavior using WPF's ContextMenu?
I faced similar issue. You can try if you want
notifyIcon1.ContextMenuStrip = new Forms.ContextMenuStrip();
notifyIcon1.ContextMenuStrip.Items.Add("YourMenuItem",null, MenuItemEvent);
I hope this will solve the problem.
Related
I made a simple button, but when i click outside of win form my button getting a black border. By the way i set BorderSize to "0" and it works great while i clicking inside of my form.
this.button.FlatAppearance.BorderSize = 0;
That's how it looks like.
it seems a focus problem. Try to reset the focus when the cursor leave the control.
Add these lines of code to your forms load event.
btn.FlatStyle = FlatStyle.Flat;//You can also use the popup flat style
btn.FlatAppearance.BorderColor = btn.Parent.BackColor;
btn.FlatAppearance.BorderSize = 0;
One simple workaround is to set the FlatAppearance.BorderColor of the Button to its Parent.BackColor:
this.button1.FlatAppearance.BorderColor = this.button1.Parent.BackColor;
You could set this Property subscribing to the ParentChanged event (or overriding OnParentChanged, if it's a Custom Control) if the Control can be assigned to another parent at some point.
You can also perform the same operation in batch, using the HandleCreated event and have all the Buttons (with FlatStyle = FlatStyle.Flat) subscribe to the event in the Form's constructor:
public Form1()
{
InitializeComponent();
foreach (Button button in this.Controls.OfType<Button>().Where(btn => btn.FlatStyle == FlatStyle.Flat))
{
button.HandleCreated += (s, e) => {
button.FlatAppearance.BorderColor = button.Parent.BackColor;
};
}
}
if (VendorSettingWondow == null)
{
VendorSettingWondow = new VendorSettingWindow();
VendorSettingWondow.Top = this.Top;
VendorSettingWondow.Left = this.Left + this.Width/2;
//VendorSettingWondow.Visibility = Visibility.Visible;
VendorSettingWondow.injdf.Text = filename; // setting file name
VendorSettingWondow.inJDFVendorInfo.ItemsSource = inJDFVendors;
// VendorSettingWondow.inJDFVendorInfo.ItemsSource = vendorList.Vendors;
VendorSettingWondow.DBVendorList.ItemsSource = DBvendorList.Vendors;
VendorSettingWondow.ShowDialog();
}
I am creating a subwindow if some condition is satisfied in the main window like above. There are two buttons in my sub window. When i click the button the sub window is getting closed. I am using ShowDialog() because I want to control to be transferred from main window to sub window when the subwindow is created
Check the IsCancel property of the Button. If it is set to true, set it to false to prevent the button from closing the dialog.
How do I create a simple form that has a MenuStrip at the top and a TabControl filling all of the remaining space?
If I go with DockStyle.Top/DockStyke.Fill tabControl fills whole form regardless of MenuStrip:
public MainWindow()
{
initializeComponent();
}
private void initializeComponent()
{
MenuStrip mainMenu = new MenuStrip();
mainMenu.Dock = DockStyle.Top;
TabControl tabs = new TabControl();
tabs.Dock = DockStyle.Fill;
TabPage test = new TabPage("test");
tabs.Controls.Add(test);
Controls.Add(mainMenu);
Controls.Add(tabs);
}
You should change the z-order of mainMenu or tabs. For example you can call:
mainMenu.SendToBack();
//Or
//tabs.BringToFront();
After adding controls to the controls collection.
Another approach through designer, without writing code manually, so your changes will affect design time too
Use Document outline tab and arrange control's hierarchy with your requirements
View -> Other Windows -> Document outline or CTRL+ALT+T
I have a windows form application where some controls are hidden at first and visible on some condition.
Sometimes tool tip balloon is not visible.
I have below code in constructor,
ToolTip toolTipBalloon;
toolTipBalloon.AutoPopDelay = 15000;
toolTipBalloon.InitialDelay = 1500;
toolTipBalloon.IsBalloon = true;
toolTipBalloon.ReshowDelay = 100;
toolTipBalloon.ToolTipTitle = "Setting";
toolTipBalloon.Popup += new System.Windows.Forms.PopupEventHandler(this.toolTipBalloon_Popup);
In event handler:
private void toolTipBalloon_Popup(object sender, PopupEventArgs e)
{
// Set title of tooltip to control's accessible name or text
Control ctrl = e.AssociatedControl;
if (!String.IsNullOrEmpty(ctrl.AccessibleName))
toolTipBalloon.ToolTipTitle = ctrl.AccessibleName;
else if (!String.IsNullOrEmpty(ctrl.Text))
toolTipBalloon.ToolTipTitle = ctrl.Text;
}
You'll have to assign the tooltip to the control at some point.
toolTipBalloon.SetToolTip(ctrl, "Message");
You can also add multiple controls with diffrent messages to the same tooltip.
toolTipBalloon.SetToolTip(btnStart, "Start the thingy!");
toolTipBalloon.SetToolTip(lblSpeed, "You're going thiiiis fast.");
toolTipBalloon.SetToolTip(txtName, "Enter your super hero name.");
Goal:
Right clicking in the listview and choose different option.
Problem:
There are two problem:
*When I'm right clicking, the left corner of the menu is not exactly located in the arrow's spot location.
*How do I create a line in the menu?
The main problem about the menu
Need support to create these two redmark.
private void lstV_Stock_MouseUp(object sender, MouseEventArgs e)
{
switch (e.Button)
{
// Right mouse click
case MouseButtons.Right:
ContextMenu myContextMenu = new ContextMenu();
MenuItem menuItem1 = new MenuItem("New product");
MenuItem menuItem2 = new MenuItem("Delete");
MenuItem menuItem3 = new MenuItem("Add quantity");
// Clear all previously added MenuItems.
myContextMenu.MenuItems.Clear();
myContextMenu.MenuItems.Add(menuItem1);
myContextMenu.MenuItems.Add(menuItem2);
myContextMenu.MenuItems.Add(menuItem3);
if (lstV_Stock.SelectedItems.Count > 0)
{
foreach (ListViewItem item in lstV_Stock.SelectedItems)
{
myContextMenu.MenuItems[1].Visible = true;
myContextMenu.MenuItems[2].Visible = true;
myContextMenu.MenuItems[0].Visible = false;
}
}
else
{
myContextMenu.MenuItems[1].Visible = false;
myContextMenu.MenuItems[2].Visible = false;
myContextMenu.MenuItems[0].Visible = true;
}
myContextMenu.Show(lstV_Stock, this.PointToClient(Cursor.Position), LeftRightAlignment.Right);
menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
break;
}
For the positioning, you can replace your
myContextMenu.Show(lstV_Stock, this.PointToClient(Cursor.Position), LeftRightAlignment.Right);
to
myContextMenu.Show(lstV_Stock, e.Location(), LeftRightAlignment.Right);
or the point e.X,e.Y. Not from this.PointToClient, but from the MouseEventArgs generating the event. You can check wahat MouseEvent have here.
To create a "line" you have to create a MenuItem with text "-"
Problem
If you just set the ListView.ContextMenu property and remove all your own right-click code, the menu should show up correctly.
For the line you need a ToolStripSeparator item. The designer will create one when you type '-' as the Text. You can drag them in the designer.
So, using a ContextMenu is the way to go here. Those "Lines" you're referring to are called Separaters.
If you're creating the COntext Menu in Design View, then click the Context Menu, then right-click inside the menu, and click Insert > Separater.
You can then drag it up or down, or into a sub-menu if you wish.