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;
}
}
}
I have a handler for a C# panels VisibleChanged event. But how do I detect if the visibility is being set to true or false??
public void Parent_VisibleChanged(object sender, System.EventArgs e)
{
if(Visible = true)
{
// do what i want to do
}
}
You should use == and not =
if(Visible == true)
You should do something like this inside the event:
if (((Panel)sender).Visible)
MessageBox.Show("Visible.");
else
MessageBox.Show("Not Visible.");
Here are two ways:
private void panel1_VisibleChanged(object sender, EventArgs e)
{
// use sending object
Panel panel = sender as Panel;
if (panel.Visible == false)
;
// alternate use name of object
if (panel1.Visible == false)
;
}
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;
}
}
I'm trying to make it in my program, if you click on a text box it will select it all. And then if you click it again it deselects it all.
I've tried making it like this..
private void url_MouseDown(object sender, MouseEventArgs e)
{
url.ReadOnly = false;
url.SelectAll();
url.DeselectAll();
}
I know the url.DeselectAll(); is in the wrong spot.
Any help? Thanks in advance!
Clicking the textbox itself clears a selection, so you'd have to do something like this;
bool selected;
private void url_MouseDown(object sender, MouseEventArgs e)
{
url.ReadOnly = false;
if (!selected)
{
selected = true;
url.SelectAll();
}
else
{
selected = false;
url.DeselectAll();
}
}
Your current code first calls
url.SelectAll();
and then immediately calls
url.DeselectAll();
Instead, check the current state of the item you are trying to toggle. It's not clear to me from the question exactly what that is, so in pseudocode:
private bool isSelected = false;
private void url_MouseDown(object sender, MouseEventArgs e)
{
url.ReadOnly = false;
if (isSelected)
{
url.DeselectAll();
}
else
{
url.SelectAll();
}
isSelected = !isSelected;
}
Replace IsDeselected with something that checks whether the current state is deselected or not.
Your code always selects and then deselects again, so your text will always be deselected after mouse down.
Try this instead:
private void url_MouseDown(object sender, MouseEventArgs e)
{
url.ReadOnly = false;
if (url.SelectedText.Length < url.Text.Length) {
url.SelectAll();
} else {
url.DeselectAll();
}
}
I want to lock my wpf tab to change the index but I'm getting dispatcher error messages with my code below. Where am I doing wrong? I'm aware that once the content changes, it fires the same event but is there any other event to fire for this ?
private void MainTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (!(e.Source is TabControl))
return;
if (Helper.GetProperty<bool>("IsTabLocked")) // my condition
{
MessageBox.Show("tab is locked");
e.Handled = true;
return;
}
The easiest solution that I can see is setting the required tab as the selected one when the execution comes to SelectionChanged event.
Try something like below.
int MyPreferedTabPageIndex = 1; // ?
private void MainTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (Helper.GetProperty<bool>("IsTabLocked")) // my condition
{
MainTabControl.SelectedIndex = MyPreferedTabPageIndex ;
MessageBox.Show("tab is locked");
}
}
I could come up with a custom solution with the source code below, but I'm sure someone has thought about this and there is an event or an easier trick which I've never heard before.
static int TabControlIndex = 0;
private void MainTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (!(e.Source is TabControl))
return;
if (TabControlIndex == MainTabControl.SelectedIndex)
return;
if (Helper.GetProperty<bool>("IsTabLocked") && TabControlIndex != MainTabControl.SelectedIndex)
{
MessageBox.Show("locked");
MainTabControl.SelectedIndex = TabControlIndex;
// = true;
return;
}