Articles in this section
Category / Section

How to make the WinForms HTMLUIControl to load html documents by dragging and dropping them on the control?

2 mins read

Drag and drop

The HTMLUI control is made to support various events related to the drag drop operation.

The Allowdrop property of the HTMLUI control should be made to true, this helps in supporting the drag drop operation on the control.

The DataFormats and the DragDropEffects should be decided to determine the action of the DragDrop event. These formats and effects can be got or set with the help of the DragEvent arguments.

During DragDrop, the file name of the document along with the location is collected and when the mouse button is released, the specified document is loaded from the mentioned location through the LoadHTML method of the HTMLUI control.

C#

//To support drag events to the control
this.htmluiControl1.AllowDrop = true;
//DragDrop and DragEnter events declaration
this.htmluiControl1.DragDrop += new System.Windows.Forms.DragEventHandler(this.htmluiControl1_DragDrop);
this.htmluiControl1.DragEnter += new System.Windows.Forms.DragEventHandler(this.htmluiControl1_DragEnter);
private void htmluiControl1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
    if(e.Data.GetDataPresent (DataFormats.FileDrop))
       //Specifying the drop effect
       e.Effect = DragDropEffects.All;
    else
       e.Effect = DragDropEffects.None;
}
private void htmluiControl1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
    //Specifying the drop format and collecting the file name
    string[] files = (string[])e.Data.GetData("FileDrop", false);
    foreach (string fileName in files)
    {
        //Loading the specified file in to the HTMLUI control
        this.htmluiControl1.LoadHTML(fileName);
    }
}

 

VB

'To support drag events to the control
Private Me.htmluiControl1.AllowDrop = True
'DragDrop and DragEnter events declaration
Private Me.htmluiControl1.DragDrop += New System.Windows.Forms.DragEventHandler(Me.htmluiControl1_DragDrop)
Private Me.htmluiControl1.DragEnter += New System.Windows.Forms.DragEventHandler(Me.htmluiControl1_DragEnter)
Private Sub htmluiControl1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
    If e.Data.GetDataPresent (DataFormats.FileDrop) Then
       'Specifying the drop effect
       e.Effect = DragDropEffects.All
    Else
       e.Effect = DragDropEffects.None
    End If
End Sub
Private Sub htmluiControl1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
     'Specifying the drop format and collecting the file name
     Dim files As String() = CType(e.Data.GetData("FileDrop", False), String())
     For Each fileName As String In files
         'Loading the specified file in to the HTMLUI control
         Me.htmluiControl1.LoadHTML(fileName)
     Next fileName
End Sub

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied