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
//...
}
}
}
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 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
}
}
Forgive the rather poor code here.
I have a picture box PictureBox and a section Panel. I am trying to start a drag and drop operation so you click and drag the picture box, and dropping it into the Section creates a copy. This is fine.
But the problem is, lets say you click the picture box 9 times, no drag, just clicks. Then on the 10th turn, you do a correct drag and drop operation.... Then you get 10 duplicates added to the Section.
I am guessing that I need to respond to when a DragAndDrop is invalid and stop the drag and drop operation to prevent these duplicate buildups, but I do not know where to look to achieve this.
private void collectablePictureBox_MouseDown(object sender, MouseEventArgs e)
{
this.SelectedSection.AllowDrop = true;
this.SelectedSection.DragEnter += new DragEventHandler(this.CollectableSelectedSection_DragEnter);
this.SelectedSection.DragDrop += new DragEventHandler(this.CollectableSelectedSection_DragDrop);
this.collectablePictureBox.DoDragDrop(this.SelectedClassModel, DragDropEffects.Copy);
}
private void CollectablePictureBox_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
{
Console.WriteLine(e.Action);
}
private void CollectableSelectedSection_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void CollectableSelectedSection_DragDrop(object sender, DragEventArgs e)
{
Point position = this.SelectedSection.PointToClient(new Point(e.X, e.Y));
position.X -= this.SelectedClassModel.Width >> 1;
position.Y -= this.SelectedClassModel.Height >> 1;
this.SelectedSection.AllowDrop = false;
this.SelectedSection.DragEnter -= this.CollectableSelectedSection_DragEnter;
this.SelectedSection.DragDrop -= this.CollectableSelectedSection_DragDrop;
this.SelectedSection.AddItem(this.SelectedClassModel, position, this.SectionCanvasSnapToGridCheckBox.Checked);
}
Your problem comes from the fact that on every MouseDown event you add handlers to the appropriate events, so when you actually do the Drag and Drop those handlers will be called more than once. Currently I see two different ways of solving this:
One way to solve the problem would be to not start the Drag and Drop on the MouseDown event handler, but - based on this MSDN article - start it in a MouseMove handler instead, like this:
private void collectablePictureBox_MouseMove(object sender, MouseEventArgs e)
{
if (sender != null && e.LeftButton == MouseButtonState.Pressed)
{
this.SelectedSection.AllowDrop = true;
this.SelectedSection.DragEnter += new DragEventHandler(this.CollectableSelectedSection_DragEnter);
this.SelectedSection.DragDrop += new DragEventHandler(this.CollectableSelectedSection_DragDrop);
this.collectablePictureBox.DoDragDrop(this.SelectedClassModel, DragDropEffects.Copy);
}
}
Another way would be to also handle the MouseUp event of the PictureBox, and do a similar cleanup as in your CollectableSelectedSection_DragDrop handler.
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.
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!");
}
}