C# - How To Drag And Drop Text In C#

C# - How To Drag And Drop Text In C#

                                                                                                                                                     

In This C# Tutorial We Will See How To Drag And Drop Text Between Three TextBoxes In C# Programming Language.


Source Code :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Drag_Drop_Text : Form
    {
        public Drag_Drop_Text()
        {
            InitializeComponent();
        }

        private void textBox1_MouseDown(object sender, MouseEventArgs e)
        {
            TextBox tb = (TextBox)sender;
            tb.SelectAll();
            tb.DoDragDrop(tb.Text, DragDropEffects.Copy);
        }

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

        private void textBox1_DragDrop(object sender, DragEventArgs e)
        {
            TextBox tb = (TextBox)sender;
            tb.Text = (string)e.Data.GetData(DataFormats.Text);
        }

        private void Drag_Drop_Text_Load(object sender, EventArgs e)
        {
            textBox2.DragEnter += new DragEventHandler(textBox1_DragEnter);
            textBox2.MouseDown += new MouseEventHandler(textBox1_MouseDown);
            textBox2.DragDrop += new DragEventHandler(textBox1_DragDrop);

            textBox3.DragEnter += new DragEventHandler(textBox1_DragEnter);
            textBox3.MouseDown += new MouseEventHandler(textBox1_MouseDown);
            textBox3.DragDrop += new DragEventHandler(textBox1_DragDrop);

        }
    }
}

=> OutPut :

c# drag and drop text
c# drag and drop text




Share this

Related Posts

Previous
Next Post »