0

How can I use values from the timer_lights_status_Tick method in groupBox1_Paint?

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));
}
3
  • 2
    Move the "int red_light1"... outside of the function. You will also need to call this.Invalidate() after setting all the lights in the timer tick to trigger a redraw. Commented Apr 18, 2015 at 16:11
  • I moved that line. You say to call "this.Invalidate()" to work like a reset? Commented Apr 18, 2015 at 16:15
  • Its not a reset so-to-speak, but it informs the control that it needs to redraw its client area, which in turn calls your paint method. Commented Apr 18, 2015 at 16:19

2 Answers 2

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));
}
Sign up to request clarification or add additional context in comments.

1 Comment

Please explain what you did and why you did it. For people without programming experience (who are learning to code) only the source code is not sufficient. An alternative would be to write a comment in the code, something like // global variables, used to exchange the values between methods. A short explanation what a global variable is, would be great.
0

Declare the variable outside of the Timer.Tick event so that you can access

Refresh the combobox on the timer event itself when the value changes

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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.