【问题标题】:Pass values from one method to use them in another method in C#从一种方法传递值以在 C# 中的另一种方法中使用它们
【发布时间】:2015-04-18 16:10:46
【问题描述】:

如何在groupBox1_Paint 中使用timer_lights_status_Tick 方法中的值?

public Main()
{
    InitializeComponent();
}

private void groupBox1_Paint(object sender, PaintEventArgs e)
{
    // Here is where I want to use those variables: red_light1, yellow_light1…
}

public void timer_lights_status_Tick(object sender, EventArgs e)
{
    int red_light1, yellow_light1, green_light1,
        red_light2, yellow_light2, green_light2;

    red_light1    = Convert.ToInt32(comport.message(4, 8, 32));
    yellow_light1 = Convert.ToInt32(comport.message(4, 8, 33));
    green_light1  = Convert.ToInt32(comport.message(4, 8, 34));

    red_light2    = Convert.ToInt32(comport.message(4, 8, 35));
    yellow_light2 = Convert.ToInt32(comport.message(4, 8, 36));
    green_light2  = Convert.ToInt32(comport.message(4, 8, 37));
}

【问题讨论】:

  • 将“int red_light1”...移到函数之外。您还需要在设置计时器滴答中的所有灯光后调用 this.Invalidate() 以触发重绘。
  • 我移动了那条线。你说调用“this.Invalidate()”就像重置一样工作?
  • 它不是可以这么说的重置,而是通知控件它需要重绘其客户区,这反过来又会调用您的绘制方法。

标签: c# methods parameter-passing


【解决方案1】:
// Declare variables outside function scope. 
// This way, any function can access and modify these same variables.
int red_light1,yellow_light1,green_light1,
red_light2,yellow_light2,green_light2; 

public Main()
{
    InitializeComponent();
}

private void groupBox1_Paint(object sender, PaintEventArgs e)
{

  // Do something with variables: red_light1,yellow_light1... that you declared before
  // ex.:
  // red_light1 = 10;
  // CallAnotherFunction(red_light1);

}


public void timer_lights_status_Tick(object sender, EventArgs e)
{
    red_light1    = Convert.ToInt32(comport.message(4, 8, 32));
    yellow_light1 = Convert.ToInt32(comport.message(4, 8, 33));
    green_light1  = Convert.ToInt32(comport.message(4, 8, 34));

    red_light2    = Convert.ToInt32(comport.message(4, 8, 35));
    yellow_light2 = Convert.ToInt32(comport.message(4, 8, 36));
    green_light2  = Convert.ToInt32(comport.message(4, 8, 37));
}

【讨论】:

  • 请解释您做了什么以及为什么这样做。对于没有编程经验的人(正在学习编码),只有源代码是不够的。另一种方法是在代码中编写注释,例如// global variables, used to exchange the values between methods。一个简短的解释什么是全局变量会很棒。
【解决方案2】:

在 Timer.Tick 事件之外声明变量以便你可以访问

当值改变时刷新定时器事件本身的组合框

private void groupBox1_Paint(object sender, PaintEventArgs e)
{

  //Now those variables can be accessed here.

}

int red_light1,yellow_light1,green_light1,
       red_light2,yellow_light2,green_light2;

public void timer_lights_status_Tick(object sender, EventArgs e)
{
    red_light1    = Convert.ToInt32(comport.message(4, 8, 32));
    yellow_light1 = Convert.ToInt32(comport.message(4, 8, 33));
    green_light1  = Convert.ToInt32(comport.message(4, 8, 34));

    red_light2    = Convert.ToInt32(comport.message(4, 8, 35));
    yellow_light2 = Convert.ToInt32(comport.message(4, 8, 36));
    green_light2  = Convert.ToInt32(comport.message(4, 8, 37));

    groupBox1.Refresh();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    • 2019-12-30
    • 2013-01-29
    • 1970-01-01
    相关资源
    最近更新 更多