# Drag Drop

## Content

[C1Editor](/componentone/api/win/online-editor-net/dotnet-api/C1.Win.Editor.10/C1.Win.Editor.C1Editor.html) provides events for drag-and-drop interactions, mouse activity, and document change notifications.
These events allow applications to monitor editor interaction, process dropped content, detect document updates, and synchronize editor state with application logic.
Typical scenarios include processing dropped files or text, tracking drag-and-drop state, responding to mouse interaction, tracking unsaved changes, and triggering validation workflows.
![C1WinForms Editor - DragDrop demonostration](https://cdn.mescius.io/document-site-files/images/c8989ae3-804c-4f29-b5ce-fe6b13c0a9e3/editordragdrop-20261008.229217.gif?width=400&verticalAlign=middle)

***

## Drag-and-Drop Events

`C1Editor` supports standard drag-and-drop events for monitoring drag operations and processing dropped content.

| Event | EventArgs Type | Typical Usage |
| ----- | -------------- | ------------- |
| `DragEnter` | `DragEventArgs` | Validate dragged content, initialize drag state |
| `DragOver` | `DragEventArgs` | Update drag feedback, control drop behavior |
| `DragLeave` | `EventArgs` | Clear drag indicators, reset temporary state |
| `DragDrop` | `DragEventArgs` | Import dropped files or text, update document content |

Applications commonly use these events to validate dropped data, update drag feedback, and import external content into the editor.

### **DragEnter**

`DragEnter` occurs when dragged content enters the editor surface.

```csharp
c1Editor1.DragEnter += (s, e) =>
{
    AddLog("DragEnter");
};
```

***

**DragOver**
`DragOver` occurs while dragged content moves over the editor surface.

```csharp
c1Editor1.DragOver += (s, e) =>
{
    AddLog("DragOver");
};
```

***

### **DragLeave**

`DragLeave` occurs when dragged content leaves the editor surface.

```csharp
c1Editor1.DragLeave += (s, e) =>
{
    AddLog("DragLeave");
};
```

***

### **DragDrop**

`DragDrop` occurs when content is dropped into the editor.

```csharp
c1Editor1.DragDrop += (s, e) =>
{
    AddLog("DragDrop");
};
```

***

## Mouse Events

`C1Editor` provides mouse interaction events for responding to user input within the editor surface.

| Event | EventArgs Type | Typical Usage |
| ----- | -------------- | ------------- |
| `MouseDown` | `MouseEventArgs` | Start interaction workflows, handle selection |
| `MouseUp` | `MouseEventArgs` | Complete interactions, display context menus |

Applications commonly use these events for custom interaction workflows, selection handling, and context-menu integration.

***

### **MouseDown**

`MouseDown` occurs when a mouse button is pressed inside the editor.

```csharp
c1Editor1.MouseDown += (s, e) =>
{
    AddLog("MouseDown");
};
```

***

### **MouseUp**

`MouseUp` occurs when a mouse button is released inside the editor.

```csharp
c1Editor1.MouseUp += (s, e) =>
{
    AddLog("MouseUp");
};
```

***

## DocumentChanged Event

`DocumentChanged` occurs when the editor content changes.

| Event | EventArgs Type | Typical Usage |
| ----- | -------------- | ------------- |
| `DocumentChanged` | `EventArgs` | Track unsaved changes, trigger validation |

```csharp
c1Editor1.DocumentChanged += (s, e) =>
{
    AddLog("DocumentChanged");
};
```

The event is raised for operations including typing, deleting, pasting, formatting changes, and programmatic content updates.
`DocumentChanged` reports that the document state changed but does not identify the specific modified content or operation. Formatting-only updates can also trigger the event.
Applications commonly use this event to:

* Track unsaved changes
* Enable save operations
* Trigger validation workflows
* Synchronize editor state with application logic

**Event Processing Behavior**
Event handlers execute on the UI thread.
Long-running operations inside handlers can block editor interaction and UI rendering. High-frequency events such as `DocumentChanged` should avoid expensive synchronous processing.
The documented events are notification events and do not provide built-in cancellation support.

***

## **Sample Code**

The following example demonstrates how to register and handle `C1Editor` interaction events, including drag-and-drop, mouse, document change, and editor initialization events.
The sample also logs event activity in a `RichTextBox`.

```csharp
public partial class Events : UserControl
{
    public Events()
    {
        InitializeComponent();
        c1Editor1.C1EditorReady += c1Editor1_C1EditorReadyAsync;
        c1Editor1.ClientSizeChanged += C1Editor1_Resize;
        c1Editor1.SizeChanged += C1Editor1_Resize;

        c1Editor1.DragDrop += C1Editor1_DragDrop;
        c1Editor1.DragEnter += C1Editor1_DragEnter;
        c1Editor1.DragOver += C1Editor1_DragOver;
        c1Editor1.DragLeave += C1Editor1_DragLeave;

        c1Editor1.MouseUp += C1Editor1_MouseUp;
        c1Editor1.MouseDown += C1Editor1_MouseDown;

        c1Editor1.DocumentChanged += C1Editor1_DocumentChanged;
    }
  
    private void C1Editor1_Resize(object sender, EventArgs e)
    {
        c1Editor1.Focus();
    }

    private async void c1Editor1_C1EditorReadyAsync(object sender, EventArgs e)
    {
        string filename = @"Resources\tesla.html";
        if (File.Exists(filename))
        {
            await c1Editor1.LoadDocumentAsync(Path.GetFullPath(filename));
            await c1Editor1.SetDefaultStylesAsync(null, true);
        }
    }

    private void C1Editor1_DragEnter(object? sender, DragEventArgs e)
    {
        AddLog("DragEnter");
    }

    private void C1Editor1_DragDrop(object? sender, DragEventArgs e)
    {
        AddLog("DragDrop");
    }

    private void C1Editor1_DragLeave(object? sender, EventArgs e)
    {
        AddLog("DragLeave");
    }

    private void C1Editor1_DragOver(object? sender, DragEventArgs e)
    {
        AddLog("DragOver");
    }
  
    private void C1Editor1_MouseDown(object? sender, MouseEventArgs e)
    {
        AddLog("MouseDown");
    }
  
    private void C1Editor1_MouseUp(object? sender, MouseEventArgs e)
    {
        AddLog("MouseUp");
    }
  
    private void C1Editor1_DocumentChanged(object? sender, EventArgs e)
    {
        AddLog("DocumentChanged");
    }

    private int count = 0;
    private string lastMessage = "";
    private int times = 0;
    private void AddLog(string message)
    {
        if (message == lastMessage)
        {
            times++;
            ReplaceLastLine(count.ToString("D3") + " - " + message + ". Times : " + times.ToString());
        }
        else
        {
            lastMessage = message;
            times = 1;
            count++;
            AppendLine(count.ToString("D3") + " - " + message);
        }
    }

    private void AppendLine(string text)
    {
        if (richTextBoxEvents.TextLength > 0)
            richTextBoxEvents.AppendText("\n");

        richTextBoxEvents.AppendText(text);
        richTextBoxEvents.SelectionStart = richTextBoxEvents.TextLength;
        richTextBoxEvents.ScrollToCaret();
    }

    private void ReplaceLastLine(string text)
    {
        int start = richTextBoxEvents.Text.LastIndexOf('\n');
        start = start < 0 ? 0 : start + 1;

        richTextBoxEvents.Select(start, richTextBoxEvents.TextLength - start);
        richTextBoxEvents.SelectedText = text;

        richTextBoxEvents.SelectionStart = richTextBoxEvents.TextLength;
    }
}
```