Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Can anyone help? I can't work out why the bool floorOne is always set to false when I press button7, even if I press button3 or button1 first. This should be a fairly simple issue, it's only made false when initialised, when button2 is pressed or when button4 is pressed. I have no idea how it's returning to false.
It probably has a relatively simple solution, but I can't find it, thank you for your time.
Edit: When I debug, it shows up as false, I don't know if that information'll help at all. I know a lot of the code probably doesn't need to be included here, but just in case there was an issue in there somehow I thought I should add it in.
Edit2: Fantastic, thank you very much everyone!
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;
}
}
}
Because in some of your methods you are declaring a new floorOne variable, instead of modifying the existing Form1's field.
Replace
bool floorOne = true;
with
floorOne = true;
In Button3 and button4 click event remove the declaration of bool variable and just update the value of it. Because you already have defined the bool floorOne so you don't need to declare it again.
Update
bool floorOne = true;
To
floorOne = true;
this realy have a simple solution.
Lets say, you click button1 and you want to set floorOne to true, but what you realy do is:
create new LOCAL boolean value floorOne and set it to true (bool floorOne = true;). But this doesnt change your GLOBAL value floorOne. It has the same name, but that doesnt matter in this case.
Delete the "bool" text in your code in button1 and then "floorOne" will be your global value.
new code for button1 (same change for all other buttons):
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;
}
Try this:
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());
}
}
Related
I have this windows form having three Group Boxes positioned at the same position.
On a selection of different option i want to make only one of them visible and rest invisible
By Default all are invisible .
here is the piece of code . I see only on selecting D1 groupBox1 is visible and on Selecting D2 and D3 groupBox1 disappears but groupBox2 and 3 never appears.
private void Form1_load(object sender,EventArgs e)
{
comboBoxCategory.Items.Add("A");
comboBoxCategory.Items.Add("B");
comboBoxCategory.Items.Add("C");
comboBoxCategory.Items.Add("D");
groupBox1.Visible = false;
groupBox2.Visible = false;
groupBox3.Visible = false;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBoxMovie.Items.Clear();
switch(comboBoxCategory.SelectedItem.ToString())
{
case "A":
this.comboBoxMovie.Items.Add("A1");
this.comboBoxMovie.Items.Add("A2");
this.comboBoxMovie.Items.Add("A3");
this.comboBoxMovie.Items.Add("A4");
break;
case "B":
this.comboBoxMovie.Items.Add("B1");
this.comboBoxMovie.Items.Add("B2");
this.comboBoxMovie.Items.Add("B3");
this.comboBoxMovie.Items.Add("B4");
break;
case "C":
this.comboBoxMovie.Items.Add("C1");
this.comboBoxMovie.Items.Add("C2");
this.comboBoxMovie.Items.Add("C3");
this.comboBoxMovie.Items.Add("C4");
break;
case "D":
this.comboBoxMovie.Items.Add("D1");
this.comboBoxMovie.Items.Add("D2");
this.comboBoxMovie.Items.Add("D3");
this.comboBoxMovie.Items.Add("D4");
break;
}
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if(comboBoxMovie.SelectedItem.ToString() == "D1")
{
groupBox1.Visible = true;
groupBox2.Visible = false;
groupBox3.Visible = false;
}
if (comboBoxMovie.SelectedItem.ToString() == "D2")
{
groupBox1.Visible = false;
groupBox2.Visible = true;
groupBox3.Visible = false;
}
if (comboBoxMovie.SelectedItem.ToString() == "D3")
{
groupBox1.Visible = false;
groupBox2.Visible = false;
groupBox3.Visible = true;
}
}
You can use a TabControl with the TabPages you need for it. Otherwise, check that groupboxes are not one inside the other and afterr clearing and adding items to comboBoxMovie you must set the selectedIndex=0 or ... so that SelectedIndexChange event can be invoked.
Tip: you can control groupBoxes visibility just by comparing comboBoxMovie.SelectedItem.ToString() any value instead using 3 if statements:
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
groupBox1.Visible = comboBoxMovie.SelectedItem.ToString() == "D1";
groupBox2.Visible = comboBoxMovie.SelectedItem.ToString() == "D2";
groupBox3.Visible = comboBoxMovie.SelectedItem.ToString() == "D3";
}
I'm using C# and I have a WinForms application.
I have a main form which contains some buttons, lets say button A and B.
I have another form,FormB which have 2 panels with different dimensions.
What I'm trying to do is when the user clicks on button A is to show FormB with
the dimensions of the first panel and present the first panel.
And when the user clicks on button B is to show formB with the second panel dimensions
and present the second panel.
I know How to present those panel in each case but the form size doesn't change as I expected.
I tried to use the Dock property and set it to fill.. but nothing happens.
private void FormB_Load(object sender, EventArgs e)
{
panel1.Location = panel2.Location = new Point();
timer1.Start();
if (!first)
{
panel1.Visible = false;
panel2.Visible = true;
}
else
{
panel1.Visible = true;
panel2.Visible = false;
}
this.Dock = DockStyle.Fill;
}
You should use panel1.Dock = DockStyle.Fill or panel2.Dock = DockStyle.Fill, not this.Dock = DockStyle.Fill because you want to set the Dock property of the Panel not the Form.
private void FormB_Load(object sender, EventArgs e)
{
panel1.Location = panel2.Location = new Point();
timer1.Start();
if (!first)
{
panel1.Visible = false;
panel2.Visible = true;
panel2.Dock = DockStyle.Fill;
}
else
{
panel1.Visible = true;
panel2.Visible = false;
panel1.Dock = DockStyle.Fill;
}
}
EDIT But previous code will change the size of the panels according to the size of the form. Since you want to set the size of the form to be the size of the panel(s), then you should set the Size property of the Form like this:
private void FormB_Load(object sender, EventArgs e)
{
panel1.Location = panel2.Location = new Point();
timer1.Start();
if (!first)
{
panel1.Visible = false;
panel2.Visible = true;
this.Size = new Size(panel2.Size.Width + 16, panel2.Size.Height + 38);
}
else
{
panel1.Visible = true;
panel2.Visible = false;
this.Size = new Size(panel1.Size.Width + 16, panel1.Size.Height + 38);
}
}
I have this button click event code:
private void button1_Click(object sender, EventArgs e)
{
offlineOnline = false;
init();
backgroundWorker1.RunWorkerAsync();
button1.Enabled = false;
this.Text = "Processing...";
label6.Text = "Processing...";
label6.Visible = true;
button2.Enabled = false;
checkBox1.Enabled = false;
checkBox2.Enabled = false;
numericUpDown1.Enabled = false;
button3.Enabled = true;
button6.Enabled = false;
button4.Enabled = true;
button5.Enabled = false;
listBox1.Enabled = false;
}
I set the listBox1 Enabled to be false. But it dosn't look mice. It looks like the listBox still active but in fact the user can't use it.
What i want is some idea what to do with the listBox while the process is on ? ( When clicking the button1 ).
Quick and dirty: disable it and change the background colour to a murky grey.
A little less dirty, subclass ListBox and set the colour when Enabled changes, possibly based on the current Windows theme.
i have here 3 buttons.
when i click on the button 1, it will disable button 1 then enable the second button, then same proces as it reach the last button. but i think there is something wrong with my code. it doesn't disable when i click on the first button
button1 is enabled and button2 and 3 is disabled when it loads.
private void groupBox1_Enter(object sender, EventArgs e)
{
Button btn = sender as Button;
if (btn == button1)
{
button1.Enabled = false;
button2.Enabled = true;
button3.Visible = false;
button3.Enabled = false;
MessageBox.Show("button 1 disabled");
}
else if (btn == button2)
{
button1.Enabled = false;
button2.Enabled = false;
button3.Visible = true;
button3.Visible = true;
MessageBox.Show("button 2 disabled");
}
else if (btn == button3)
{
button1.Enabled = false;
button2.Enabled = false;
button3.Visible = false;
button3.Visible = false;
MessageBox.Show("button 3 disabled");
}
}
Are you subscribing to the right event? It says groupBox1_Enter.
Button[] buttons = null; // Initialize somewhere with all the buttons.
void OnButtonClick(object sender, EventArgs e)
{
for (int index = 0; index < buttons.Length; index++)
{
if (buttons[index] == sender)
{
buttons[index].Enabled = buttons[index].Visible = false;
}
else
{
buttons[index].Enabled = buttons[index].Visible = true;
}
}
}
Sorry I miss read your post. In order below
Button[] buttons = null;
void OnButtonClick(object sender, EventArgs e)
{
int buttonIndex = Array.IndexOf(buttons, sender);
for (int index = 0; index < buttons.Length; index++)
{
if (index == buttonIndex + 1)
{
buttons[index].Enabled = buttons[index].Visible = true;
}
else
{
buttons[index].Enabled = buttons[index].Visible = false;
}
}
}
Check what are the initial values of each button in the properties of each button.
Its better to have a Initializer to set the properties of the button before changing it.
Don't put the code inside the Enter event of the GroupBox, this event will have the groupoBow as sender. Subscribe the ButtonPressed event of one of the button and eventually use the same generated method to subscribe the ButtonPressed event of the 2 other buttons (if you want to use the if-else statements as you wrote)
Maybe you should try it this way:
private void groupBox1_Enter(object sender, EventArgs e)
{
Button btn = sender as Button;
if (btn == button1)
{
button1.Enabled = false;
button2.Enabled = true;
button3.Enabled = false;
MessageBox.Show("button 1 is disabled");
}
else if (btn == button2)
{
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = true;
MessageBox.Show("button 1 & button 2 are disabled");
}
else if (btn == button3)
{
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
MessageBox.Show("button 3 disabled");
}
}
is this the way you think it has to work or am i getting this wrong?
I have a form with three buttons and a tabcontrol which has three tabpages.
The buttons are placed outside the tabcontrol. I want button1 to be visible when tabpage1 selected, button2 visible when tabpage2 is selected and button3 visible when tabpage3 is selected.
The code I currently have is as follows:
button1.Visible = false;
button2.Visible = false;
button3.Visible = false;
if (tabControl1.SelectedTab == tabPage1)
{ button1.Visible = true; }
else if (tabControl1.SelectedTab == tabPage2)
{ button2.Visible = true; }
else if (tabControl1.SelectedTab == tabPage3)
{ button3.Visible = true; }
This code isn't working.
The code i tried with .Enabled didnt work too. I am using visual studio 2010 and i drag n drop a tabcontrol with three tabpages and three buttons also are OUTSIDE of the tabcontrol. This isnt working too.
private void Form1_Load(object sender, EventArgs e){
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
if (tabControl1.SelectedTab == tabPage1){
button1.Enabled = true;
button2.Enabled = false;
button3.Enabled = false;
} else if (tabControl1.SelectedTab == tabPage2){
button1.Enabled = false;
button2.Enabled = true;
button3.Enabled = false;
} else if (tabControl1.SelectedTab == tabPage3){
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = true;
}
}
button1.Visible = tabControl1.SelectedTab == tabPage1;
button2.Visible = tabControl1.SelectedTab == tabPage2;
button3.Visible = tabControl1.SelectedTab == tabPage3;