简体   繁体   中英

Trying to make a button display something when another button is clicked (C#)

I am using Windows Forms Application with the language being C#. I have created 5 buttons and 3 labels were text will be displayed. Door Locked, Door Unlocked, Dog is home, Dog is not home, Show Risk = are the names of the buttons. What i am trying to do is to show the risk of the door being unlocked and the dog not being home as it will be easy for thieves to break in. I am stuck at the Show Risk button as i don't know how to use if statement and tell the button that if Unlock door and Dog is not home buttons are clicked then display "Thieves might break into the house"

public partial class Form1 : Form { public Form1() { InitializeComponent(); }

    private void btLock_Click(object sender, EventArgs e)
    {
        lbDoor.Text = "Door is now locked!";
    }

    private void btUnlock_Click(object sender, EventArgs e)
    {
        lbDoor.Text = "Door is now unlocked!";
    }

    private void btDogIsHome_Click(object sender, EventArgs e)
    {
        lbDog.Text = "Dog is home!";
    }

    private void btDogIsNotHome_Click(object sender, EventArgs e)
    {
        lbDog.Text = "Dog is not home!";
    }

    private void btShowRisk_Click(object sender, EventArgs e)
    {    
        if ()
        {
            lbDisplayRisk.Text = "Thieves might break into the house!";
        }
        else 
        {
            lbDisplayRisk.Text = "The house is secure!";
        }
    }
}

Here's how you can add state by introducing a boolean to represent the state of the door (Locked or Unlocked):

class SecuritySystem {
    // this variable maintains the state of the door
    private boolean doorLocked;

    // door lock click event handler
    private void btLock_Click(object sender, EventArgs e) {
        doorLocked = true;
        lbDoor.Text = "Door is now locked!";
    }

    // door unlock click event handler
    private void btUnlock_Click(object sender, EventArgs e) {
        doorLocked = false;
        lbDoor.Text = "Door is now unlocked!";
    }
}

Knowing this, can you figure out how to maintain the state of the Dog? (Hint, it's the same way!)

Now that you have the state of the Dog and the Door, can you figure out what you need to write in the if statement for the Risk? (FYI that logical tests that you put in the if statement are called predicates .)

Since you have two binary outcomes, this leaves you with a total of four cases to account for:

  1. Door Unlocked and Dog Not at Home (00)
  2. Door Unlocked and Dog at Home (01)
  3. Door Locked and Dog Not at Home (10)
  4. Door Lockd and Dog at Home (11)

The numbers on the right stand for: [Door State - 0 = unlocked; 1 = locked][Dog State - 0 is not at home; 1 = at home]

public partial class Form1 : Form { public Form1() { InitializeComponent(); }

private bool isDoorLocked =true;
private bool isDogHome =true;
private void btLock_Click(object sender, EventArgs e)
{
    lbDoor.Text = "Door is now locked!";
    isDoorLocked = true;

}

private void btUnlock_Click(object sender, EventArgs e)
{
    lbDoor.Text = "Door is now unlocked!";
    isDoorLocked = false;
}

private void btDogIsHome_Click(object sender, EventArgs e)
{
    lbDog.Text = "Dog is home!";
    isDogHome= true;
}

private void btDogIsNotHome_Click(object sender, EventArgs e)
{
    lbDog.Text = "Dog is not home!";
    isDogHome = false;
}

private void btShowRisk_Click(object sender, EventArgs e)
{    
    if (!isDogHome && !isDoorLocked )
    {
        lbDisplayRisk.Text = "Thieves might break into the house!";
    }
    else 
    {
        lbDisplayRisk.Text = "The house is secure!";
    }
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM