Hi,
I have been working on my custom control derived from the TextBox and I have encountered a problem I can not solve right now. Brief description of the problem: my textbox contains plain text which contains tags which I want to keep consistent - so far I have overriden text selection so they can be selected only as a whole tag etc.
Right now I have moved to processing of drag&drop. If any text is dropped on the textfield and it is dropped on the tag, I want the insertion to be moved before or after the tag. The actual problem is with setting of the e.Handled=true. If I set it to true, it almost works - the text is inserted via my routine, but it is not removed from the source. If I set it to false, after executing my method the original textbox's insertion method is run. Is there any way to alter event routing? Or am I approaching this wrong from the start?
Code of my method:
protected override void OnPreviewDragEnter(DragEventArgs e)
{
base.OnPreviewDragEnter(e);
e.Handled = true;
// let us draw our very own caret...
}
protected override void OnPreviewDrop(DragEventArgs e)
{
base.OnPreviewDrop(e);
fieldsReady = false;
int selStart = this.SelectionStart;
int selLength = this.SelectionLength;
string droppedData = (string)e.Data.GetData(DataFormats.StringFormat);
// where to insert
Point whereDropped = e.GetPosition(this);
int droppedIndex = GetCharacterIndexFromPoint(whereDropped, true);
if (droppedIndex == this.Text.Length - 1)
{
double c = GetRectFromCharacterIndex(droppedIndex).X;
if (whereDropped.X > c)
droppedIndex++;
}
// only if the source was us, do this:
if (this.SelectionLength > 0) // this means that we are dragging from our textbox!
{
// was there any selection? if so, remove it!
this.Text = this.Text.Substring(0, selStart) + this.Text.Substring(selStart + selLength);
e.Handled = true;
// 2DO!! alter the indices depending on the removed selection
// insertion
this.Text = this.Text.Substring(0, droppedIndex) + droppedData + this.Text.Substring(droppedIndex);
}
}