Drag And Drop in WPF


“This demo will demonstrate how to drag and drop a txt file in the WPF textbox and it show the content of that files”.

 Download: http://www.4shared.com/file/GWB4a3bQ/drg.html

1.     “select the textbox and change it “Allow Drop” property true.”

2.     “write the following code in the textbox “Preview Drag Enter” event. ”

Code:

private void textBox1_PreviewDragEnter_1(object sender, DragEventArgs e)
        {
            bool isCrt = true;</pre>
            if (e.Data.GetDataPresent(DataFormats.FileDrop, true) == true)
            {

                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop, true);
                foreach (string file in files)
                {
                    if (File.Exists(file) == false)
                    {
                        isCrt = false;
                        break;
                    }
                    FileInfo info = new FileInfo(file);
                    if (info.Extension != ".txt")
                    {
                        isCrt = false;
                        break;
                    }

                }
            }
            if (isCrt == true)
            {
                e.Effects = DragDropEffects.All;

            }
            else
                e.Effects = DragDropEffects.None;
            e.Handled = true;
        }

3. “write the following code in the textbox preview drop event function.”

Code:

private void textBox1_PreviewDrop(object sender, DragEventArgs e)
        {

            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop, true);
            foreach (string file in files)
                textBox1.Text += File.ReadAllText(file);
            e.Handled = true;
          }
Advertisements

One thought on “Drag And Drop in WPF

  1. hi sir
    i have solved my problem of database connection.i give table name but there should be database name. thanks for help. pls do one more favore.
    i want to apply password when a form load. pls send me its code as soon as possible because it is expected in my tomorrow test. thanks again

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s