I am trying to add a context menu to a listbox when you right click an item.
I am not even sure if the right click function with working properly.
Here is the code:
private void lstSource_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
Console.WriteLine("Right Click");
}
}
Nothing prints to the console. Am I missing something?
Thanks.
Make sure you wire the event up (and the ListBox is enabled):
private void Form1_Load(object sender, EventArgs e)
{
listBox1.MouseDown += new MouseEventHandler(listBox1_MouseDown);
}
void listBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
MessageBox.Show("Right Click");
}
}
You can also have the designer wire up the event for you by selecting the ListBox and double-clicking on the MouseDown event in the Properties window (click on the lightning bolt).
Console.WriteLine() method wont display anything on GUI. Use MessageBox.Show("Right Click");
private void lstSource_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
MessageBox.Show("Right Click");
}
}
EDIT: Be sure that the handler is attached with MouseDown event or not.
Related
I think this is to do with casting:
First, I declare an event handler for my picturebox:
pictureBox1.MouseHover += new EventHandler(this.pictureBox1_MouseHover);
Next I'm trying to check whether the left mousebutton is held down with:
private void pictureBox1_MouseHover(object sender, EventArgs e)
{
if (e.Button == MouseButtons.Left)
{
/*some gibberish for when the left mouse button is held*/
}
}
But this doesn't work, because EventArgs is not MouseEventArgs
Can I Cast or somehow convert EventArgs e to be treated as MouseEventArgs so that the above code would work?
Can you turn an apple in an orange? No. Well, you can't just make an MouseEventArgs from an EventArgs instance.
In this case, your code doesn't make sense. You are trying to get the button of a hover event. Hovering is done without any button clicks. If you want to know the button pressed at time of hovering, you need to cache the MouseDown and MouseUp events first to register what button was clicked.
If you want to check if a mouse button is pressed while moving the mouse, then you should subscribe to PictureBox.MouseDown, PictureBox.MouseMove and PictureBox.MouseUp events if you want to record the mouse movement from the point you first pressed the button, while you're moving the mouse while still holding the button and when you release the button.
private void PictureBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
//Handle mouse down logic here (press).
}
}
private void PictureBox_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
//Handle mouse move logic here (hold).
}
}
private void PictureBox_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
//Handle mouse up logic here (release).
}
}
i need to hide a ListBox when I focus out a textbox. if i click on a different control or use Tab key then the textbox's "Leave" event occurs. But if I click inside the form, on any free space, then focusout doesn't happen. i saw something called mouse capture but i cant implement it.
i tried this:
private void txtProduct_Enter(object sender, EventArgs e)
{
listProduct.Show();
UIElement el = (UIElement)sender;
el.CaptureMouse();
}
private void MouseClickedElseWhere(object sender, MouseEventArgs e)
{
if (e.Clicks >= 1)
{
txtProduct_Leave(sender, new EventArgs());
}
}
private void txtProduct_Leave(object sender, EventArgs e)
{
listProduct.Hide();
}
but obviously it shows error. how do i achieve this? any help?
I had to make click event for my groupboxes even if groupbox doesnt have a click event by default.
//my_page.designer.cs
this.groupBox2.Click += new System.EventHandler(this.groupBox2_clicked);
//my_page.cs
private void groupBox2_clicked(object sender, EventArgs e)
{
listProduct.Hide();
}
I am working in mouse_down event.
private void treeView_MouseDown(object sender, MouseEventArgs e)
Now, In mouse_down event How to check user clicked expand/collpase icon not node?
Note : Before_Expand and Before_Check events will not work bcoz mouse_down event fire first.
private void treeView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
var Test = treeView1.HitTest(e.Location);
if (Test.Location == TreeViewHitTestLocations.PlusMinus)
{
//You can check here
}
}
After I added drag & drop to a DataGridView, the CellDoubleClick event stopped working. In the CellMouseDown event I have the following code:
private void dataGridView2_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
var obj = dataGridView2.CurrentRow.DataBoundItem;
DoDragDrop(obj, DragDropEffects.Link);
}
How do I correct this to enable CellDoubleClick event?
Yes, that cannot work. Calling DoDragDrop() turns mouse control over to the Windows D+D logic, that's going to interfere with normal mouse handling. You need to delay starting the D+D until you see the user actually dragging. This ought to solve the problem:
Point dragStart;
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) {
if (e.Button == MouseButtons.Left) dragStart = e.Location;
}
private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
var min = SystemInformation.DoubleClickSize;
if (Math.Abs(e.X - dragStart.X) >= min.Width ||
Math.Abs(e.Y - dragStart.Y) >= min.Height) {
// Call DoDragDrop
//...
}
}
}
I'm creating a System-Tray only application. It's somewhat complicated to have the icon without a main form, but through previous topics on StackOverflow I've worked it out. The right-click works fine, I've linked in a context menu, etc.
I'm having problems with the left-click. As far as I can tell, the "notifyIcon1_Click" event isn't firing at all.
private void notifyIcon1_Click(object sender, EventArgs e)
{
Debug.WriteLine("Does it work here?");
if (e.Equals(MouseButtons.Left))
{
Debug.WriteLine("It worked!");
}
}
Neither of those debug lines are outputting, breakpoints in that event don't stop the program, etc.
Am I doing this incorrectly? What should my next step be? I'm coding this in C#, using Windows 7 if that matters at all for taskbar behavior.
If you want to determine if it's a left or right click, wire up the MouseClick, rather than click.
That way you get a signature like this:
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
//Do the awesome left clickness
else if (e.Button == MouseButtons.Right)
//Do the wickedy right clickness
else
//Some other button from the enum :)
}
If you want the click event of the Message/Balloon itself use
_notifyIcon.BalloonTipClicked += notifyIconBalloon_Click;
private void notifyIconBalloon_Click(object sender, EventArgs e)
{
// your code
}
private void NotifyIcon_Click(object sender, EventArgs e)
{
MouseEventArgs mouseEventArgs = (MouseEventArgs)e;
if (mouseEventArgs.Button == MouseButtons.Right && IsHandleCreated)
{
popupMenu1.ShowPopup(MousePosition);
return;
}
if (mouseEventArgs.Button == MouseButtons.Left && IsHandleCreated)
{
if (isWindowMinimized)
showWindow();
else
hideWindow();
}
}
The other answer is not clear that you need MouseClick event instead of Click.
notifyIcon.MouseClick += MyClickHandler;
Then your handler function will work fine.
void MyClickHandler(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Console.WriteLine("Left click!");
}
}