I have a tray application and I would like to open the ContextMenuStrip if I click on the tray logo. This is my code:
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
timeBroMenu.Show(Cursor.Position);
}
}
Double click is working easily.
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
timeBroMenu.Show(Cursor.Position);
}
Is there any way to make the left click work?
You don't need to handle the Click event if you have assigned the NotifyIcon's ContextMenuStrip Property.
Related
I have the same ContextMenu assigned to a Form and to a NotifyIcon.
this.ContextMenu = this.contextMenu;
this.notifyIcon.ContextMenu = this.ContextMenu;
In the Popup event of the context menu I'm trying to find out who is displaying the context menu: the form (such as a right click on the form), or the notify icon (a right click on the notification icon):
private void ContextMenu_Popup(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine(this.contextMenu.SourceControl.Name);
}
However, I always get the form as the source control, even if I right click on the notification icon.
I'm using C#, .NET Framework 4.6 and Windows Forms.
Maybe, a quickest way is to have a flag:
private bool fromIcon;
private void notifyIcon1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
fromIcon = true;
}
}
private void ContextMenu_Popup(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine(fromIcon.ToString());
fromIcon = false;
}
I need to run code when I dismiss(end) dragging click. How this event is called?
Here is main example, please find the below screenshot for more information:
I made that I can drag the car on other picture boxes like below this:
Repeat again - I need to know what EVENT is then you dismiss to drag on picture box?
There is no event for when the drag is released on a control, but you don't really need one. This is what I did to simulate what (I think) you're looking for. I used code courtesy of this stackoverflow answer
private Point? _mouseLocation;
private void Form1_Load(object sender, EventArgs e)
{
this.pictureBox1.MouseDown += this.pictureBox1_MouseDown;
this.pictureBox1.MouseUp += this.pictureBox1_MouseUp;
this.pictureBox1.MouseMove += this.pictureBox1_MouseMove;
}
void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if ( this._mouseLocation.HasValue)
{
this.pictureBox1.Left = e.X + this.pictureBox1.Left - this._mouseLocation.Value.X;
this.pictureBox1.Top = e.Y + this.pictureBox1.Top - this._mouseLocation.Value.Y;
}
}
void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
this._mouseLocation = null;
}
void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
//Check if you've left-clicked if you want
this._mouseLocation = e.Location;
}
Setting the mouse location to null with this._mouseLocation = null; is your "drag released" code.
I guess you talking about DragDrop. You can find example here: How to: Enable Drag-and-Drop Operations with the Windows Forms RichTextBox Control
I would please like to know how to display a context menu when you right click on the window.
Here is my code so far :
private void ShowContextMenu_RightClick(object sender, EventArgs e)
{
toolStripMenuItem5.Visible = true;
}
private void toolStripMenuItem5_Click(object sender, EventArgs e)
{
MessageBox.Show("Hi there this is my 3rd app which is *animation*.", "Programmed by D & K");
}
AFAIK there is no direct RightClick event in winforms. You can use the mousedown event to achieve this
private void toolStripButton1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
MessageBox.Show("Hi there this is my 3rd app which is *animation*.", "Programed by D & K");
}
}
in form cs file you can attach your context menu like this..
public Form1()
{
InitializeComponent();
//Create right click menu..
ContextMenuStrip s = new ContextMenuStrip();
// add one right click menu item named as hello
ToolStripMenuItem hello = new ToolStripMenuItem();
hello.Text = "Hello";
// add the clickevent of hello item
hello.Click += hello_Click;
// add the item in right click menu
s.Items.Add(hello);
// attach the right click menu with form
this.ContextMenuStrip = s;
}
void hello_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello Clicked");
}
You should use MouseDown. Then you can get the clicked button with e.Button and the coordinates with e.X and e.Y.
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
MessageBox.Show(e.Button.ToString() + " " + e.X + " " + e.Y);
}
Add a ContextMenuStrip control to the form (note, it does not display on the form but is instead shown at the bottom of the designer).In the form's ContextMenuStrip property, choose the name of the ContextMenuStrip control you just added to the form.That's it. HansPassant stated this in a comment to the question but I think it is being overlooked.
The ContextMenuStrip property is a property of many UI controls that you would use the same technique for.
I have a button on my WinForms app that I want to be invisible until the user moves his mouse over the button. Then they could click it. If the mouse leaves the button, it needs to be hidden again. The button.Visible parameter makes the button completely inaccessible and disables the mouse over. Any ideas or other button parameters I could use?
This currently does not work:
private void settingButton_MouseEnter(object sender, EventArgs e)
{
settingButton.Visible = true;
}
private void settingButton_MouseLeave(object sender, EventArgs e)
{
settingButton.Visible = false;
}
This issue was brought up and answered here:
C# WinForms MouseHover and MouseLeave problem
private void Form_MouseMove(object sender, MouseEventArgs e) {
if(settingButton.Bounds.Contains(e.Location) && !settingButton.Visible) {
settingButton.Show();
}
}
i have this function who catch right click
private void listBoxFiles_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
contextMenuStripListBox.Show((Control)sender, e.X, e.Y);
}
}
There's a sample in the docs for MouseEventArgs:
if (e.Button == MouseButtons.Right)
{
...do your stuff
}
You mean you want to add an entry to a listbox?
Use:
ListBox.Items.Add("My content here");
If you want to change the context menu for the right click action, have a look at this guide:
http://msdn.microsoft.com/en-us/library/aa984319%28v=vs.71%29.aspx