I have basically two controls involved in a drag/drop operation. I do this for the start control:
private void controlA_MouseMove(object sender, MouseEventsArgs e)
{
if(e.LeftButton == MouseButtonState.Pressed)
{
//set DataObject...datao
DragDrop.DoDragDrop(controlB, datao, DragDropEffects.Copy | DragDropEffects.Copy;
}
}
The user moves off of controlA, onto controlB and continues dragging to some point on controlB. I've tried the following in several events with no luck to establish a different cursor from the default arrow with the little box under it:
Mouse.OverrideCursor = Cursors.Hand;
and
Mouse.SetCursor(Cursors.Hand);
In these events for controlB, which is where the drop happens:
DragOver
DragEnter
GiveFeedback
How do I get rid of the default arrow with the little box under it while dragging over controlB?
Set ControlB.Cursor = Cursors.Whatever; inside your ControB_MouseEnter() event handler.
You may want to limit it under an if(e.LeftButton == MouseButtonState.Pressed) condition.
Tested MouseEnter while MouseLeftButton is Pressed:
Related
I have a couple of event handlers attached to a label: one is MouseEnter and the other is MouseLeave. The MouseEnter works fine and changes the mouse cursor to an IBeam as the mouse enters the label boundary, however, the mouse cursor doesn't back to an arrow and remains as an IBeam as the mouse exists the label boundary. I can't seem to figure out what is wrong.
void lbRefLevel_MouseLeave(object sender, MouseEventArgs e)
{
Label lbRefLevel = (Label)sender;
Mouse.OverrideCursor = Cursors.Arrow;
Mouse.Capture(lbRefLevel);
}
void lbRefLevel_MouseEnter(object sender, MouseEventArgs e)
{
Label lbRefLevel = (Label)sender;
Mouse.OverrideCursor = Cursors.IBeam;
Mouse.Capture(lbRefLevel);
}
Set Mouse.OverrideCursor = null; in your mouse leave event, this will reset the override you done on your mouse enter.
Overriding again is not going to help.
As the title suggests i am having trouble getting a DragOver event to function correctly. I have over 100 buttons on a form and i want their colour to change when a picturebox is dragged over them. I have set all buttons AllowDrop = true and have included the code below in the method.
private void ShipOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.None;
Button b = (Button)sender;
b.BackColor = Color.Green;
label22.Text = "";
}
I do not see why this will not work. I also have a DragLeave method which simply changes the colour to a different one.
One thing to note is that the item i am dragging over the button is larger than the button itself. Not sure whether this will have an effect.
You need to wire up the events to your method. If all of the buttons are in a single panel, you can do something like this in your form's constructor:
foreach (Button b in panel1.Controls.OfType<Button>()) {
b.DragOver += ShipOver;
}
Same principle applies to the DragLeave event.
I have a WPF datagrid where a user can drag and drop various rows to reorder them. However, I need an indicator that will display where the row will be inserted.
I've tried various 'hacky' ways of doing it, including detecting mouse move events and inserting a blank indicator row where the mouse is. Unfortunately, it seems that while dragging rows, none of my mouse move events are getting fired.
The UIElement class provides several events that you can use.
Those are:
DragEnter
DragOver
DragLeave
Drop
The first two are the one you can use.
<DataGrid DragOver="MyGrid_DragOver"
DragEnter="MyGrid_DragEnter"
I had to use both to get my drag and drop working.
I start the Drag operation by reacting to a move mouve event:
private void MyGrid_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
var dragSource = ...;
var data = ...;
DragDrop.DoDragDrop(dragSource, data, DragDropEffects.Move);
}
}
Then you code the event handlers on the target control (your DataGrid):
private void MyGrid_DragOver(object sender, DragEventArgs e)
{
this.DragOver_DragEnter(sender, e);
}
private void MyGrid_DragEnter(object sender, DragEventArgs e)
{
this.DragOver_DragEnter(sender, e);
}
Then you can set the Effects propery to give a visual feedback to the user:
private void DragOver_DragEnter(object sender, DragEventArgs e)
{
// code here to decide whether drag target is ok
e.Effects = DragDropEffects.None;
e.Effects = DragDropEffects.Move;
e.Effects = DragDropEffects.Copy;
e.Handled = true;
return;
}
Check out this response on MSDN too: Giving drag and drop feedback using DragEventArgs.Effect
I'm looking to find out whether the user is currently holding down the vertical scroll bar or not.
This question spawns from the fact that scrolling is cancelled when a DataGridView's DataSource is updated.
What I'm hoping for is to make an extension method like IsUserScrolling() to put on the DataGridView. The idea is that I don't update the DataGridView until the user stops scrolling.
You can know if user scroll the DataGridView via Scroll event, you can know if user holding mouse down on the Thumb and scroll via its ScrollEventArgs like this:
private void dataGridView1_Scroll(object sender, ScrollEventArgs e){
if(e.ScrollOrientation == ScrollOrientation.VerticalScroll &&
(e.Type == ScrollEventType.LargeIncrement || e.Type == ScrollEventType.LargeDecrement)){
//your code here
}
}
The code above almost works well, however somehow you can change the VerticalScroll.Value (this doesn't exist) with Large Change programmatically, the event will be fired even when user doesn't hold mouse down on vertical thumb. So we can add condition MouseButtons == MouseButtons.Left to make it work better:
private void dataGridView1_Scroll(object sender, ScrollEventArgs e){
if(e.ScrollOrientation == ScrollOrientation.VerticalScroll && MouseButtons == MouseButtons.Left &&
(e.Type == ScrollEventType.LargeIncrement || e.Type == ScrollEventType.LargeDecrement)){
//your code here
}
}
Another short way to detect if user holding mouse down everywhere on the vertical scrollbar (both Thumb and Arrow Repeat button) using HitTest method, you can add more code to make it work more reliably so that we don't miss some kind of programmatical scroll with real user scrolling action:
private void dataGridView1_Scroll(object sender, ScrollEventArgs e){
Point p = dataGridView1.PointToClient(MousePosition);
if (dataGridView1.HitTest(p.X, p.Y).Type == DataGridViewHitTestType.VerticalScrollBar){
//Your code here
}
}
i've searched it and found an answer. it might not be the perfect answer, but it works:
i've created a dataGridView, and Created an hScrollBar, put the hScrollBar on top of the dataGridView scroll bar (you can use vScrollBar if you meant vertical), set the width of the scroll bar to be the same as the dataGridView, and on the Scroll event, i did:
private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
dataGridView1.HorizontalScrollingOffset = hScrollBar1.Value;
}
and this way you can use the MouseDown and MouseUp events of the hScrollBar. you well come
I have a mousemove event that takes the position of the cursor and outputs it to two labels (X and Y), the value dynamically changes as I hover around. I have a mousedown event that when clicked, the same values are outputted to a textbox. How can I combine the mousedown and mousemove events so that when I hover AND hold down the mouse button, the textbox value dynamically changes as I move.
You can interrogate the mouse buttons in your Move event handler, i.e. :
void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) {
String tipText = String.Format("({0}, {1})", e.X, e.Y);
trackTip.Show(tipText, this, e.Location);
}
}
Track the mouse down and mouse up events to set a variable determining whether or not the mouse button is pressed (ie set in down unset in mouse up) then just check this variable in mouse_move
see http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousebuttons.aspx
for an example
Use
private void OnMouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
}
}
like this and in second if you will have a condition when your mosue moved and mouse Left button is down.