I am having a windows form menu strip control. And having ToolStripMenu Item with text of "Click Me". Now i want to display its sub menu toolstrip items on Mouse Hover Event of the "Click Me" ToolStrip. Can any one suggest how its can be done.
Here on mousehover event i want to display its sub menu item like this
You can handle MouseHover event of the items and then using ShowDropDown method, open the dropdown. This way, menus will open on hover rather than click.
For example:
private void Form1_Load(object sender, EventArgs e)
{
this.menuStrip1.Items.OfType<ToolStripMenuItem>().ToList().ForEach(x =>
{
x.MouseHover += (obj, arg) => ((ToolStripDropDownItem)obj).ShowDropDown();
});
}
System.Windows.Forms.ToolStripMenuItem clickmeeToolStripMenuItem
this.clickmeeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem()
this.clickmeeToolStripMenuItem.MouseHover += new System.EventHandler(this.clickmeeToolStripMenuItem_MouseHover);
//ADD THIS METHOD TO YOUR EVENT METHOD
clickmeeToolStripMenuItem.ShowDropDown();
Related
I have a WinForms app that starts up in the tray only. On clicking it, it opens a form. This works fine.
notifyIcon.Click += notifyIcon_Click;
//Fires on icon click, AND on contextmenuitem click
private void notifyIcon_Click(object sender, EventArgs e)
{
new ActiveIssues(_hubProxy).Show();
}
I have added a context menu, but when I click the ContextMenuItem, it first fires the NotifyIcon click event, THEN the ContextMenuItem click event, opening both forms.
notifyIcon.ContextMenu = GetCrestContextMenu();
private ContextMenu GetCrestContextMenu()
{
var contextMenu = new ContextMenu();
contextMenu.Name = "CResT Alerts";
contextMenu.MenuItems.Add(GetTextOptionMenuItem());
return contextMenu;
}
private MenuItem GetTextOptionMenuItem()
{
var textOptionMenuItem = new MenuItem { Text = _textOptedIn ? "Opt Out of Text Alerts" : "Opt In to Text Alerts" };
textOptionMenuItem.Click += TextOptionMenuItem_Click;
return textOptionMenuItem;
}
//Fires on menuitem click, after the NotifyIcon click event is called
private void TextOptionMenuItem_Click(object sender, EventArgs e)
{
if (_textOptedIn) new TextOptOut().Show();
else new TextOptIn().Show();
}
Any idea how to either NOT have it fire the notifiyicon click event or to tell that the click was on the context menu?
So it turns out that the right-button click is not registered until after the context menu is clicked, so it is the right-button click that registered and raised the NotifyIcon click event. As such, I had to cast the EventArgs provided for the click as MouseEventArgs, and check the button.
private void notifyIcon_Click(object sender, EventArgs e)
{
if(((MouseEventArgs)e).Button == MouseButtons.Left) new ActiveIssues(_hubProxy).Show();
}
I have Windows Form TestForm, and in my Form I have several labels that are only used to display some text.
I need to display a MessageBox.Show anytime the Form is clicked. So I have an event handler for the click, which looks like this:
private void TestForm_Click(object sender, EventArgs e)
{
MessageBox.Show("The form has been clicked");
}
Unfortunately, the click event doesn't fire when I click over a label in the Form. Is there a way to fix this, besides consuming the click event for the labels?
Thanks.
To use the same click event for all labels:
In the properties for each label, go to the Events (lightning bolt tab).
You will see (probably near the top) a label for Click, click the dropdown for this event, and you will be shown a list of handlers that you could use for that label.
Here's the Properties > Events > Click handler (bottom right):
Because all of your labels are of the same type, and produce the same EventArgs, you are able to use the same handler for all of them.
Then, when you are adding more Labels, just choose the event handler from the Click event dropdown:
Hope this helps!
To flesh out LarsTech's comment, I have used something like this in the past when I was having problems with labels overlapping each other and lack of true transparency in WinForms. What I did was make the labels invisible on the Form, then iterate through them in the Form's paint event, pull the information out of them and then use Graphics.DrawString to draw the text. That way you you will still be able see them in design mode.
This is a quick example of what I mean.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
foreach (var temp in this.Controls)
{
if (temp is Label) //Verify that control is a label
{
Label lbl =(Label)temp;
e.Graphics.DrawString(lbl.Text, lbl.Font, new SolidBrush(lbl.ForeColor), new Rectangle(lbl.Location, lbl.Size));
}
}
}
private void Form1_Click(object sender, EventArgs e)
{
MessageBox.Show("The Form has been clicked");
}
}
I have encountered some problems in the bookmark section for my wpf web-browser. I want to be able to delete already existing buttons but I can't seem to figure out how to detect the content of the button I right click on. (To get my ContextMenu to show).
My visual progress so far: http://puu.sh/6Dxat.png
Adding a context menu to the buttons:
public void button_MouseRightButtonDown(object sender, MouseButtonEventArgs e)//add a context menu to buttons
{
Button button = sender as Button;
menu = new ContextMenu();
menu.Items.Add(new MenuItem() { Header = "Delete" });
button.ContextMenu = menu;
menu.Closed += button_DeleteButtonClicked;//find the right event
}
(I know that the event is wrong, but that is not important right now.)
And the event:
private void button_DeleteButtonClicked(object sender, RoutedEventArgs e)//delete bookmark
{
//This is where I need help. I want the content (which is the URL) of the button
//right clicked onto, for example, show up in a messagebox. How to do?
}
Since you have hooked Close event of context menu here, so sender will be ContextMenu here and you can get button by using PlacementTarget property of ContextMenu.
private void button_DeleteButtonClicked(object sender, RoutedEventArgs e)
{
Button button = ((ContextMenu)sender).PlacementTarget as Button;
}
I have a PictureBox.
If I right click the PictureBox, my ContextMenuStrip (right click menu) appears.
In that ContextMenuStrip is a ToolStripMenuItem (one of the options in the right click menu).
There is an event on the ToolStripMenuItem that handles what happens if that option is clicked.
We're starting from ToolStripMenuItem's "Clicked" function. I need to somehow get back to the PictureBox programmatically. From there, I can modify the PictureBox.
ToolStripMenuItem -> ContextMenuStrip -> PictureBox
How would I go about this?
If the click event handler for your menu item is named OnToolStripMenuItemClick, the following might be an approach to your problem:
private void OnToolStripMenuItemClick(object sender, EventArgs e)
{
var menuItem = sender as ToolStripMenuItem;
var contextMenu = menuItem.Parent as ContextMenuStrip;
var pictureBox = contextMenu.SourceControl;
}
Of course, don't forget to check for null when accessing properties after conversion with as.
I'm not sure that I really understood your problem, but I guess you want to let users can return to the picturebox when they want to cancel current operation by clicking the right button. In this case, you should not implement your work in click event, because right and left button both can trigger the click event, instead, you should process your work in the event "MouseUp", like this:
private void menuItemBack_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
MessageBox.Show("back item is clicked");
}
else
{
MessageBox.Show("I will come back.");
//do your return things here.
}
}
I've just come across this same problem in C#, and the path to the value seems to be something like:
sender.Owner.SourceControl;
However, since sender, Owner and so on are generic classes, I had to cast everything as follows:
PictureBox pb = (PictureBox) ((ContextMenuStrip)((ToolStripMenuItem)sender).Owner).SourceControl;
Ugly, but it works.
i have a WPF tab control with two tabs(A, B). Since there is no Clicked Event on the TabItem,therefore i add an previewMouseDown event on the Tab B and a messageBox will show up. However, after i close the messageBox, my application will not navigate to the Tab B. Anyone can help me?
C# code snippet :
private void MyTabB_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("Please login");
}
You can use the TabControlSelectionChanged event.
Since you know the selected tab for login say index 0, change selected tab after the MessageBox ie something like this
MyTabB_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
TabControl tc= ((TabControl)sender;
if(tc.SelectedIndex == tc.Items.IndexOf(A/*Login tab*/))
{
MessageBox.Show("Login")
tc.SelectedIndex = tc.Items.IndexOf(B);
}
}
This means whenever you select A you will MessageBox will pop up then you login.
Alternatively why not put a button or any control with ClickedEvent in A then when clicked tc.SelectedIndex is changed.