2013-12-09 64 views
-1

任何人都可以帮忙吗?当我按下按钮7时,即使先按下按钮3或按钮1,我也无法确定为什么布尔floorOne总是设置为false。这应该是一个相当简单的问题,只有在初始化时,按下按钮2或按下按钮4时,它才会变为false。我不知道它是如何返回到错误的。C#总是假布尔

它可能有一个相对简单的解决方案,但我找不到它,谢谢你的时间。

编辑:当我调试时,它显示为false,我不知道这些信息是否会有所帮助。我知道很多代码可能不需要包含在这里,但为了万一有问题在那里我想我应该加入它。

Edit2:太棒了,非常感谢大家!

using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace Elevator 
{ 
    public partial class Form1 : Form 
    { 
     public bool doorsOpen; 
     public bool floorOne; 
     public bool groundFloor; 
     public Form1() 
     { 
      InitializeComponent(); 

      pictureBox1.Visible = false; // This is the bottom floor doors closed picture 
      pictureBox2.Visible = true; // This is the bottom floor doors open picture 
      pictureBox3.Visible = false; // This is the top floor doors closed picture 
      pictureBox4.Visible = false; // This is the top floor doors open picture 
      floorOne = false; 
      richTextBox1.Text = "Ground floor"; 
      button1.BackColor = Color.Gray; 
      button2.BackColor = Color.Gray; 
      button3.Enabled = true; 
      button4.Enabled = false; // This makes it impossible to click the buttons if 
      button5.Enabled = false; // the lift is already on that floor, to start with this is the 
      button6.Enabled = true; // ground floor. 
      button5.BackColor = Color.Black; 
      button6.BackColor = Color.Red; 
      doorsOpen = true; 
      richTextBox2.Text = "Doors open"; 
     } 

     public void Form1_Load(object sender, EventArgs e) 
     { 

     } 

     public void button3_Click(object sender, EventArgs e) 
     { 
      if (doorsOpen == true) 
      { 
       doorsOpen = false; 
       richTextBox2.Text = "Doors closed"; 
      } 
      pictureBox1.Visible = false; // This is the bottom floor doors closed picture 
      pictureBox2.Visible = false; // This is the bottom floor doors open picture 
      pictureBox3.Visible = true; // This is the top floor doors closed picture 
      pictureBox4.Visible = false; // This is the top floor doors open picture 
      bool floorOne = true; 
      button1.BackColor = Color.Gray; 
      button2.BackColor = Color.Gray; 
      richTextBox1.Text = "First floor"; 
      button3.Enabled = false; 
      button4.Enabled = true; 
      button5.Enabled = true; 
      button6.Enabled = false; 
      button5.BackColor = Color.Red; 
      button6.BackColor = Color.Black; 
     } 

     public void button4_Click(object sender, EventArgs e) 
     { 
      if (doorsOpen == true) 
      { 
       doorsOpen = false; 
       richTextBox2.Text = "Doors closed"; 
      } 
      pictureBox1.Visible = true; // This is the bottom floor doors closed picture 
      pictureBox2.Visible = false; // This is the bottom floor doors open picture 
      pictureBox3.Visible = false; // This is the top floor doors closed picture 
      pictureBox4.Visible = false; // This is the top floor doors open picture 
      bool floorOne = false; 
      button1.BackColor = Color.Gray; 
      button2.BackColor = Color.Gray; 
      richTextBox1.Text = "Ground floor"; 
      button3.Enabled = true; 
      button4.Enabled = false; 
      button5.Enabled = false; 
      button6.Enabled = true; 
      button5.BackColor = Color.Black; 
      button6.BackColor = Color.Red; 
     } 

     public void button7_Click(object sender, EventArgs e) 
     { 
      doorsOpen = true; 
      richTextBox2.Text = "Doors open"; 
      if (floorOne == true) 
      { 
      pictureBox1.Visible = false; // This is the bottom floor doors closed picture 
      pictureBox2.Visible = false; // This is the bottom floor doors open picture 
      pictureBox3.Visible = false; // This is the top floor doors closed picture 
      pictureBox4.Visible = true; // This is the top floor doors open picture 
      } 
      else if (floorOne != true) 
      { 
       pictureBox1.Visible = false; // This is the bottom floor doors closed picture 
       pictureBox2.Visible = true; // This is the bottom floor doors open picture 
       pictureBox3.Visible = false; // This is the top floor doors closed picture 
       pictureBox4.Visible = false; // This is the top floor doors open picture 
      } 
     } 

     public void button1_Click(object sender, EventArgs e) 
     { 
      if (doorsOpen == true) 
      { 
       doorsOpen = false; 
       richTextBox2.Text = "Doors closed"; 
      } 
      pictureBox1.Visible = false; // This is the bottom floor doors closed picture 
      pictureBox2.Visible = false; // This is the bottom floor doors open picture 
      pictureBox3.Visible = true; // This is the top floor doors closed picture 
      pictureBox4.Visible = false; // This is the top floor doors open picture 
      bool floorOne = true; 
      button1.BackColor = Color.Yellow; 
      button2.BackColor = Color.Gray; 
      richTextBox1.Text = "First floor"; 
      button3.Enabled = false; 
      button4.Enabled = true; 
      button5.Enabled = true; 
      button6.Enabled = false; 
      button5.BackColor = Color.Red; 
      button6.BackColor = Color.Black; 

     } 

     public void button2_Click(object sender, EventArgs e) 
     { 
      if (doorsOpen == true) 
      { 
       doorsOpen = false; 
       richTextBox2.Text = "Doors closed"; 
      } 
      pictureBox1.Visible = true; // This is the bottom floor doors closed picture 
      pictureBox2.Visible = false; // This is the bottom floor doors open picture 
      pictureBox3.Visible = false; // This is the top floor doors closed picture 
      pictureBox4.Visible = false; // This is the top floor doors open picture 
      bool floorOne = false; 
      button1.BackColor = Color.Gray; 
      button2.BackColor = Color.Yellow; 
      richTextBox1.Text = "Ground floor"; 
      button3.Enabled = true; 
      button4.Enabled = false; 
      button5.Enabled = false; 
      button6.Enabled = true; 
      button5.BackColor = Color.Black; 
      button6.BackColor = Color.Red; 
     } 
    } 
} 
+3

边注'如果(floorOne ==真)......否则,如果(floorOne != true)' - 你确定你需要检查一楼是不是真的,如果不是真的?完全相同应该用'if(floorOne).. else'来验证。 –

+3

你应该更好地为你的变量和控件命名。 – Maarten

+0

有时你应该使用调试器... – giammin

回答

6

因为你的一些方法你声明,而不是修改现有Form1的领域floorOne变量。

更换

bool floorOne = true; 

floorOne = true; 
1

在将Button3和将Button4单击事件删除布尔变量的声明,只是更新了它的价值。因为你已经定义了布尔floorOne所以你不需要再次声明它。

更新

bool floorOne = true; 

floorOne = true; 
0

这真的有一个简单的解决方案。 假设你点击了button1,并且想要将floorOne设置为true,但是你真正要做的是: 创建新的LOCAL布尔值floorOne并将其设置为true(bool floorOne = true;)。但是这不会改变你的GLOBAL价值floorOne。它有相同的名称,但在这种情况下并不重要。 删除button1中代码中的“bool”文本,然后“floorOne”将成为您的全局值。

为Button1(对于所有其他按键相同的变化)

新代码:

public void button1_Click(object sender, EventArgs e) 
    { 
     if (doorsOpen == true) 
     { 
      doorsOpen = false; 
      richTextBox2.Text = "Doors closed"; 
     } 
     pictureBox1.Visible = false; // This is the bottom floor doors closed picture 
     pictureBox2.Visible = false; // This is the bottom floor doors open picture 
     pictureBox3.Visible = true; // This is the top floor doors closed picture 
     pictureBox4.Visible = false; // This is the top floor doors open picture 
     floorOne = true; // HERE IS THE CHANGE 
     button1.BackColor = Color.Yellow; 
     button2.BackColor = Color.Gray; 
     richTextBox1.Text = "First floor"; 
     button3.Enabled = false; 
     button4.Enabled = true; 
     button5.Enabled = true; 
     button6.Enabled = false; 
     button5.BackColor = Color.Red; 
     button6.BackColor = Color.Black; 
    } 

试试这个:

public partial class Form1 : Form 
{ 
    public bool floorOne; // global value (automatically set to false (default value of false) 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    public void button3_Click(object sender, EventArgs e) 
    { 
     bool floorOne = true; // new local value named floorOne (doesn change your global value) 
     MessageBox.Show("value of global floorOne is: " + this.floorOne.ToString()); 
     // this.floorOne is your global value (try to find something about "this.") 
     this.floorOne = true; 
     MessageBox.Show("value of global floorOne is: " + this.floorOne.ToString()); 
    } 
}