ChooseFileExtension.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using DevExpress.XtraEditors;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8. namespace ExtensionsDev
  9. {
  10. public static class ChooseFileExtension
  11. {
  12. public static ButtonEdit UseChooseFile(this ButtonEdit ctrl)
  13. {
  14. ctrl.AllowDrop = true;
  15. ctrl.DragEnter += ctrl_DragEnter;
  16. ctrl.DragDrop += ctrl_DragDrop;
  17. ctrl.ButtonClick += Ctrl_ButtonClick;
  18. return ctrl;
  19. }
  20. private static void Ctrl_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
  21. {
  22. var ctrl = sender as Control;
  23. XtraOpenFileDialog xtraOpenFileDialog1 = new XtraOpenFileDialog();
  24. xtraOpenFileDialog1.Multiselect = false;
  25. if (xtraOpenFileDialog1.ShowDialog() == DialogResult.OK)
  26. {
  27. ctrl.Text = xtraOpenFileDialog1.FileName;
  28. }
  29. }
  30. private static void ctrl_DragEnter(object sender, DragEventArgs e)
  31. {
  32. if (e.Data.GetDataPresent(DataFormats.FileDrop))
  33. {
  34. e.Effect = DragDropEffects.Link;
  35. }
  36. else
  37. {
  38. e.Effect = DragDropEffects.None;
  39. }
  40. }
  41. private static void ctrl_DragDrop(object sender, DragEventArgs e)
  42. {
  43. var file = ((Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
  44. (sender as Control).Text = file;
  45. }
  46. }
  47. }