TextBox.DoDragDrop : TextBox « System.Windows.Forms « C# / C Sharp by API






TextBox.DoDragDrop

 


using System;
using System.Windows.Forms;

public class TextBoxDragDropDemo : Form
{
    public TextBoxDragDropDemo()
    {
        InitializeComponent();
    }

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

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

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

    [STAThread]
    public static void Main(string[] args)
    {
        Application.Run(new TextBoxDragDropDemo());
    }
    private System.Windows.Forms.TextBox TextBox2;
    private System.Windows.Forms.TextBox TextBox1;

    private void InitializeComponent()
    {
        this.TextBox2 = new System.Windows.Forms.TextBox();
        this.TextBox1 = new System.Windows.Forms.TextBox();
        this.SuspendLayout();

        this.TextBox2.AllowDrop = true;
        this.TextBox2.Location = new System.Drawing.Point(28, 129);
        this.TextBox2.Multiline = true;
        this.TextBox2.Size = new System.Drawing.Size(196, 77);
        this.TextBox2.DragDrop += new System.Windows.Forms.DragEventHandler(this.TextBox_DragDrop);
        this.TextBox2.DragEnter += new System.Windows.Forms.DragEventHandler(this.TextBox_DragEnter);
        this.TextBox2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.TextBox_MouseDown);

        this.TextBox1.AllowDrop = true;
        this.TextBox1.Location = new System.Drawing.Point(28, 36);
        this.TextBox1.Multiline = true;
        this.TextBox1.Size = new System.Drawing.Size(196, 77);
        this.TextBox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.TextBox_DragDrop);
        this.TextBox1.DragEnter += new System.Windows.Forms.DragEventHandler(this.TextBox_DragEnter);
        this.TextBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.TextBox_MouseDown);

        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292, 266);
        this.Controls.Add(this.TextBox2);
        this.Controls.Add(this.TextBox1);
        this.ResumeLayout(false);
        this.PerformLayout();

    }
}

   
  








Related examples in the same category

1.TextBox.AcceptsTab
2.TextBox.Anchor
3.TextBox.BorderStyle
4.TextBox.CanFocus
5.TextBox.Clear()
6.TextBox.Click
7.TextBox.ContextMenu
8.TextBox.ContainsFocus
9.TextBox.DataBindings
10.TextBox.DragDrop
11.TextBox.DragEnter
12.TextBox.Focus()
13.TextBox.Focused
14.TextBox.GotFocus
15.TextBox.KeyPress
16.TextBox.Lines
17.TextBox.LostFocus
18.TextBox.Multiline
19.TextBox.MouseDown
20.TextBox.PasswordChar
21.TextBox.ScrollBars
22.TextBox.SelectAll()
23.TextBox.SelectedText
24.TextBox.SelectionLength
25.TextBox.TextChanged
26.TextBox.Validated
27.TextBox.Validating