1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.richTextBox1.AllowDrop = true;
this.richTextBox1.DragEnter += new System.Windows.Forms.DragEventHandler(this.richTextBox1_DragEnter);
this.richTextBox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.richTextBox1_DragDrop);
}
private void richTextBox1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
if (((DragEventArgs)e).Data.GetDataPresent(DataFormats.FileDrop))
((DragEventArgs)e).Effect = DragDropEffects.Copy;
else
((DragEventArgs)e).Effect = DragDropEffects.None;
}
private void richTextBox1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
// récupère le nom des fichiers
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
// teste le nombre de fichiers passés
if (files.Length > 1)
{
MessageBox.Show("1 seul fichier à la fois.", "trop de fichiers", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (files.Length == 1)
{
try
{
FileInfo info = new FileInfo(files[0]);
if (info.Extension == ".txt")
{
richTextBox1.LoadFile(files[0], RichTextBoxStreamType.PlainText);
}
else if (info.Extension == ".rtf")
{
richTextBox1.LoadFile(files[0], RichTextBoxStreamType.RichText);
}
else
{
MessageBox.Show("Seuls les fichiers .txt et .rtf sont acceptés", "erreur de format de fichier", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch
{
MessageBox.Show("Error lors de l'ouverture du fichier", "Erreurr!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}
} |
Partager