Drag And Drop
-
Well im going to try to ask this question in the most understandable way... This is what im trying to do: I have a Rich Text Box on a form. When you drag a file onto the rich text box (Presumably a text file) it opens it and copies all of the text onto the rich text box. Im thinking that it will somehow have to find out the location of the file, open it and use stream reader to read it. If anyone has ANY suggestions please write a message. Thank You :) MWA HAHAHAHA I AM A NOOB AND WE NOOBS WILL KILL YOU ALL. JK Whats intresting is that im only 12 years old :P
-
Well im going to try to ask this question in the most understandable way... This is what im trying to do: I have a Rich Text Box on a form. When you drag a file onto the rich text box (Presumably a text file) it opens it and copies all of the text onto the rich text box. Im thinking that it will somehow have to find out the location of the file, open it and use stream reader to read it. If anyone has ANY suggestions please write a message. Thank You :) MWA HAHAHAHA I AM A NOOB AND WE NOOBS WILL KILL YOU ALL. JK Whats intresting is that im only 12 years old :P
You are correct. Do something like this:
public Form1()
{
InitializeComponent();
richTextBox1.DragEnter += new DragEventHandler(richTextBox1_DragEnter);
richTextBox1.DragDrop += new DragEventHandler(richTextBox1_DragDrop);
}void richTextBox1_DragDrop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); //array of the full paths of all files that were dropped
if (files.Length > 0)
{
try
{
System.IO.StreamReader r = new System.IO.StreamReader(files[0]);
richTextBox1.Text = r.ReadToEnd();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Error opening file " + files[0] + ":\n"+ex.Message);
}
}
}void richTextBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
{
e.Effect = DragDropEffects.All;
}
}Hope this helps, DigitalKing