I am working on a large application and adding some drag / drop functions to it. In particular, I allow the user to drag the file into the main window to open the file.
The problem is that the drag / drop operation is still allowed when the main window displays a dialog box (for example, the properties window for an item in the file that is currently opening). I would prefer not to allow this if a modal dialog box is displayed in the main window. This is because when loading a new file in the application when opening a dialog box, the program may crash: the code that invokes the dialog box does not expect the open file to be modified when the dialog box opens (therefore, the dialog box was modal ...).
The main application is written in C ++, but I am posting a C # sample. The symptom / behavior is the same on both platforms, but I can demonstrate this with much less C # code. I am very familiar with both languages / platforms, so I can translate any answers into the corresponding language as needed.
To demonstrate the problem with my sample code, compile and run the following C # code. It will create a “main window” that is a valid target. Drag the file from Windows Explorer to the main window: you will see a window with discarded messages. Now click on the button on the form to open the dialog box. Again, try dragging the file to the main window while the dialog box is open. Note that crash is allowed even if the modal dialog box is open. How can I prevent this when the dialog is open?
The obvious answer is to temporarily set AllowDrop to false when the dialog box opens. The problem is that the main application is very large and therefore there are many places that open dialog boxes. It will be difficult to find every place that opens a dialog and adding this code. In addition, each developer must know to complete this action each time they open a modal window; it is unlikely that everyone will remember. I am worried that this is not a good solution.
Of course, there is a more convenient way of doing this, which does not require adding code to every place where the dialog opens?
using System;
using System.Windows.Forms;
using System.Drawing;
public class MyDialog : Form {
public MyDialog() {
Text = "MyDialog";
}
}
public class MainForm : Form {
public MainForm() {
Button btn = new Button();
btn.Location = new Point(0, 0);
btn.Text = "ShowDialog";
btn.Size = new Size(75, 23);
btn.Click += new EventHandler(GoToDialog);
this.AllowDrop = true;
this.Controls.Add(btn);
this.Text = "Drop Target";
this.DragDrop += new DragEventHandler(this.MyDragDrop);
this.DragEnter += new DragEventHandler(this.MyDragEnter);
}
private void MyDragDrop(object sender, DragEventArgs e) {
MessageBox.Show("dropped");
}
private void MyDragEnter(object sender, DragEventArgs e) {
e.Effect = DragDropEffects.Copy;
}
private void GoToDialog(object sender, EventArgs e) {
using (MyDialog ab = new MyDialog()) {
ab.ShowDialog(this);
}
}
}
static class Program {
[STAThread]
static void Main() {
Application.Run(new MainForm());
}
}