DragLeave fires when action is cancel, how to validate? - c#

Does anyone knows how can I validate if the event is raised because the DragAction changed to cancel, or because it actually object was dragged out from the control?
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dragleave.aspx
[edit]
In this part I start the drag in the object being dragged.
void control_MouseDown(object sender, MouseEventArgs e)
{
var wasClicked = WasClicked;
if (wasClicked != null)
{
WasClicked(this, EventArgs.Empty);
}
// Select this UC on click.
if (IsSelected == false)
IsSelected = true;
else
IsSelected = false;
if (IsSelected == true)
{
_beingDragged = true;
DoDragDrop(this, DragDropEffects.Move);
}
return;
}
This part the event validates when the object being dragged leaves the FlowLayoutPanel.
private void LocationControl_DragLeave(object sender, EventArgs e)
{
foreach (Control c in Controls)
{
if (((PersonDragDrop)c).beingDragged == true)
{
((PersonDragDrop)c).LeaveLocation();
DeletePerson((PersonDragDrop)c);
}
}
}

Related

Context menu shows changes on next click

I am using a DevExpress TreeList. I have two methods MouseDown() and MouseUP() to get the item from the treelist by right click and then show a contextmenu/popup menu with changes to it at runtime.
Problem: ContextMenu or PopupMenu displays the barSubItem3.Enabled = false;change on the next click. Not on the current click.
private void TreeList1_MouseDown(object sender, MouseEventArgs e)
{
TreeList tree = sender as TreeList;
if (e.Button == MouseButtons.Right && ModifierKeys == Keys.None
&& tree.State == TreeListState.Regular)
{
Point pt = tree.PointToClient(MousePosition);
TreeListHitInfo info = tree.CalcHitInfo(pt);
if (info.HitInfoType == HitInfoType.Cell)
{
SavedFocused = new TreeListNode();
SavedFocused = tree.FocusedNode;
tree.FocusedNode = info.Node;
/* get value from node that is clicked by column index */
switch (SavedFocused.GetValue(0).ToString())
{
case "A":
barSubItem3.Enabled = false;
break;
case "B":
barSubItem3.Enabled = true;
break;
}
}
}
}
private void TreeList1_MouseUp1(object sender, MouseEventArgs e)
{
TreeList tree = sender as TreeList;
if (e.Button == MouseButtons.Right && ModifierKeys == Keys.None
&& tree.State == TreeListState.Regular)
{
popUpMenu.ShowPopup(MousePosition);
}
}
I guess that's happening because you're actually modifying the state of the item once it's already being shown.
Use the PopupMenuShowing event instead. Here is an example on how to modify the PopUpMenu using a GridView.
private void Whatever_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
{
var menu = e.Menu;
var hi = e.HitInfo;
if (!(sender is GridView view))
return;
var inDetails = (hi.HitTest == GridHitTest.EmptyRow);
if (menu == null && inDetails)
{
menu = new DevExpress.XtraGrid.Menu.GridViewMenu(view);
e.Menu = menu;
}
if (menu == null)
return;
//If there are any entries, show "Duplicate" button
var rowHandle = hi.RowHandle;
if (!view.IsDataRow(rowHandle)) return;
var mnuDuplicate = new DXMenuItem("Duplicate",
async delegate { await ClickDuplicate(); },
Properties.Resources.copy_16x16)
{
BeginGroup = true
};
menu.Items.Add(mnuDuplicate);
}

How to regain focus in application after drag and drop to grid

In my application, I have a form with two panels. Inside one panel is a button. Inside the other is a DevExpress Grid control. The grid is made up of 3 columns. You can drag values from one column into the other to copy it.
My problem is that whenever I do a drag-and-drop from one column to another, the focus on the application goes into an unusual state. The grid remains focused; I can mouse over the headers and see them react as normal. However the rest of the application is not focused. Mouse over the button in the other panel does not react, nor do the menus or form controls. If I click on the button, it reacts like I clicked on an unfocused application. I have to click again to actually activate the button. Same for every control except the grid.
I have tried using Activate() and Focus() on the button and form but to no avail.
namespace Company.StuffUploader
{
public partial class ComputationGrid : DevExpress.XtraEditors.XtraUserControl
{
private BindingList<ComputationLinkModel> _links = new BindingList<ComputationLinkModel>();
public List<ComputationLinkModel> ComputationLinkModels
{
get
{
return new List<ComputationLinkModel>(_links);
}
}
public ComputationGrid()
{
InitializeComponent();
}
private void ComputationGrid_Load(object sender, EventArgs e)
{
_gridControl.DataSource = _links;
}
private DragDropEffects GetDragEffect(DragEventArgs e)
{
var text = e.Data.GetData("System.String") as string;
if (text == null)
return DragDropEffects.None;
var link = GetLinkFromScreenPoint(new Point(e.X, e.Y));
if (link == null)
return DragDropEffects.None;
var tokens = text.Split('\t');
if (tokens.Count() != 2)
return DragDropEffects.None;
var dateString = link.movedate.ToString("yyyy-MM-dd");
if (link.StuffSurfaceName == tokens[0] && dateString != tokens[1])
return DragDropEffects.Move;
else
return DragDropEffects.None;
}
private ComputationLinkModel GetLinkFromScreenPoint(Point screenPt)
{
var pt = _gridControl.PointToClient(screenPt);
var hitInfo = _gridView.CalcHitInfo(pt);
return _gridView.GetRow(hitInfo.RowHandle) as ComputationLinkModel;
}
private void _gridControl_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
var hitInfo = _gridView.CalcHitInfo(e.Location);
if (hitInfo == null || !hitInfo.InRowCell)
return;
// Only allow dragging from target column
if (hitInfo.Column.AbsoluteIndex != 0)
return;
var link = _gridView.GetRow(hitInfo.RowHandle) as ComputationLinkModel;
if (link == null)
return;
var item = string.Format("{0}\t{1}", link.StuffSurfaceName, link.movedate.ToString("yyyy-MM-dd"));
DoDragDrop(item, DragDropEffects.Move);
}
}
private void _gridControl_DragOver(object sender, DragEventArgs e)
{
e.Effect = GetDragEffect(e);
}
private void _gridControl_DragDrop(object sender, DragEventArgs e)
{
}
private void _gridControl_DragEnter(object sender, DragEventArgs e)
{
e.Effect = GetDragEffect(e);
}
private void _unlinkButton_Click(object sender, EventArgs e)
{
}
}
}
I figured out my own problem. Calling DoDragDrop() from within MouseDown event does not seem to work correctly. The proper way is to call it from MouseMove(). The documentation on MSDN hints at this in its example code.
Ensure that you set the DXMouseEventArgs.Handled property to true in the GridView's Mouse~ event handlers. It guarantees that default handling of these events will be prohibited. Review this example to see how to do this.

Why are Stylus and Mouse events fired when touching my object?

I have a PushPin object I have hooked up to a handful of Touch / Stylus / Mouse events:
pp.MouseDown += pp_MouseDown;
pp.TouchDown += pp_TouchDown;
pp.TouchUp += pp_TouchUp;
pp.StylusDown += pp_StylusDown;
pp.StylusUp += pp_StylusUp;
Handlers
void pp_MouseDown(object sender, MouseButtonEventArgs e)
{
PushPinUpOrDown(sender);
e.Handled = true;
}
private void pp_TouchDown(object sender, TouchEventArgs e)
{
var pushpin = (sender as Pushpin);
pushpin.CaptureTouch(e.TouchDevice);
e.Handled = true;
}
void pp_StylusDown(object sender, StylusDownEventArgs e)
{
var pushpin = (sender as Pushpin);
pushpin.CaptureStylus();
e.Handled = true;
}
void pp_StylusUp(object sender, StylusEventArgs e)
{
var pushpin = (sender as Pushpin);
e.Handled = true;
if (pushpin != null && e.StylusDevice.Captured == pushpin)
{
PushPinUpOrDown(sender);
pushpin.ReleaseStylusCapture();
}
}
void pp_TouchUp(object sender, TouchEventArgs e)
{
var pushpin = (sender as Pushpin);
e.Handled = true;
if (pushpin != null && e.TouchDevice.Captured == pushpin)
{
PushPinUpOrDown(sender);
pushpin.ReleaseTouchCapture(e.TouchDevice);
}
}
but when I touch my PushPin firstly the StylusDown event fires then followed by the MouseDown. The TouchDown event I would expect to fire never fires.
Why is this? is this a problem with my program or my monitor?
Do I need both Stylus and Touch events?
(I am using a touch enabled monitor not tablet or anything)
So from here I got the code:
void pp_StylusDown(Object sender, StylusEventArgs e)
{
if (sender != null)
{
//Capture the touch device (i.e. finger on the screen)
e.StylusDevice.Capture(sender as Pushpin);
}
}
void pp_StylusUp(Object sender, StylusEventArgs e)
{
var device = e.StylusDevice;
if (sender != null && device.Captured == sender as Pushpin)
{
(sender as Pushpin).ReleaseStylusCapture();
PushPinUpOrDown(sender, true);
e.Handled = true;
}
}
which stopped the MouseDown event firing. The main reason (strangely) was removing e.Handled = true from pp_StylusDown fixed the problem
Still doesn't explain why the stylus event fires when touching the screen

Change visibility buttons in RepositoryItemButtonEdit by click

I have gridView with 3 columns. One column has repositoryItempictureEdit with 4 EditorButtons
this.repActionsBtn.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
new DevExpress.XtraEditors.Controls.EditorButton(),
new DevExpress.XtraEditors.Controls.EditorButton(),
new DevExpress.XtraEditors.Controls.EditorButton(),
new DevExpress.XtraEditors.Controls.EditorButton()});
And i have a buttonClick event handler
private void repActionsBtn_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
{
ButtonEdit editor = sender as ButtonEdit;
if (editor != null)
{
object obj = this.mainView.GetFocusedRow();
int id = GetValueFromAnonymousType<int>(obj, "ID");
//undo
if (e.Button == editor.Properties.Buttons[0])
{
_ignoredIds.Remove(id);
}
//delete
else if (e.Button == editor.Properties.Buttons[1])
{
//HERE i want change visibility buttons
e.Button.Visibility = false;
_ignoredIds.Add(id);
}
//edit
else if (e.Button == editor.Properties.Buttons[2])
{
_storedIds.Clear();
_storedIds.Add(id);
this.DialogResult = System.Windows.Forms.DialogResult.Retry;
}
//save
else if (e.Button == editor.Properties.Buttons[3])
{
//save
_storedIds.Remove(id);
}
mainView.RefreshRow(this.mainView.FocusedRowHandle);
}
}
But fires redraw and i get default repositoryItemButtonEdit with buttons is visible.
How i can change visibility(or property Enabled) of EditorButtons by user actions. (For each row)?
Devexpress support give me a solution. Here you can find the solution and download test project.

Detect if VisibleChanged is true or false

I have a handler for a C# panels VisibleChanged event. But how do I detect if the visibility is being set to true or false??
public void Parent_VisibleChanged(object sender, System.EventArgs e)
{
if(Visible = true)
{
// do what i want to do
}
}
You should use == and not =
if(Visible == true)
You should do something like this inside the event:
if (((Panel)sender).Visible)
MessageBox.Show("Visible.");
else
MessageBox.Show("Not Visible.");
Here are two ways:
private void panel1_VisibleChanged(object sender, EventArgs e)
{
// use sending object
Panel panel = sender as Panel;
if (panel.Visible == false)
;
// alternate use name of object
if (panel1.Visible == false)
;
}

Categories