使用openFiledialog打开文件时,计时器无法启动

时间:2017-09-12 12:16:31

标签: c# timer openfiledialog

我想使用openfiledialog()打开excel文件并在datagridview中显示它 我想在openfiledialog中按下打开时启动计时器 但是当文件完全加载到datagridview中时,计时器启动;

1 个答案:

答案 0 :(得分:1)

编辑:

@PaulF是对的,不需要 WndProc尝试这个

if(openFileDialog1.ShowDialog() == DialogResult.OK) {
    if (!string.IsNullOrEmpty(openFileDialog1.FileName)) {
         timer1.Start();
    }
}

您可以按OpenFileDialog抓取WndProc窗口,这是一个示例

bool IsOpenFileDialog = false; // global variable to check if openfile opened

private void button1_Click_1(object sender, EventArgs e) {
     IsOpenFileDialog = true; // button opens openfile dialog on click event
     openFileDialog1.ShowDialog(); // show it, it waits until dialog closed
     IsOpenFileDialog = false; // after closed set it to false
}

private void timer1_Tick(object sender, EventArgs e) {
     textBox1.Text += "Ticking! \n"; // added a textbox to show the ticking 
}


protected override void WndProc(ref Message m) {
      base.WndProc(ref m);
      if (m.Msg == 289) // check message loop
      {
         if (!timer1.Enabled && IsOpenFileDialog) {
      // if timer is not running already and button clicked to open openfiledialog
            timer1.Start(); // start timer
         }
      }
}

enter image description here 希望有所帮助,