Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在c语言中,如何将一个单词从一个文本框拖放到另一个特定位置#_C#_Drag And Drop - Fatal编程技术网

C# 在c语言中,如何将一个单词从一个文本框拖放到另一个特定位置#

C# 在c语言中,如何将一个单词从一个文本框拖放到另一个特定位置#,c#,drag-and-drop,C#,Drag And Drop,当涉及到从C#winforms应用程序拖放文本时,我有一个小问题 我想将内容从“TextBoxA”拖到“TextBoxB”中的特定位置 e、 g 当从TextBoxA拖动“大”并将其放在TextBoxB的“Hello World”之间时,TextBoxB会出现类似“Hello Big World”(取决于鼠标的释放位置)的结果。我希望这会对您有所帮助。我认识到这是一个非常古老的问题,但仍然值得回答 下面代码中的注释是不言自明的——如果您有任何问题,请告诉我 public Form1()

当涉及到从C#winforms应用程序拖放文本时,我有一个小问题

我想将内容从“TextBoxA”拖到“TextBoxB”中的特定位置

e、 g


当从TextBoxA拖动“大”并将其放在TextBoxB的“Hello World”之间时,TextBoxB会出现类似“Hello Big World”(取决于鼠标的释放位置)的结果。

我希望这会对您有所帮助。

我认识到这是一个非常古老的问题,但仍然值得回答

下面代码中的注释是不言自明的——如果您有任何问题,请告诉我

    public Form1()
    {
        InitializeComponent();

        // Allow TextBoxB to accept a drop.
        TextBoxB.AllowDrop = true;

        // Add event handlers to manage the drag drop action.
        TextBoxB.DragEnter += TextBoxB_DragEnter;
        TextBoxB.DragDrop += TextBoxB_DragDrop;
    }

    void TextBoxB_DragEnter(object sender, DragEventArgs e)
    {
        // Update cursor to inform user of drag drop action.
        if (e.Data.GetDataPresent(DataFormats.Text)) e.Effect = DragDropEffects.Copy;
    }

    void TextBoxB_DragDrop(object sender, DragEventArgs e)
    {
        // Get the current cursor postion within the control.
        var point =TextBoxB.PointToClient(Cursor.Position);

        // Find the character index based on cursor position.
        var pos = TextBoxB.GetCharIndexFromPosition(point);

        // Insert the required text into the control.
        TextBoxB.Text = TextBoxB.Text.Insert(pos, TextBoxA.Text);
    }

在问题的开始,你说的是拖动文件,然后是文本框文本:/Hello Reniuz,刚刚更新了措辞-抱歉,谢谢Anton,但这不是我想要的。我最终找到了鼠标的位置,计算了字符串中每个字符的宽度,并将其放置在适当的位置。
    public Form1()
    {
        InitializeComponent();

        // Allow TextBoxB to accept a drop.
        TextBoxB.AllowDrop = true;

        // Add event handlers to manage the drag drop action.
        TextBoxB.DragEnter += TextBoxB_DragEnter;
        TextBoxB.DragDrop += TextBoxB_DragDrop;
    }

    void TextBoxB_DragEnter(object sender, DragEventArgs e)
    {
        // Update cursor to inform user of drag drop action.
        if (e.Data.GetDataPresent(DataFormats.Text)) e.Effect = DragDropEffects.Copy;
    }

    void TextBoxB_DragDrop(object sender, DragEventArgs e)
    {
        // Get the current cursor postion within the control.
        var point =TextBoxB.PointToClient(Cursor.Position);

        // Find the character index based on cursor position.
        var pos = TextBoxB.GetCharIndexFromPosition(point);

        // Insert the required text into the control.
        TextBoxB.Text = TextBoxB.Text.Insert(pos, TextBoxA.Text);
    }