Use of Drag and Drop to Load Images in csXImage

Image files can be loaded in csXImage by dragging the files from Windows Explorer or My Computer and dropping them onto the control. This functionality is available by default, so there is no need to write any code to enable it to happen. The file will be loaded without any prompting to ask the user for confirmation.

It is possible to modify this behaviour so that some checking takes place before the file is loaded. For example, the loading of files could be limited to those with a specific format, or from a specific location. The most common way to modify the behaviour is to add a user dialogue so that the user is asked to confirm that the file should be loaded.

This is achieved by adding code to the OnStartDragDrop event procedure, which is called when a file is dropped on the control, but before the image is loaded. At this point, the DragDropCancel method can be called to prevent the file being loaded, and the DragDropFileName property can be read to find the name of the file being dropped.

The following examples show how to code this procedure in VB.NET, VB5/6 or C#.

Example 1 - VB.NET

Private Sub AxImageBox1_OnStartDragDrop(ByVal sender As Object, ByVal e As System.EventArgs) Handles AxImageBox1.OnStartDragDrop

If MsgBox("Do you want to load file " & AxImageBox1.DragDropFileName & "?", _
 vbYesNo, "File Load Confirmation") = vbNo Then
  AxImageBox1.DragDropCancel()
End If

End Sub

Example 2 - VB5/6

Private Sub ImageBox1_OnStartDragDrop()

If MsgBox("Do you want to load file " & ImageBox1.DragDropFileName & "?", _
 vbYesNo, "File Load Confirmation") = vbNo Then
  ImageBox1.DragDropCancel
End If

End Sub

Example 3 - C#

private void axImageBox1_OnStartDragDrop(object sender, EventArgs e)
{
  if (MessageBox.Show("Do you want to load file " + axImageBox1.DragDropFileName +
      "?", "File Load Confirmation", MessageBoxButtons.OKCancel) ==
      DialogResult.Cancel)
    axImageBox1.DragDropCancel();
}