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";
}
That's my code below and it works perfectly fine, except there is one thing that doesn't work correctly. When I click two buttons to match, they should stay visible until the user clicks a third button. How can I do that? Thank you.
namespace Memorija_Seminarska
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
tableLayoutPanel1.Enabled = false;
label1.Visible = false;
label2.Visible = false;
label3.Visible = false;
progressBar1.Visible = false;
label2.Text = vreme.ToString();
}
public int vreme = 150;
Random random = new Random();
Button firstClicked = null;
Button secondClicked = null;
List<string> drzava = new List<string>()
{
"Македонија","Македонија", "Бугарија","Бугарија", "Србија","Србија",
"Германија","Германија", "Канада","Канада", "Шпанија","Шпанија",
"Португалија","Португалија", "Австрија","Австрија", "Данска","Данска",
"Индија","Индија", "Италија","Италија", "Англија","Англија",
"Турција","Турција", "Грција","Грција","Хрватска","Хрватска",
"Холандија","Холандија", "Русија", "Русија", "Швајцарија","Швајцарија"
};
private void startButton_Click(object sender, EventArgs e)
{
Add();
tableLayoutPanel1.Enabled = true;
label1.Visible = true;
label2.Visible = true;
progressBar1.Visible = true;
timer2.Start();
timer3.Start();
}
private void Add()
{
foreach (Control control in tableLayoutPanel1.Controls)
{
Button b = control as Button;
if (b != null)
{
int randNum = random.Next(drzava.Count);
b.Text = drzava[randNum];
b.ForeColor = b.BackColor;
drzava.RemoveAt(randNum);
}
}
}
private void button_Click(object sender, EventArgs e)
{
if (timer1.Enabled == true)
return;
Button clickedButton = sender as Button;
if (clickedButton != null)
{
if (clickedButton.ForeColor == Color.Black)
return;
if (firstClicked == null)
{
firstClicked = clickedButton;
firstClicked.ForeColor = Color.Black;
return;
}
secondClicked = clickedButton;
secondClicked.ForeColor = Color.Black;
Win();
if (firstClicked.Text == secondClicked.Text)
{
firstClicked.BackColor = Color.GreenYellow;
secondClicked.BackColor = Color.GreenYellow;
firstClicked = null;
secondClicked = null;
return;
}
timer1.Start();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
firstClicked.ForeColor = firstClicked.BackColor;
secondClicked.ForeColor = secondClicked.BackColor;
firstClicked = null;
secondClicked = null;
}
private void timer2_Tick(object sender, EventArgs e)
{
vreme--;
label2.Text = vreme.ToString();
if (vreme == 0)
{
tableLayoutPanel1.Enabled = false;
label3.Text = "Game over!";
label3.Visible = true;
label2.Visible = false;
timer2.Stop();
timer3.Stop();
label1.Visible = false;
progressBar1.Visible = false;
}
}
private void timer3_Tick(object sender, EventArgs e)
{
progressBar1.Value -= 1;
if (progressBar1.Value == 0)
{
timer3.Stop();
}
}
private void Win()
{
foreach (Control control in tableLayoutPanel1.Controls)
{
Button button1 = control as Button;
if (button1 != null)
{
if (button1.ForeColor == button1.BackColor)
{
return;
}
}
}
label3.Text = "Браво!!!";
label3.Visible = true;
tableLayoutPanel1.Enabled = false;
timer2.Stop();
timer3.Stop();
label2.Visible = false;
progressBar1.Visible = false;
label1.Visible = false;
}
}
}
As far as I can see, your timer1_Tick handler performs the hiding automatically when it's time period expires. In case you want this hiding to happen manually when third card is clicked, you should not hide the buttons there, but should just perform a check in the beginning of button_Click:
private void button_Click(object sender, EventArgs e)
{
//two cards are open and not matching (if they matched, they would be already null)
if ( firstClicked != null && secondClicked != null )
{
//hide the buttons
firstClicked.ForeColor = firstClicked.BackColor;
secondClicked.ForeColor = secondClicked.BackColor;
firstClicked = null;
secondClicked = null;
}
}
And delete the timer1.Start() from the end of the event handler.
im not good at understanding other people code, but as far as i look through, you do this in here:
private void button_Click(object sender, EventArgs e)
Check for null
get reference of button 1 and return
get reference of button 2
do win() method
check for equality
here you should send step 5 to front line, check for null as MZetko said, and then check for equality, so it will be third click, else if you get reference as you did, after second button filled up, the checking will also launch
i hope i were helpful
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;
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I enable this timer in C#?
Im trying to get a little project running. When I use a break point it goes through the code correctly, but when running the program at normal speed it the sequence runs too fast. Im trying to get the traffic lights sequence to change every 1 second. What is wrong with this code? Its a simple sequence of traffic lights, incase your interested :). Newbie project.
}
public int counter = 0;
private void rbStart_CheckedChanged(object sender, EventArgs e)
{
Light_timer.Start();
counter++;
if (counter == 1)
{
pbRed.Visible = true;
pbAmber.Visible = false;
pbGreen.Visible = false;
}
else if (counter == 2)
{
pbRed.Visible = true;
pbAmber.Visible = true;
pbGreen.Visible = false;
}
else if (counter == 3)
{
pbRed.Visible = false;
pbAmber.Visible = false;
pbGreen.Visible = true;
}
else if (counter == 4)
{
pbRed.Visible = false;
pbAmber.Visible = true;
pbGreen.Visible = false;
}
else if (counter == 5)
{
pbRed.Visible = true;
pbAmber.Visible = false;
pbGreen.Visible = false;
}
else
{
counter = 0;
}
}
private void rbStop_CheckedChanged(object sender, EventArgs e)
{
pbRed.Visible = false;
pbAmber.Visible = false;
pbGreen.Visible = false;
}
private void Form1_Load(object sender, EventArgs e)
{
Light_timer.Tick += new EventHandler(rbStart_CheckedChanged);
Light_timer.Interval = 1000;
Light_timer.Stop();
}
}
}
You're hooking up the event handler every time the timer elapses and so on... Try this:
private void Form1_Load(object sender, EventArgs e)
{
Light_timer = new Timer();
Light_timer.Tick += new EventHandler(TimerElapsed);
Light_timer.Interval = 1000;
}
private void TimerElapsed(object sender, EventArgs e)
{
counter++;
if (counter == 1)
{
pbRed.Visible = true;
pbAmber.Visible = false;
pbGreen.Visible = false;
}
else if (counter == 2)
{
pbRed.Visible = true;
pbAmber.Visible = true;
pbGreen.Visible = false;
}
else if (counter == 3)
{
pbRed.Visible = false;
pbAmber.Visible = false;
pbGreen.Visible = true;
}
else if (counter == 4)
{
pbRed.Visible = false;
pbAmber.Visible = true;
pbGreen.Visible = false;
}
else if (counter == 5)
{
pbRed.Visible = true;
pbAmber.Visible = false;
pbGreen.Visible = false;
}
else
{
counter = 0;
Light_timer.Stop();
}
}
private void rbStart_CheckedChanged(object sender, EventArgs e)
{
Light_timer.Start();
}
private void rbStop_CheckedChanged(object sender, EventArgs e)
{
Light_timer.Stop();
pbRed.Visible = false;
pbAmber.Visible = false;
pbGreen.Visible = false;
}
In the code below, the datagridview1_SelectionChanged event which fires after the selection has been changed through the code in datagridview1_RowsAdded event handler, the CurrentRow property is null. but I have just set it in datagridview1_RowsAdded handler and it is not null in there.
However, if I comment the two lines in datagridview1_RowsAdded handler and select the row through mouse click on any row, the program works fine. Can anyone tell me why this is happening?
Here’s my code:
private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[1];
dataGridView1.CurrentCell.Selected = true;
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (dataGridView1.CurrentRow != null)
{
if (dataGridView1.CurrentRow.Index != -1)
{
dataGridView2.Enabled = true;
dataGridView3.Enabled = true;
dataGridView4.Enabled = true;
}
else
{
dataGridView2.Enabled = false;
dataGridView3.Enabled = false;
dataGridView4.Enabled = false;
}
}
else
{
dataGridView2.Enabled = false;
dataGridView3.Enabled = false;
dataGridView4.Enabled = false;
}
}
In your RowsAdded method, you have selected the current cell but not the current row. You can select the current row with this:
dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Selected = true;