Make previous two buttons disappear when third is clicked in C# - c#

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

Related

Control flicker while dragging in run time

Stack Overflow users. In a C# VS 2010 Windows Form project I have a problem regarding control flicker when dragging a user created control around on a tab page during run time. I used the following code:
private void control_MouseMove(object sender, MouseEventArgs e)
{
if (isDragged)
{
Point newPoint = ((Control)sender).PointToScreen(new Point(e.X,
e.Y));
newPoint.Offset(ptOffset);
((Control)sender).Location = newPoint;
((Control)sender).Refresh();
}
}
private void control_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDragged = true;
Point ptStartPosition = ((Control)sender).PointToScreen(new
Point(e.X, e.Y));
ptOffset = new Point();
ptOffset.X = ((Control)sender).Location.X - ptStartPosition.X;
ptOffset.Y = ((Control)sender).Location.Y - ptStartPosition.Y;
}
else
{
isDragged = false;
}
}
private void control_MouseUp(object sender, MouseEventArgs e)
{
((Control)sender).Refresh();
isDragged = false;
}
private void createButton_PB_Click(object sender, EventArgs e)
{
int ctrlExists = 0;
string btnName = btnName_TB.Text;
foreach (Button button in tabControl1.SelectedTab.Controls)
{
if (button.Text == btnName)
{
ctrlExists = 1;
}
}
if (btnName_TB.Text != "" && ctrlExists == 0)
{
Button newButton = new Button();
newButton.Name = btnName.Replace(" ", String.Empty);
newButton.Name += "u";
newButton.Text = btnName;
tabControl1.SelectedTab.Controls.Add(newButton);
newButton.Left = 10;
newButton.Top = 420;
lastBtnClicked = newButton;
}
SetupClickEvents(tabControl1.SelectedTab);
}
So, the problem is that I can add a button and drag it around in run time. But, when I add another Button and drag it around...after I've done that, and go back to trying to drag the first button, that button flickers and acts as if it is trying to move all over the place. Sometimes it disappears. I feel like this has something to do with the fact that the controls are inside a tab page. Perhaps I am not properly calculating the "newPoint" variable. Any ideas guys?
Ok, so I found a few fundamental flaws related to the button creation and the events that were being added at the time of creation. I made some considerable changes and the issue seems to have gone away. Following is the updated code.
private void control_MouseMove(object sender, MouseEventArgs e)
{
if (isDragged)
{
Point newPoint = ((Control)sender).PointToScreen(new Point(e.X,
e.Y));
newPoint.Offset(ptOffset);
((Control)sender).Location = newPoint;
((Control)sender).Refresh();
}
}
private void control_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && checkBox1.Checked)
{
isDragged = true;
((Control)sender).MouseMove += new
MouseEventHandler(control_MouseMove);
Point ptStartPosition = ((Control)sender).PointToScreen(new
Point(e.X, e.Y));
ptOffset = new Point();
ptOffset.X = ((Control)sender).Location.X - ptStartPosition.X;
ptOffset.Y = ((Control)sender).Location.Y - ptStartPosition.Y;
}
else
{
isDragged = false;
}
}
private void control_MouseUp(object sender, MouseEventArgs e)
{
((Control)sender).MouseMove -= control_MouseMove;
((Control)sender).Refresh();
isDragged = false;
}
private void SetupClickEvents(Control control)
{
control.Click += new EventHandler(StoreLastClick);
control.MouseDown += new MouseEventHandler(control_MouseDown);
//control.MouseMove += new MouseEventHandler(control_MouseMove);
control.MouseUp += new MouseEventHandler(control_MouseUp);
}
private void createButton_PB_Click(object sender, EventArgs e)
{
ctrlExists = 0;
string btnName = btnName_TB.Text;
foreach (Button button in tabControl1.SelectedTab.Controls)
{
if (button.Name == btnName)
{
ctrlExists = 1;
}
}
if (btnName_TB.Text != "" && ctrlExists == 0)
{
Button newButton = new Button();
newButton.Name = btnName.Replace(" ", String.Empty);
newButton.Text = btnName;
tabControl1.SelectedTab.Controls.Add(newButton);
newButton.Left = 10;
newButton.Top = 420;
SetupClickEvents(newButton);
}
}
private void deleteButton_PB_Click(object sender, EventArgs e)
{
ctrlExists = 0;
if (lastCtrlClicked != null)
{
string btnName = lastCtrlClicked.Name;
foreach (Button button in tabControl1.SelectedTab.Controls)
{
if (button.Name == btnName)
{
ctrlExists = 1;
}
}
}
if (ctrlExists == 1 && lastCtrlClicked != null)
{
tabControl1.SelectedTab.Controls.Remove(lastCtrlClicked);
lastCtrlClicked.Dispose();
ctrlExists = 0;
}
lastCtrlClicked = null;
}

XO game using c# and ASP.NET

i'm new to C# (and programming at all) and i'm trying to write an 'XO' game along with ASP.NET
i'm getting a problem after the first player clicks a button.
turns doesn't switch and any click after the 1st does nothing. what is wrong with my code ?
public partial class GamePage : System.Web.UI.Page
{
Player player1 = new Player();
Player player2 = new Player();
int turn;
protected void Page_Load(object sender, EventArgs e)
{
this.turn = 0;
if (!IsPostBack)
{
Label1.Visible = true;
}
if (turn == 0)
{
Label1.Text = (Session["player1"] as Player).getname();
}
else
{
Label1.Text = (Session["player2"] as Player).getname();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Session["p1"] = player1;
Session["p2"] = player2;
player1.setsymbol("X");
player2.setsymbol("O");
if (Button1.Text == "")
{
if (turn == 0)
{
Button1.Text = player1.getsymbol();
Label1.Text = (Session["player2"] as Player).getname();
turn = 1;
}
else
{
Button1.Text = player2.getsymbol();
Label1.Text = (Session["player1"] as Player).getname();
turn = 0;
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
if (Button2.Text == "")
{
if (turn == 0)
{
Button2.Text = player1.getsymbol();
Label1.Text = (Session["player2"] as Player).getname();
turn = 1;
}
else
{
Button2.Text = player2.getsymbol();
Label1.Text = (Session["player1"] as Player).getname();
turn = 0;
}
}
}
protected void Button3_Click(object sender, EventArgs e)
{
if (Button3.Text == "")
{
if (turn == 0)
{
Button3.Text = player1.getsymbol();
Label1.Text = (Session["player2"] as Player).getname();
turn = 1;
}
else
{
Button3.Text = player2.getsymbol();
Label1.Text = (Session["player1"] as Player).getname();
turn = 0;
}
}
}
// this is an example - i have the same lines from button1 to 9
Everytime page renders, you set turn to 0 in Page_Load. Because Page_Load is executed upon every page load, you won't get any other value and this is probably the major issue here.
To properly support the lifetime of such variables that should keep value upon consecutive requests, wrap them in simple property:
public int turn
{
get
{
if ( Session["turn"] != null )
return (int)Session["turn"];
return 0; // default value if not set before
}
set
{
Session["turn"] = value;
}
}
This way everytime you refer to turn in your code, setting it to 0 or 1 or comparing the value to 0 or 1, you will refer to the same value, possibly stored during previous request(s).
this.turn=0; should be executed only when IsPostBack is false. Move this line inside if in your Page_Load.

Two simultaneous Textchanged Events firing in Winforms C#

When I type in the first textbox, it should run a conversion which appears in the second, and when I type in the second, it will appear in the first. However, when I type in the first textchanged event, it triggers the second, which disrupts entering in the first and vice versa. Is there a way I can disable firing the textchanged event when it is highlighted or something?
public void dB10_TextChanged(object sender, EventArgs e)
{
TextBox dB10 = sender as TextBox;
double dBV;
int i = dB10.Text.Trim().Length;
if (i > 0)
{
dBV = Convert.ToDouble(dB10.Text);
}
else
return;
UnitConverter dBConverter = new UnitConverter();
// Controls for if various radiobuttons were clicked
if (dBVRadio.Checked == true)
{
dBV = dBConverter.dBVToVolts(dBV);
voltage.Text = dBV.ToString();
}
else if (dBuRadio.Checked == true)
{
dBV = dBConverter.dBuToVolts(dBV);
voltage.Text = dBV.ToString();
}
}
public void voltage_TextChanged(object sender, EventArgs e)
{
TextBox voltage = sender as TextBox; //V >> dB10 (dBV/dBu)
int i = voltage.Text.Trim().Length;
double volts;
if (i > 0)
{
volts = Convert.ToDouble(voltage.Text);
}
else
return;
UnitConverter dBConverter = new UnitConverter();
if (dBVRadio.Checked == true)
{
dBuRadio.Checked = false;
volts = dBConverter.voltsTodBV(volts);
dB10.Text = volts.ToString();
}
else if (dBuRadio.Checked == true)
{
volts = dBConverter.voltsTodBu(volts);
dB10.Text = volts.ToString();
}
}
you can remove the handler of another textbox and then add it
public void dB10_TextChanged(object sender, EventArgs e)
{
voltage.TextChanged-= voltage_TextChanged;
TextBox dB10 = sender as TextBox;
double dBV;
int i = dB10.Text.Trim().Length;
if (i > 0)
{
dBV = Convert.ToDouble(dB10.Text);
}
else
return;
UnitConverter dBConverter = new UnitConverter();
// Controls for if various radiobuttons were clicked
if (dBVRadio.Checked == true)
{
dBV = dBConverter.dBVToVolts(dBV);
}
else if (dBuRadio.Checked == true)
{
dBV = dBConverter.dBuToVolts(dBV);
}
voltage.Text = dBV.ToString();
voltage.TextChanged+= voltage_TextChanged;
}
You can just use a bool variable:
bool escape = false;
public void dB10_TextChanged(object sender, EventArgs e)
{
if(escape)
return;
escape = true;
// your code
escape = false;
}
public void voltage_TextChanged(object sender, EventArgs e)
{
if(escape)
return;
escape = true;
// your code
escape = false;
}

disabling button after clicking it

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?

Problem when summing selected cells in DataGridview

I using this code for sum selected cells. Its work good but when user selecte cell where is letter is throws exceptions : ) how can i secure when in selectet cells is letters dont make sum
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
String filterStatus = DataGridViewAutoFilterColumnHeaderCell.GetFilterStatus(dataGridView1);
if (String.IsNullOrEmpty(filterStatus))
{
showAllLabel.Visible = false;
filterStatusLabel.Visible = false;
}
else
{
int result = -1;
Int32.TryParse(filterStatus, out result);
if (result != 0)
{
// it is a number
showAllLabel.Visible = true;
filterStatusLabel.Visible = true;
filterStatusLabel.Text = filterStatus;
}
else
{
// it can be a number yet won't help you with adding
}
}
}
this is my code
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
String filterStatus = DataGridViewAutoFilterColumnHeaderCell.GetFilterStatus(dataGridView1);
if (String.IsNullOrEmpty(filterStatus))
{
showAllLabel.Visible = false;
filterStatusLabel.Visible = false;
}
else
{
showAllLabel.Visible = true;
filterStatusLabel.Visible = true;
filterStatusLabel.Text = filterStatus;
}
}

Categories