Saif Elkholy
Saif Elkholy

Reputation: 37

How do I switch between 2 windows forms back and forth

I want to be able to change between the forms using an IR sensor.
I have created an edge variable that stores the edges detected when moving your hand across it.
So by moving my hands across the sensor I should be able to go back and forth between the forms.
However after the 2nd swipe I just get tons of errors.

This is the part of the code that is not working:

 static void g_detected(object sender,PinStatusEventArgs e)
    {
        edges++;
        switch(edges)
        {
            case 1:
                break;
            case 2:
                edges = 0;
                if (weatherView.Visible)
                {
                    weatherView.Visible = false;
                    stockView.Visible = true;
                }
                else if (!weatherView.Visible)
                {
                    weatherView.Visible = true;
                    stockView.Visible = false;
                }
                break;
        }
    }

Upvotes: 0

Views: 132

Answers (1)

Ozan Gunceler
Ozan Gunceler

Reputation: 1067

A better approach would be getting rid of checking the incremented variable. Instead we can use mod.

static void g_detected(object sender, PinStatusEventArgs e) {
  edges += 1;
  switch (edges % 2) {
    case 1:
      break;
    case 0:
      if (weatherView.Visible) {
        weatherView.Visible = false;
        stockView.Visible = true;
      } else if (!weatherView.Visible) {
        weatherView.Visible = true;
        stockView.Visible = false;
      }
      break;
    default:
      //Will never hit, just to handle general coding conventions.
      break;
  }
}

Upvotes: 1

Related Questions