【问题标题】:Show message with timer显示带有计时器的消息
【发布时间】:2017-03-15 03:11:19
【问题描述】:

我有一个包含 2 个表单的应用程序,

从 .csv 文件读取以插入 SQL Server 的 1 个表单,

出现问题时会显示 1 个表单(无法读取 .csv 文件或读取错误)

我的代码如下:

public void Timer_tick ( object sender eventargs e)
{
   PushData();  // with time interval =100    
}

public void PushData()
{
   Form2 fr = new Form2();

   // function to reading .csv file
   if ( checkData(name) == "OK") 
   {
     //update data to SQL Server    
   }
   else 
   {
      timer1.Stop();
      fr.Show();  // User need to click OK button to hide Form2 and come back Form1
      this.Refresh();
   }
}

Form2 只显示一次。

但在用户单击 Form2 上的按钮后,返回 Form1。但是 Form1 不能正常使用计时器。

请帮我解决这个应用程序。

谢谢!

【问题讨论】:

  • 看不懂,是不是Form2关闭后不重启定时器的问题?
  • 是的,我想在 Form2 关闭后重新启动计时器。

标签: c#


【解决方案1】:

将表单显示为对话框,完成后重新启动计时器:

timer1.Stop();

fr.ShowDialog();  // User need to click OK button to hide Form2 and come back Form1

timer1.Start();

【讨论】:

    【解决方案2】:

    您可以使用消息框而不是使用 form2 并显示错误消息。请参阅下面的类似代码和示例代码。

    {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                PushData();
            }
    
            public void PushData()
            {
                Form2 fr = new Form2();
                // function to reading .csv file
                if (checkBox1.Checked==true)
                {
                    progressBar1.Value += 1;
                    //update data to SQL Server
    
                }
    
                else
                {
    
                    timer1.Stop();
    
                    MessageBox.Show("Error message");
    
                    timer1.Start();
    
                }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                timer1.Start();
            }
        }
    

    【讨论】:

    • 我的应用程序需要使用其他表单。因为需要显示其他表单,用户输入密码并单击 OK 按钮后 Form2 关闭。并使用 Form1 重新启动计时器
    【解决方案3】:

    试试下面的代码,让我知道它是否有效

    public void Timer_tick ( object sender eventargs e)
    {
        PushData();  // with time interval =100    
    }
    
    public void PushData()
    {
       Form2 fr = new Form2();
       frm.Closed += fr_Closed;
       // function to reading .csv file
       if ( checkData(name) == "OK") 
       {
         //update data to SQL Server    
       }
       else 
       {
          timer1.Stop();
          fr.Show();  // User need to click OK button to hide Form2 and come back Form1
          this.Refresh();
       }
    }
    
    void fr_Closed(object sender, EventArgs e)
    {
         timer1.Start();
    }
    

    【讨论】:

      猜你喜欢
      • 2011-05-15
      • 2020-03-18
      • 1970-01-01
      • 2013-04-17
      • 2020-07-31
      • 2018-09-22
      • 1970-01-01
      • 1970-01-01
      • 2011-08-01
      相关资源
      最近更新 更多