1

I want to add text into textbox whenever label is drag and dropped inside textbox, so far i accomplished it using below methods. Consider i have already some text in textbox, now when i drop the label it adds the text to end, i understand it's because of i am adding textbox=textbox+labelcontents.

Is there any other way, to add text to that same location where it is being dropped, and all previous text remain same. Can we use location points?

In the form default Constructor:

lblBreakStartTime.MouseDown += new MouseEventHandler(lblBreakStartTime_MouseDown);    
txtBoxDefaultEnglish.AllowDrop = true;
txtBoxDefaultEnglish.DragEnter += new DragEventHandler(txtBoxDefaultEnglish_DragEnter);
txtBoxDefaultEnglish.DragDrop += new DragEventHandler(txtBoxDefaultEnglish_DragDrop);

Mouse Down Event for label which will be dropped:

private void lblBreakStartTime_MouseDown(object sender, MouseEventArgs e)
        {
            DoDragDrop("START_TIME", DragDropEffects.Copy);
        }

Textbox events:

private void txtBoxDefaultEnglish_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Text)) e.Effect = DragDropEffects.Copy;
        }
        private void txtBoxDefaultEnglish_DragDrop(object sender, DragEventArgs e)
        {

       txtBoxDefaultEnglish.Text = txtBoxDefaultEnglish.Text + " " + "[" + (string)e.Data.GetData(DataFormats.Text) + "]";
       txtBoxDefaultEnglish.SelectionStart = txtBoxDefaultEnglish.Text.Length;
    }
3
  • just found solution, its very easy , i just have to use cursor position. int CursorPos = txtBoxDefaultEnglish.SelectionStart; txtBoxDefaultEnglish.Text=txtBoxDefaultEnglish.Text.Insert(CursorPos, "[" + (string)e.Data.GetData(DataFormats.Text) + "]"); Commented Oct 27, 2016 at 6:42
  • Well post this as answer and accept it please. Maybe helpful for others ;) Commented Oct 27, 2016 at 6:43
  • @Nisar, you'll be able to insert your text at cursor location but not at dropped location Commented Oct 27, 2016 at 6:46

1 Answer 1

1

Try this:

private void txtBoxDefaultEnglish_DragDrop(object sender, DragEventArgs e)
{
    //Get index from dropped location
    int selectionIndex = txtBoxDefaultEnglish.GetCharIndexFromPosition(txtBoxDefaultEnglish.PointToClient(new Point(e.X, e.Y)));
    string textToInsert = string.Format(" [{0}]", (string)e.Data.GetData(DataFormats.Text));
    txtBoxDefaultEnglish.Text = txtBoxDefaultEnglish.Text.Insert(selectionIndex, textToInsert);
    txtBoxDefaultEnglish.SelectionStart = txtBoxDefaultEnglish.Text.Length;

    //Set cursor start position
    txtBoxDefaultEnglish.SelectionStart = selectionIndex;
    //Set selction length to zero
    txtBoxDefaultEnglish.SelectionLength = 0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Its a good solution, one small thing, if we drop label between text, it will move cursor to end of text Box. How can we keep cursor at location after insertion and not move it to end of complete line. i hope you got my point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.