ChooseFileExtension.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 void 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. }
  19. private static void Ctrl_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
  20. {
  21. var ctrl = sender as Control;
  22. XtraOpenFileDialog xtraOpenFileDialog1 = new XtraOpenFileDialog();
  23. xtraOpenFileDialog1.Multiselect = false;
  24. if (xtraOpenFileDialog1.ShowDialog() == DialogResult.OK)
  25. {
  26. ctrl.Text = xtraOpenFileDialog1.FileName;
  27. }
  28. }
  29. private static void ctrl_DragEnter(object sender, DragEventArgs e)
  30. {
  31. if (e.Data.GetDataPresent(DataFormats.FileDrop))
  32. {
  33. e.Effect = DragDropEffects.Link;
  34. }
  35. else
  36. {
  37. e.Effect = DragDropEffects.None;
  38. }
  39. }
  40. private static void ctrl_DragDrop(object sender, DragEventArgs e)
  41. {
  42. var file = ((Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
  43. (sender as Control).Text = file;
  44. }
  45. }
  46. }