如何在c#中为主窗体的子窗体更改计时器设置

时间:2017-03-24 12:04:14

标签: c#

我的主应用程序中有一个计时器功能,用作定时会话。但是,我想更改定时器间隔以及通过子窗体打开和关闭它的功能,但不能使用其他窗体中的定时器功能元素来更改设置。以下是我的代码段。任何提示或示例将不胜感激。

尝试:  主要表格

int n = int.Parse(Console.ReadLine());
Dictionary<string,string> phoneBook = new Dictionary<string,string>();

for (int i = 0; i < n; i++)
{
    var names = Console.ReadLine().Split(' ');
    phoneBook.Add(names[0], names[1] );
}

string name = Console.ReadLine();
do
{ 
    string printValue = "Not found";
    if (phoneBook.ContainsKey(name))
    {
        printValue = name + "=" + phoneBook[name];
    }

    Console.WriteLine(printValue);

    name = Console.ReadLine();

}while(!string.IsNullOrEmpty(name)); 

计时器控制表格

private void Booyaa_Load(object sender, EventArgs e)
        {
            BooyaaTimer.Interval = 45 * 60 * 1000); // 45 mins
            BooyaaTimer.Tick += new EventHandler(BooyaaTimer_Tick);
            BooyaaTimer.Start();
            if (!Properties.Settings.Default.SettingShutdown)
            {
                MessageBox.Show("Not properly shut down");
                GetPass pass = new GetPass();
                DialogResult result = pass.ShowDialog();
                if (result == DialogResult.OK) { 

                    Properties.Settings.Default.SettingShutdown = true;
                Properties.Settings.Default.Save();
            }
             else
            {
                Close();
            }
             }

private void BooyaaTimer_Tick(object sender, EventArgs e)
        {
            MessageBox.Show("TIME IS UP");

            GetPass pass = new GetPass();
                DialogResult result = pass.ShowDialog();
                if (result == DialogResult.OK)
                {

                Show();
                Properties.Settings.Default.SettingShutdown = true;
                Properties.Settings.Default.Save();
            }
                else
            {

                BooyaaTimer.Start();
                Properties.Settings.Default.SettingShutdown = false;
                Properties.Settings.Default.Save();
                Hide();
            }

            } 

1 个答案:

答案 0 :(得分:1)

我要猜一猜。 Timer以主窗体

创建
public partial class Form1 : Form {
    Timer BooyaaTimer = new Timer(); // Or this is created in Designer

    void SomeFunctionThatCreatesTheOtherForm() {
        TimerControlsForm form2 = new TimerControlsForm();
        // Pass the timer to form2
        form2.BooyaTimer = BooyaTimer;
        form2.ShowDialog();
    }
}

另一种形式

public partial class TimerControlsForm : Form {
    // This has to be a Timer object
    public Timer BooyaTimer {get; set;}

    private void btn_confirm_Click(object sender, EventArgs e) {
        BooyaaTimer.Interval = Int32.Parse(textBox1.Text);
    }
}