If one checkbox is checked, set the other to unchecked - c#

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;
}
}
}

Related

How to disable some cells in DataGrid?

I found lot of answers on Stack Overflow how to disable specific cell in DataGrid in Windows Forms or WPF. Now I want to ask same question in DevExpress. Thank you for your answers!
My current somehow working code prevent user to check specific checkbox in grid but this checkbox dosen't look like it is disabled. How can I visually disable this field making it gray or none visible at all?
bool expression = ... // some expresssion
private void grid_ShownEditor(object sender, EventArgs e)
{
GridView view sender as GridView;
if(view.FocusedColumn.FieldName == "specific column name with checkbox cells")
{
var row = view.GetRow(view.FocusedRowHandle);
view.ActiveEditor.Enabled = expression;
}
}
Use GridView.ShowingEditor and GridView.CustomDrawCell to do what you're after. See:
private bool isDisabled = false;
private bool IsDisabled(int row, GridColumn col)
{
if (col.FieldName == "somename")
return isDisabled;
return false;
}
private void GridView_ShowingEditor(object sender, CancelEventArgs e)
{
var gv = sender as GridView;
e.Cancel = IsDisabled(gv.FocusedRowHandle, gv.FocusedColumn);
}
private void GridView_CustomDrawCell(object sender, RowCellCustomDrawEventArgs e)
{
if(IsDisabled(e.RowHandle, e.Column))
{
e.Appearance.BackColor = Color.Gray;
e.Appearance.Options.UseBackColor = true;
}
}
If you would like to not show a checkbox at all, you can do this:
private static RepositoryItemTextEdit _nullEdit;
public static RepositoryItemTextEdit NullEdit
{
get
{
if (_nullEdit == null)
{
_nullEdit = new RepositoryItemTextEdit();
_nullEdit.ReadOnly = true;
_nullEdit.AllowFocused = false;
_nullEdit.CustomDisplayText += (sender, args) => args.DisplayText = "";
}
return _nullEdit;
}
}
private void GridView_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e)
{
if(IsDisabled(e.RowHandle,e.Column))
{
e.RepositoryItem = NullEdit;
}
}

Detect two buttons clicked for touchscreen

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;
}
}

Enable button if textbox populated

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

Get checkBox unchecked state

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;
}
}

Get Mouse State without access to MouseEventArgs?

I have a form with many, many controls. I need to detect if the mouse is down or if it's up. Most of the time, I don't have MouseEventArgs.
Is there a quick and easy way to tell if the mouse is down without mouseEventArgs?
Is there an alternative, or is something like this the only way?:
foreach (Control c in this.Controls)
{
c.MouseUp += new MouseEventHandler(globalMouseUp);
c.MouseDown += new MouseEventHandler(globalMouseDown);
}
bool isMouseUp = true;
private void globalMouseDown(object sender, MouseEventArgs e)
{
isMouseUp = false;
}
private void globalMouseUp(object sender, MouseEventArgs e)
{
isMouseUp = true;
}
You can try checking with a timer:
private void timer1_Tick(object sender, EventArgs e) {
this.Text = "Mouse Is " + (Control.MouseButtons == MouseButtons.Left);
}
ChecK Control.MouseButtons static property:
if (Control.MouseButtons == MouseButtons.Left)
{
}

Categories