【问题标题】:after clicking on button1 should be switched on and off checkBox the time interval点击button1后应该打开和关闭checkBox的时间间隔
【发布时间】:2012-07-23 20:00:46
【问题描述】:

你能帮忙吗?在C#中,点击button1后,checkBoxWMVFile(时间间隔) 应该打开和关闭。

private void button1_Click(object sender, EventArgs e)
{

    if (timercheckbox.Enabled == true)
    {
        timercheckbox = new Timer();
        timercheckbox.Start();
        timercheckbox.Interval = 10000; // 10 second

        if(timercheckbox.Enabled)
        {
            timercheckbox.Start();
            checkBoxWMVFile.Checked = true;
        }
        else
        {
            timercheckbox.Stop();
            checkBoxWMVFile.Checked = false;
        }
    }
}

【问题讨论】:

  • What have you tired? 请提供有关您的具体问题的更多信息,而不是要求提供完整的解决方案!
  • 几乎不可能理解您的问题。请澄清您到底想要完成什么?
  • 要响应计时器超时,您需要添加超时处理程序timercheckbox.Elapsed += timercheckbox_Elapsed; 如果您澄清您的问题,我可能会给您一个正确的答案。
  • 我使用的是 medialooks videomixer SDK 应用程序,1 小时后我不会自动录制视频。所以我想checkboxfileWMV每小时打开一次,然后关闭

标签: c# checkbox


【解决方案1】:

据我了解,您需要这样的东西

private Timer _timer = new Timer();

    public Form1()
    {
        InitializeComponent();
        _timer.Interval = 10000;
        _timer.Tick += new EventHandler(_timer_Tick);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (checkBox1.Checked)
        {
            checkBox1.Checked = false;
            if(_timer.Enabled)
                _timer.Stop();
        }
        else
        {
            checkBox1.Checked = true;
            if (!_timer.Enabled)
                _timer.Start();
        }
    }

    void _timer_Tick(object sender, EventArgs e)
    {
        //do something here
        throw new NotImplementedException();
    }

【讨论】:

  • 感谢您的回答。当我将代码输入到您的 App 并按下 button1 10 秒以显示错误消息时: throw new NotImplementedException ();方法或操作未实现。
  • Martin,在将 @steavy 发布的代码粘贴到您的项目之前,您还没有阅读过代码,是吗?查看 _timer_Tick 方法。
  • martin,你必须实现 _timer_Tick 方法。我留下了例外,因为我不知道你在你的应用程序中到底在做什么。可能你需要在这个方法中重置计时器,但可能你需要一些额外的逻辑,所以这取决于你。
  • Steavy 嗨,嗨 Verdana!我不是程序员,我只是想简化我的工作...... App videomixer 从摄像机录制视频。我想每小时打破一次记录并创建一个单独的视频文件。我以为我们使用了一个系统 Thraed.sleep 但它不起作用。唯一的选择是时间间隔,但不知道......最后一次尝试......我如何实现_timer_Tick方法。只需在 Timer 中选择 Toolbox 并将其变成 _Timer?因为我试过了,什么都没有……
【解决方案2】:

为了让您的代码正常工作,您必须反转逻辑,即

private void button1_Click(object sender, EventArgs e)
{
    if(checkBoxWMVFile.Checked == false)//if the textbox is not checked
    {
        if (timercheckbox == null)//If this is the first time
        {
            timercheckbox = new Timer();//Create a timer
            timercheckbox.Interval = 10000; // Set the interval, before starting it.
            timercheckbox.Start();//Start it
        }
        else timercheckbox.Start();//If it is not the first time, just start it
        checkBoxWMVFile.Checked = true;//Check the checkbox
    }
    else//the checkbox is checked
    {
       timercheckbox.Stop();//Stop the timer
       checkBoxWMVFile.Checked = false;//Uncheck the checkbox
    }
}

【讨论】:

  • 嗨,ThunderGr。我试过你的代码。点击button1后,10秒后开启录像,但还是/录像还是/。应该还有什么?
  • 对于 Codesparkle:您好,抱歉。应用程序。 Videomixer 通过单击 checkboxfileWMV 录制视频,并且一直录制视频。当我点击button1时,我想每1小时录制一次视频。我已将视频保存 24 小时除以 1 小时
  • 该代码是 button1_click 事件的示例。您必须定义要在计时器事件中经过时间后执行的操作,如 steavy 所示。我认为可以解决您的问题的一种简单方法是从计时器事件中调用 button1_click 事件。
【解决方案3】:

这段代码可以正常工作:

// InitializeComponent        
this.timercheckbox.Interval = 5000;
this.timercheckbox.Tick += new System.EventHandler(this.timercheckbox_Tick);

private void timercheckbox_Tick(object sender, EventArgs e)
{
    checkBoxWMVFile.Checked = false;
    checkBoxWMVFile.Checked = true;
}

private void button1_Click(object sender, EventArgs e)
{
    if( timercheckbox.Enabled == true )
    {            
        timercheckbox.Enabled = false;
        button1.Text = "Start Auto save";
    }
    else
    {
        timercheckbox.Interval = (int)numericChnageTime.Value;
        timercheckbox.Enabled = true;
        checkBoxWMVFile.Checked = true;
        button1.Text = "Stop Auto save";
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-23
    • 2011-03-10
    • 2020-11-06
    • 1970-01-01
    • 2011-12-15
    • 1970-01-01
    • 2022-11-02
    • 2021-06-16
    相关资源
    最近更新 更多