I need to implement a check box to switch between two methods enabling/disabling some control. I am using the following code, I tried also in other ways but no luck.
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)//this is working
{
trackBar2.Enabled = false;
button3.PerformClick();
textBox8.Enabled = true;
}
else// this is supposed to work if checkbox is unchecked but doesn't work
{
trackBar2.Enabled = true;
textBox8.Enabled = false;
}
}
The result I get is always the same. If I check the checkbox the first condition is meet and it is fine. If I uncheck the text box, nothing happen and does not go back to the first condition either.
How can I detect the checked/unchecked condition?
You could also write that as:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
trackBar2.Enabled = !checkBox1.Checked;
textBox8.Enabled = checkBox1.Checked;
if (checkBox1.Checked)
{
button3.PerformClick();
}
}
I think you should add if(checkBox1.Checked == false) to the else :
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)//this is working
{
trackBar2.Enabled = false;
button3.PerformClick();
textBox8.Enabled = true;
}
else if(checkBox1.Checked == false)
{
trackBar2.Enabled = true;
textBox8.Enabled = false;
}
}
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 have an application for kiosk machine that appears always on the top and fullscreen. Also, I have to turn off explorer.exe.
Therefore, I will not be able to access anything without a keyboard.
I'm thinking to make gestures or invincible buttons so that I can turn on explorer.exe without keyboard.
I would like to know if there is a way to detect if two buttons are clicked at the same time. I've tried using the following code but it is not working.
PS: I can't debug it line by line as my PC do not have touchscreen.
Therefore, I cannot find out which line causes the problem.
private bool button1WasClicked = false;
private bool button2WasClicked = false;
private void button1_MouseDown(object sender, MouseEventArgs e)
{
button1WasClicked = true;
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
button1WasClicked = false;
}
private void button2_MouseUp(object sender, MouseEventArgs e)
{
button2WasClicked = false;
}
private void button2_MouseDown(object sender, MouseEventArgs e)
{
if (button1WasClicked == true)
{
Process.Start(Path.Combine(Environment.GetEnvironmentVariable("windir"), "explorer.exe"));
Application.Exit();
button1WasClicked = false;
}
}
You can't click two buttons at once with a mouse or keyboard, and if you're talking about using a touchscreen, the WinForms framework doesn't support them (taps will simply be interpreted as individual mouse clicks at best). You'll want to look at using the Surface SDK or something else instead.
I've found a different solution where the buttons(panels) have to be clicked in a certain sequence to achieve what I wanted. I've also added a timer. Below is my code.
private bool panel1WasClicked = false;
private bool panel2WasClicked = false;
int second = 0;
private void panel1_Click(object sender, EventArgs e)
{
MaintenanceTimer.Interval = 500;
MaintenanceTimer.Start();
second = 0;
if (panel1WasClicked == false)
{
panel1WasClicked = true;
}
else
{
panel1WasClicked = false;
}
}
private void panel2_Click(object sender, EventArgs e)
{
if (panel2WasClicked == false && panel1WasClicked == true)
{
panel2WasClicked = true;
}
else
{
panel2WasClicked = false;
}
}
private void panel3_Click(object sender, EventArgs e)
{
if (panel1WasClicked && panel2WasClicked == true)
{
//Do something
}
panel1WasClicked = false;
panel2WasClicked = false;
MaintenanceTimer.Stop();
}
private void MaintenanceTimer_Tick(object sender, EventArgs e)
{
second += 1;
if (second >= 5)
{
MaintenanceTimer.Stop();
second = 0;
panel1WasClicked = false;
panel2WasClicked = false;
}
}
I have tried to code a button such that it is disabled upon form loading however enabled once a textbox has had text entered. My code is below, which is probably familiar:
Public Form()
{
InitializeComponent();
this.button1.enabled = false;
}
private void textbox_TextChanged (object sender, EventArgs e)
{
button1.Enabled = !string.IsNullOrWhiteSpace(textbox.Text);
}
The button indeed loads up disabled, the enabling function doesn't work upon text input and I'm not sure what the issue could be. It is a modal form is that matters. I was wondering if maybe I needed an event listener (although I'm not certain how exactly they work).
Check your Designer.cs file and make sure you have event handler registration there. Something like this:
this.textBox.TextChanged += new System.EventHandler(this.textBox_TextChanged);
Will this work? I can't really see a problem with your code though...
button1.Enabled = textbox.Text != "";
I hope this helps.
May not be the solution to your problem, but this would be the fastest check for one's computer to perform (at least if you let the JIT compiler optimize your code):
button1.Enabled = textbox.Text.Length > 0;
try this
private void Form1_Load(object sender, EventArgs e)
{
button1.Enabled = false;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text))
{
button1.Enabled = true;
}
}
You could try either of the following on the TextChanged property of the TextBox :
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textbox.Text.Length > 0)
{
button1.Enabled = true;
}
else
button1.Enabled = false;
}
or, using the string.IsNullOrEmpty method:
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textbox.Text))
{
button1.Enabled = true;
}
else
button1.Enabled = false;
}
The the below line:
button1.Enabled = !string.IsNullOrWhiteSpace(textbox.Text);
If it didn't work for you or if you are getting an error, then probably because the IsNullOrWhiteSpace method was introduced in .NET 4
I have two checkboxes on my form; chkBuried and chkAboveGround. I want to set it up so if one is checked, the other is unchecked. How can I do this?
I have tried the CheckChanged property:
private void chkBuried_CheckedChanged(object sender, EventArgs e)
{
chkAboveGround.Checked = false;
}
private void chkAboveGround_CheckedChanged(object sender, EventArgs e)
{
chkBuried.Checked = false;
}
And it works, just not as well as I hoped. That is, when I check chkBuried, then check chkAboveGround, both boxes become unchecked before I can check another one again.
modify your code as below.
private void chkBuried_CheckedChanged(object sender, EventArgs e)
{
chkAboveGround.Checked = !chkBuried.Checked;
}
private void chkAboveGround_CheckedChanged(object sender, EventArgs e)
{
chkBuried.Checked = !chkAboveGround.Checked;
}
I suggest you use check_click instead of check_changed
private void checkBox1_Click(object sender, EventArgs e)
{
checkBox2.Checked = false;
checkBox3.Checked = false;
}
private void checkBox2_Click(object sender, EventArgs e)
{
checkBox1.Checked = false;
checkBox3.Checked = false;
}
private void checkBox3_Click(object sender, EventArgs e)
{
checkBox1.Checked = false;
checkBox2.Checked = false;
}
The reason for the behavior you have explained is that you are using CheckedChanged event, which means that when you are setting the Checked property of a CheckBox manually, the event is also fired, causing another box to react again.
Therefore, the following might help you:
private void chkBuried_CheckedChanged(object sender, EventArgs e)
{
if (chkBuried.Checked == true) {
chkAboveGround.Checked = false;
} else {
chkAboveGround.Checked = true;
}
}
private void chkAboveGround_CheckedChanged(object sender, EventArgs e)
{
if (chkAboveGround.Checked == true) {
chkBuried.Checked = false;
} else {
chkBuried.Checked = true;
}
}
UPDATE 29.03.2020: functionally the code in my answer is the same as the answer given by Riz. Nevertheless, I am leaving the code as I put it originally since it might make the whole situation easier to understand for the people who are new to coding. If you are to implement anything similar in production code, please use the answer by Riz as an example.
I would prefer radio buttons, but you can do something like this:
public void CheckACheckBox(Checkbox ck)
{
foreach (Control ckb in this.Controls)
{
if ((ckb is CheckBox) && (ckb == ck))
ck.Checked = true;
else
ck.Checked = false;
}
}
List<CheckBox> groupOfCheckBoxes = new List<CheckBox>();
void InitFunction() {
groupOfCheckBoxes.Add(checkbox1);
groupOfCheckBoxes.Add(checkbox2);
groupOfCheckBoxes.Add(checkbox3);
foreach (CheckBox cb in groupOfCheckBoxes)
cb.Click += checkbox_Click
}
void checkbox_Click(object sender, EventArgs e)
{
foreach (CheckBox cb in groupOfCheckBoxes) {
cb.IsChecked = cb == sender;
}
}
However I would suggest radio boxes as well.
The code above is untested and may have some typos
Better version, allows the user to uncheck boxes.
private void chkOne_CheckedChanged(object sender, EventArgs e)
{
if (chkTwo.Checked == true)
{
chkTwo.Checked = !chkOne.Checked;
}
}
private void chkTwo_CheckedChanged(object sender, EventArgs e)
{
if (chkOne.Checked == true)
{
chkOne.Checked = !chkTwo.Checked;
}
}
The best option and easiest way for me was to create a Click event instead of a CheckedChanged event.
This method works perfectly with two or more checkbox and allows to have them all unchecked.
private void chkOne_Click(object sender, EventArgs e)
{
chkTwo.Checked = false;
}
private void chkTwo_Click(object sender, EventArgs e)
{
chkOne.Checked = false;
}
This will work for two unchecked boxes, since they are already unchecked it is simpler.
I had to do this myself also.
private void customer_IsCheckedChanged(object sender, EventArgs e)
{
business.IsChecked = false;
}
private void business_IsCheckedChanged(object sender, EventArgs e)
{
customer.IsChecked = false;
}
I needed to show or not show a table when activating the CheckBox, how they were two, if I activated both, everything was fine, but if I tried to deactivate one later, the other was also deactivated. PD: The default value for the tables that I used was Visible=false. The solution I got was the following:
protected void YourNameOfMethodForBothCheckBox(object sender, EventArgs e)
{
if (CheckBox_1.Checked == true)
{
Table_1.Visible = true;
if (CheckBox_2.Checked == true)
{
Table_2.Visible = true;
}
else { Table_2.Visible = false; }
}
else
{
Table_1.Visible = false;
if (CheckBox_2.Checked == false)
{
Table_2.Visible = false;
}
else
{
Table_2.Visible = true;
}
}
}
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