SimulationEditor.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using DevExpress.XtraEditors;
  2. using System;
  3. using System.IO;
  4. using System.Windows.Forms;
  5. namespace XdDw.App.EditForms
  6. {
  7. public partial class SimulationEditor : DevExpress.XtraEditors.XtraForm
  8. {
  9. public string SimulationFile;
  10. public SimulationEditor()
  11. {
  12. InitializeComponent();
  13. this.Text = "仿真文件选择";
  14. this.StartPosition = FormStartPosition.CenterParent;
  15. }
  16. public SimulationEditor(string fileName)
  17. : this()
  18. {
  19. btnFile.Text = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
  20. }
  21. private void btnCancel_Click(object sender, EventArgs e)
  22. {
  23. this.DialogResult = DialogResult.Cancel;
  24. }
  25. private void btnOk_Click(object sender, EventArgs e)
  26. {
  27. var file = btnFile.Text;
  28. if (string.IsNullOrEmpty(file))
  29. {
  30. XtraMessageBox.Show("仿真文件不能为空!");
  31. return;
  32. }
  33. if (!File.Exists(file))
  34. {
  35. XtraMessageBox.Show($"仿真文件{file}不存在!");
  36. return;
  37. }
  38. SimulationFile = file;
  39. this.DialogResult = DialogResult.OK;
  40. }
  41. private void btnFile_Click(object sender, EventArgs e)
  42. {
  43. OpenFileDialog openFileDialog = new OpenFileDialog();
  44. openFileDialog.InitialDirectory = "c:\\";
  45. openFileDialog.Filter = "数据文件|*.dat";
  46. openFileDialog.RestoreDirectory = false;
  47. openFileDialog.FilterIndex = 1;
  48. if (openFileDialog.ShowDialog() == DialogResult.OK)
  49. {
  50. btnFile.Text = openFileDialog.FileName;
  51. }
  52. }
  53. private void btnFile_DragDrop(object sender, DragEventArgs e)
  54. {
  55. var files = ((System.Array)e.Data.GetData(DataFormats.FileDrop));
  56. if (files.Length > 0)
  57. {
  58. btnFile.Text = files.GetValue(0).ToString();
  59. }
  60. }
  61. private void btnFile_DragEnter(object sender, DragEventArgs e)
  62. {
  63. if (e.Data.GetDataPresent(DataFormats.FileDrop))
  64. {
  65. var files = ((System.Array)e.Data.GetData(DataFormats.FileDrop));
  66. if (files.Length == 0)
  67. {
  68. e.Effect = DragDropEffects.None;
  69. return;
  70. }
  71. string file = files.GetValue(0).ToString();
  72. var extension = Path.GetExtension(file);
  73. if (File.Exists(file) && extension.ToUpper() == ".dat".ToUpper())
  74. {
  75. e.Effect = DragDropEffects.Link;
  76. }
  77. }
  78. else
  79. {
  80. e.Effect = DragDropEffects.None;
  81. }
  82. }
  83. }
  84. }