In this section we're going to examine drag drop operations.
In this example the code to support the toolbox looks like this:
private void button_StartDragDrop(object sender, MouseEventArgs e) { IElement el; string tagName = ((Label)sender).Tag.ToString(); DataObject d = new DataObject(); switch (tagName) {
// This is how you can create elements with predefined settings case "TEXTAREA": el = _editor.CreateElement("TEXTAREA"); ((TextAreaElement)el).cols = 10; ((TextAreaElement)el).rows = 3; goto case "INPUT"; case "LISTBOX": el = _editor.CreateElement("SELECT"); ((SelectElement)el).multiple = true; ((SelectElement)el).size = 3; goto case "INPUT";
case "INPUT button": el = new InputButtonElement(_editor); goto case "INPUT"; case "INPUT image": el = new InputImageElement(_editor); goto case "INPUT"; case "INPUT submit": el = new InputSubmitElement(_editor); goto case "INPUT"; case "INPUT checkbox": el = new InputCheckboxElement(_editor); goto case "INPUT"; case "INPUT radio": el = new InputRadioElement(_editor); goto case "INPUT"; case "INPUT text": el = new InputTextElement(_editor); goto case "INPUT"; case "INPUT password": el = new InputPasswordElement(_editor); goto case "INPUT"; case "INPUT hidden": el = new InputHiddenElement(_editor); goto case "INPUT";
case "INPUT": d.SetData(typeof(IElement), el); break; } ((Label)sender).DoDragDrop(d, DragDropEffects.Copy); }
The button "MouseDown" event is used to initiate this, because this is the most intuitive way to begin a drag operation. In the Html Editor the property AllowDrop is set to true. All other part of this operation is handled internally. The data format DataFormats.Html is used only in case the internal operation does not support the element sent. It will fall back to drop regular HTML. Only difference is, that the Drop event does not recognize the element as an object. If this doesn't matter, pure HTML is just fine.
Several events are supported. Especially to treat a dropped element afterwards the DragDrop event is crucial. In the example code the handler looks like this:
void ToolWindow_DragDrop(object sender, DragEventArgs e) { foreach (string format in e.Data.GetFormats()) { switch (format) { case "GuruComponents.Netrix.WebEditing.Elements.IElement": AnchorElement a = (AnchorElement)e.Data.GetData(typeof(IElement)); a.href = "http://www.netrixcomponent.net"; a.InnerText = "Netrix Component"; break; } } }
Because the element sent is of type IElement we look for the appropriate type. The former type can be restored just by a cast. Once the element object is present, you can set any properties you like.