Timer 无法设置全局变量的值

Timer 无法设置全局变量的值,timer,global-variables,bots,Timer,Global Variables,Bots,我对编程相当陌生,但我非常渴望深入了解这方面的知识,尤其是c。我已经为自动打字机(垃圾邮件机器人,如果我可以的话)编写了一些代码,只是作为我创建的目标。基本上,我希望该程序执行以下操作: 当我启动Form1时,全局变量“\u timerValue”设置为 一千 当我点击开始按钮时,上文本框中的文本将发送到 “\u timerValue”的间隔 当我按下速度按钮时,将显示Form2 当我快速点击时,“\u timerValue”设置为5000(测试目的) 表格1代码: public parti

我对编程相当陌生,但我非常渴望深入了解这方面的知识,尤其是c。我已经为自动打字机(垃圾邮件机器人,如果我可以的话)编写了一些代码,只是作为我创建的目标。基本上,我希望该程序执行以下操作:

  • 当我启动Form1时,全局变量“\u timerValue”设置为 一千
  • 当我点击开始按钮时,上文本框中的文本将发送到 “\u timerValue”的间隔
  • 当我按下速度按钮时,将显示Form2
  • 当我快速点击时,“\u timerValue”设置为5000(测试目的)
表格1代码:

public partial class Form1 : Form
{

    static class TimerIntervalValue
    {
        Form2 f2 = new Form2();
        TimerIntervalValue = f2._timerValue;
    }

    public Form1()
    {
        InitializeComponent();
        f2._timerValue = "1000";
    }

    public void timer1_Tick(object sender, EventArgs e)
    {
        SendKeys.Send(textBox1.Text);
    }

    private void button1_Click(object sender, EventArgs e)
    {

        timer1.Enabled = true;
    }

    private void button1_MouseDown(object sender, MouseEventArgs e)
    {
        MessageBox.Show(f2._timerValue);
        timer1.Interval = Convert.ToInt32(f2._timerValue);

        if (timer1.Enabled == false)
        {
            timer1.Enabled = true;
            textBox1.Enabled = false;
            button1.Text = ("Stop");
        }
        else if (timer1.Enabled == true)
        {
            timer1.Enabled = false;
            textBox1.Enabled = true;
            button1.Text = ("Start");
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Form2 form2 = new Form2();
        form2.Show();
    }
}
表格2代码:

public partial class Form2 : Form
{
    public string TimerValue;

    public string _timerValue
    {
        get { return TimerValue; }
        set { TimerValue = value; }
    }

    public Form2()
    {
        InitializeComponent();
    }

    public void button1_Click(object sender, EventArgs e)
    {

        Form2 frm2 = new Form2();
        frm2._timerValue = "5000";
    }     
}
我最初试图在Form1中的“InitializeComponent();”下创建一个Form2实例,但这似乎无法通过其他函数访问

我只知道这很简单,比如我使用了错误的类来创建Form2实例或者类似的东西


不管怎样,提前谢谢你

只需将TimerValue和_TimerValue标记为static。那么你就不需要使用

Form2 f2 = new Form2(); or Form2 frm2 = new Form2();
再也没有了。在表格1中,只需使用表格2.\u timerValue而不是f2.\u timerValue。在表格2中,只需更改:

public void button1_Click(object sender, EventArgs e)
{
    _timerValue = "5000";
}     

_timerValue必须是静态类型,因为您希望从不同的对象访问相同的值。这是正确的吗?是的,我已经修好了。正如我所说,这么一个愚蠢的小错误,我就是看不出问题所在。谢谢你的帮助:)非常感谢,照我的意思做了:)