Select and unselect checkboxes - c#

i have a list of Checkbox's .
checkbox1,checkbox2....
i want to uncheck the checkbox if its is checked and vice versa.Is there any way to do this.

foreach (CheckBox cb in cbList) {
cb.Checked = !cb.Checked;
}

if (CheckBox1.Checked == true)
{
CheckBox1.Checked = false
}
else
{
CheckBox1.Checked = true
}

I don't know if you're working in ASP.NET, WPF, WinForms, ...
But it's as easy as check if checkbox is checked, then uncheck, and viceversa.
Or you can implement an extension method like:
public static class CheckboxExtensions
{
public static void ToggleChecked(this CheckBox some)
{
if(some != null)
{
if(some.Checked)
{
some.Checked = false;
}
else
{
some.Checked = true;
}
}
}
}
... And your actual code will look like this:
chkSome.ToggleChecked();

Try
checkBox1.Checked = !checkBox1.Checked;

Related

session check for disable & enable checkbox C#

I'm beginner & I need help
I try this code for check the id for employee to enable or disable the checkbox depend on the id for employee but the checkbox was disabled even when I login with the same Id in the session
if (Session["employeeNo"] == "12345")
{
CheckBoxList1.Enabled = true;
}
else
{
CheckBoxList1.Enabled = false;
}
If u r using CheckBox instead of Checkboxlist ...Try this Code It will Work
if (Session["employeeNo"].ToString() == "12345")
{
CheckBox1.Checked = true; // for Check
CheckBox1.Enabled = true; // for Enable
}
else
{
CheckBox1.Checked = false; // for UnCheck
CheckBox1.Enabled = false; // for Disable
}
The Session["employeeNo"] gives you an object, if you compare this with a string value the result will be false always. You have to convert this to string in order to compare with a string value, change the condition like this:
if (Session["employeeNo"].ToString() == "12345")

How to check if a GridView contains a certain Button asp.net c#

hi i would like to know how can i check if any of the reviewBtn is visible in the gridview and if ANY reviewBtn is present, btn_reviewAll will be visible.
currently the code below only shows the btn_reviewAll when ALL reviewBtn is visible. pls advise thanks!
foreach (GridViewRow row in GridViewReview.Rows)
{
Control reviewBtn = row.FindControl("ButtonReview") as Button;
if (reviewBtn.Visible == true)
{
btn_reviewAll.Visible = true;
}
else
{
btn_reviewAll.Visible = false;
}
}
change your code like this
foreach (GridViewRow row in GridViewReview.Rows)
{
Control reviewBtn = row.FindControl("ButtonReview") as Button;
if (reviewBtn.Visible == true)
{
btn_reviewAll.Visible = true;
break;
}
else
{
btn_reviewAll.Visible = false;
}
}
what it does is when one reviewBtn is visible it will set btn_reviewAll to visible and break out the foreach loop
#Shreesha's answer is absolutely correct, you can also do with less code using LINQ like this:-
if (GridViewReview.Rows.OfType<GridViewRow>()
.Any(b => ((Button)b.FindControl("ButtonReview")).Visible))
btn_reviewAll.Visible = true;
else
btn_reviewAll.Visible = false;

how to disable other check box if one or more check box is checked

I am doing a c# windows form application. Right now, I have a list of checkbox which I put it in an array. I have a loop to loop through the list of checkbox to do stuff. I am wondering is there a way to disable other checkbox that isn't checked?. Here is my code, help will be appreciated. Thanks. For example, let say checkbox 1 and 6 is checked, then checkbox 2,3,4,5 will be disabled.
Code:
CheckBox[] myCheckBoxArray = new CheckBox[6];
myCheckBoxArray [0] = checkBox1;
myCheckBoxArray [1] = checkBox2;
myCheckBoxArray [2] = checkBox3;
myCheckBoxArray [3] = checkBox4;
myCheckBoxArray [4] = checkBox5;
myCheckBoxArray [5] = checkBox6;
foreach (CheckBox checkBox in myCheckBoxArray )
{
if (checkBox .Text == className && comboBox1.SelectedIndex == index)
{
checkBox .Checked = true;
}
}
This code will do:
public void DisableAllUnchecked(IEnumerable<CheckBox> items)
{
items.ForEach(m=> m.Enabled = !m.Checked);
}
Sample Code:
DisableAllUnckeched(myCheckBoxArray);
Or if you don't want to create a method you can do it in a single line:
myCheckBoxArray.ToList().ForEach(m=> m.Enabled = !m.Checked);
You can modify foreach loop as below:
foreach(CheckBox checkBox in myCheckBoxArray)
{
if (checkBox.Text == className && comboBox1.SelectedIndex == index)
{
checkBox.Checked = true;
}
else
{
checkBox.Checked = false;
checkBox.Enabled = false;
}
}
//put an else after your if
foreach (CheckBox checkBox in myCheckBoxArray )
{
if (checkBox .Text == className && comboBox1.SelectedIndex == index) {
checkBox .Checked = true;
}
else
{
checkBox .Enabled= false;
}
}
this would do the trick
foreach(CheckBox checkBox in myCheckBoxArray)
{
if(checkBox.IsChecked==false)
{
checkBox.IsEnabled = false;
}
}

How to eliminate the usage of a nullable bool

I came up with this code, which I thought was rather clever (the requirement is that if the selected date is in the past, the TextBoxes should be readonly, otherwise (today's date or a future date) they should be editable):
bool? setReadOnly = null;
if (SelectedDateIsInThePast() && (!currentlyReadOnly)) {
setReadOnly = true;
} else if (!SelectedDateIsInThePast() && (currentlyReadOnly)) {
setReadOnly = false;
}
if (setReadOnlyToTrue.HasValue) {
foreach (Control ctrl in tableLayoutPanelPlatypus.Controls) {
if (ctrl is TextBox) {
tb = (TextBox)ctrl;
tb.ReadOnly = setReadOnlyToTrue.Value;
}
}
}
...but find that nullable bools are "data type non grata" among my compadres.
Is there a non-complicated way to do the same thing (only loop through the controls if the readonly value needs to be changed?). Of course, I could simply set them regardless of whether they needed to be set this way:
if (SelectedDateIsInThePast()) {
setReadOnly = true;
} else {
setReadOnly = false;
}
foreach (Control ctrl in tableLayoutPanelPlatypus.Controls) {
if (ctrl is TextBox) {
tb = (TextBox)ctrl;
tb.ReadOnly = setReadOnly;
}
}
...but I don't like to perform moot operations, if it's reasonably possible to avoid them.
Factor the loop into a method, and only call the method in the cases you set setReadOnly:
if (SelectedDateIsInThePast() && (!currentlyReadOnly)) {
SetReadOnly(true);
} else if (!SelectedDateIsInThePast() && (currentlyReadOnly)) {
SetReadOnly(false);
}
You can use |=, &=, and two non-nullable booleans to implement the same requirement:
bool forceReadOnly = SelectedDateIsInThePast() && (!currentlyReadOnly);
bool clearReadOnly = !(!SelectedDateIsInThePast() && (currentlyReadOnly));
foreach (Control ctrl in tableLayoutPanelPlatypus.Controls) {
if (ctrl is TextBox) {
tb = (TextBox)ctrl;
tb.ReadOnly |= forceReadOnly;
tb.ReadOnly &= clearReadOnly;
}
}
I think a nullable bool is fine... but another way would be:
public enum ControlState
{
Unknown = 0,
DateInPast,
DateInFuture
}
....
var state = ControlState.Unknown;
if (SelectedDateIsInThePast() && (!currentlyReadOnly)) {
state = ControlState.DateInPast;
} else if (!SelectedDateIsInThePast() && (currentlyReadOnly)) {
state = ControlState.DateInFuture;
}
if (state != ControlState.Unknown) {
foreach (Control ctrl in tableLayoutPanelPlatypus.Controls) {
if (ctrl is TextBox) {
tb = (TextBox)ctrl;
tb.ReadOnly = setReadOnlyToTrue.Value;
}
}
}
Use an enum with three separate, meaningful states ?
enum ShouldSetState
{
No,
SetReadOnly,
SetReadable
}
Then do
ShouldSetState setState = ShouldSetState.No;
if (SelectedDateIsInThePast() && (!currentlyReadOnly)) {
setState = ShouldSetState.SetReadOnly;
} else if (!SelectedDateIsInThePast() && (currentlyReadOnly)) {
setState = ShouldSetState.SetReadable;
}
if (setState != ShouldSetState.No) {
foreach (Control ctrl in tableLayoutPanelPlatypus.Controls) {
if (ctrl is TextBox) {
tb = (TextBox)ctrl;
tb.ReadOnly = setState == ShouldSetState.SetReadOnly;
}
}
}
Your code could be made more consise without using a nullable bool:
bool inThePast = SelectedDateIsInThePast();
if (currentlyReadOnly != inThePast )
{
currentlyReadOnly = inThePast;
foreach(var tb in tableLayoutPanelPlatypus.Controls.OfType<TextBox>())
tb.ReadOnly = currentlyReadOnly;
}
Also, if you have to do a lot of these types of UI manipulations, you might consider data binding.

Check Control.Value for data

I have several different controls (TextBoxes, DateTimePickers, MaskedTextBoxes) on a form that I would like to check to see if they contain any data. I have the following code in the Click event of my "Save" button:
private void radBtnSave_Click(object sender, EventArgs e)
{
this.Cancelled = false;
bool bValid = true;
foreach(Control control in this.Controls)
{
if (control.Tag == "Required")
{
if (control.Text == "" || control.Text == null)
{
errorProvider.SetError(control, "* Required Field");
bValid = false;
}
else
{
errorProvider.SetError(control, "");
}
}
}
if (bValid == true)
{
bool bSaved = A133.SaveData();
if (bSaved != true)
{
MessageBox.Show("Error saving record");
}
else
{
MessageBox.Show("Data saved successfully!");
}
}
}
This works fine for the TextBoxes and MaskedEditBoxes, however, it does not work for the DateTimePickers. For those, I know I need to check the .Value property, but I cannot seem to access that from the Control object (i.e. "control.Value == "" || control.Value == null").
Am I missing something obvious? Any suggestions of modifications I can make to this code to allow me to check the DateTimePicker values (or just to improve the code at all) will be greatly appreciated.
You need to cast them to a DateTimePicker:
DateTimePicker dtp = control as DateTimePicker;
if(dtp !=null)
{
//here you can access dtp.Value;
}
Also, use String.IsNullOrEmpty(control.Text) in the first part of your code.
There is no Value property for Controls; DateTimePicker, for example, creates its own property that is unique to it.
Unfortunately for you, there is no fully generic way of handling this from a single loop of Control objects. The best you can do is something along the lines of this:
if(control is DateTimePicker)
{
var picker = control as DateTimePicker;
// handle DateTimePicker specific validation.
}
You'll need to do something like this:
foreach(Control control in this.Controls)
{
if (control.Tag == "Required")
{
DateTimePicker dtp = control as DateTimePicker;
if (dtp != null)
{
// use dtp properties.
}
else if (control.Text == "" || control.Text == null)
{
errorProvider.SetError(control, "* Required Field");
bValid = false;
}
else
{
errorProvider.SetError(control, "");
}
}
}

Categories