For loop on multiple check boxes - c#

I have checkboxes named CheckBox1,CheckBox3,CheckBox5,CheckBox7,CheckBox9,CheckBox11. I wants to iterate through these check boxes and wants to disable them. I have written like this
for (int i=1; i < 12; i++)
{
((CheckBox)(i.ToString())).Enabled = false;
i=i+2;
}
but this is not the proper way. please help

Try using FindControl like this if you have id of controls like Checkbox1,Checkbox3 etc.
for (int i = 1; i < 12; i+=2)
{
CheckBox cb = (CheckBox)Page.FindControl("Checkbox" + i);
cb.Enabled = false;
}

Your code would like to set the enabled=false for checkboxes that have an odd number at the end of their id? Then this will do that
foreach (Control ctrl in Page.Controls)
{
if (ctrl is CheckBox && ctrl.id.length > 9)
{
int chkboxNumber = int.Parse(ctrl.id.SubStr(9));
if ( chkboxNumber % 2 == 1) // Check for odd numbers
{
(CheckBox)ctrl.Enabled = false;
}
}
}

May this help you.
foreach (Control c in Page.Controls)
{
if (c is CheckBox)
{
CheckBox myCheckBox = (CheckBox)c;
myCheckBox.Enabled = false;
}
}
This will disable you all CheckBoxes you have.
Edit: Sorry didnt see the +2 at the i. My fault.

Related

How to transfer selected rows from one datagridview to another?

I try to transfer selected rows from one datagridview to another. I used combobox textchange event to load selected data to datagrid. So that transfer code need to do dataGridView1.AllowUserToAddRows = false; for both dataGridViews. But due to use combobox textchange event I can't disable adding new rows. How to solve this? I use following code to transfer values:
private void btnaddtoanother_Click(object sender, EventArgs e)
{
for (int i = 0; i <= dataGridView2.Rows.Count - 1; i++)
{
bool rowAlreadyExist = false;
bool checkedCell = (bool)dataGridView2.Rows[i].Cells[0].Value;
if (checkedCell == true)
{
DataGridViewRow row = dataGridView2.Rows[i];
if (dataGridView1.Rows.Count != 0)
{
for (int j = 0; j <= dataGridView1.Rows.Count - 1; j++)
{
if (row.Cells[0].Value.ToString() == dataGridView1.Rows[j].Cells[0].Value.ToString())
{
rowAlreadyExist = true;
break;
}
}
if (rowAlreadyExist == false)
{
dataGridView2.Rows.Add(row.Cells[0].Value.ToString());
dataGridView2.Rows.Add(row.Cells[1].Value.ToString());
dataGridView2.Rows.Add(row.Cells[3].Value.ToString());
}
}
else
{
dataGridView2.Rows.Add(row.Cells[0].Value.ToString());
dataGridView2.Rows.Add(row.Cells[1].Value.ToString());
dataGridView2.Rows.Add(row.Cells[3].Value.ToString());
}
}
}
}
Start with removing the row from the Grid before you try to add it to the new Grid.
foreach (GridViewRow row in fromGridView.SelectedRows.OfType<DataGridViewRow>().ToArray())
{
fromGridView.Rows.Remove(row);
toGridView.Rows.Add(row);
}

Accessing checkboxes with variables

I have asked this question previously but for VB.NET here:
Accessing buttons names using variables
Now I want to do the same but in C# and with CheckBoxes so for example, if I have 31 check boxes labeled "CheckBox1...CheckBox31" I could do :
for (int i = 0; i < 10; i++)
{
(CheckBox + i).Enabled = false;
}
Thanks for any suggestions.
Try following
for (int i = 0; i < 10; i++)
{
((CheckBox)this.Controls[$"CheckBox{i}"]).Enabled = false;
}
You can try creating a List<CheckBox> or an array of all checkboxes:
for (int i = 0; i < checkbox_array.Length; i++) {
checkbox_array[i].Enabled = false;
}
EDIT: I might be a bit late, but if you put all CheckBoxes in a GroupBox (wich I really recommend doing if you have soo many checkboxes), you can just loop trough all the controls in that groupbox like this:
foreach (CheckBox cbx in gbxCheckBoxes.Controls) {
cbx.Enabled = true;
}
or like this: (if you only need to enable them)
gbxCheckBoxes.Enabled = false;
(gbxCheckBoxes is the GroupBox I was talking about)

How can I loop through the first 'n' rows in a grid view.

I have a question about if there is a better more efficient way to do what I want to accomplish:
What I want to do is loop through the rows in a grid view and check the check boxes for the first 100 records.
For this I came up with the solution below:
int limit = 0;
int max = 100;
foreach (GridViewRow gvr in GridView1.Rows)
{
limit++;
if (gvr.RowType == DataControlRowType.DataRow && limit <= max)
{
CheckBox cb = (CheckBox)gvr.FindControl("chkSelect");
cb.Checked = true;
}
}
I am wondering if this is the best way of doing this or if there is an easier/quicker way to do accomplish the same task.
Thanks for any help.
Why you use a for-each loop if you dont want to check all rows? I would use a normal for loop like so:
int max = 100;
for (int i = 0; (i < GridView1.Rows.Count && i < max); i++)
{
GridViewRow gvr = GridView1.Rows[i];
if(gvr.RowType == DataControlRowType.DataRow){
CheckBox cb = (CheckBox)gvr.FindControl("chkSelect");
cb.Checked = true;
}
}
The check i < GridView1.Rows.Count is optional, but prevent you from outofbounds exceptions.

having problems with checklistbox click events

This code works but the results show up when I check and then uncheck again in the first checkbox. I want to check only once.Does anyone have an opinion? Thanks!
private void clb_grup_MouseClick(object sender, MouseEventArgs e)
{
Liste.Items.Clear();
string s = "";
foreach (var item in clb_kisiler.CheckedItems)
{
s = s + item.ToString() + " ";
}
string[] secilenler = s.Split(' ');
if (clb_grup.GetItemChecked(0) == true)
{
for (int x = 0; x < clb_kisiler.Items.Count; x++)
{
clb_kisiler.SetItemChecked(x, true);
}
for (int i = 0; i < clb_kisiler.Items.Count; i++)
{
Liste.Items.Add(clb_kisiler.Items[i].ToString());
}
}
else if (clb_grup.GetItemChecked(1) == true)
{
for (int x = 0; x < clb_idari.Items.Count; x++)
{
clb_idari.SetItemChecked(x, true);
}
for (int i = 0; i < clb_idari.Items.Count; i++)
{
Liste.Items.Add(clb_idari.Items[i].ToString());
}
}
else if (clb_grup.GetItemChecked(2) == true)
{
for (int x = 0; x < clb_teknik.Items.Count; x++)
{
clb_teknik.SetItemChecked(x, true);
}
for (int i = 0; i < clb_teknik.Items.Count; i++)
{
Liste.Items.Add(clb_teknik.Items[i].ToString());
}
}
foreach (object i in clb_kisiler.CheckedItems)
{
Liste.Items.Add(i.ToString());
}
}
NEW CODE
My friend used string arrays instead of the combo boxes.She is also having the same problem as me,and she also tried to write a code that would prevent dublicate items being showed up in the list box.She has one checked list box the same as me,her second checked list box is empty,and one list box.Heres the code.
private void chklstbx_bolum_ItemCheck(object sender, ItemCheckEventArgs e)
{
//event for the first check list box
string[] tumu = { "Jane", "Tammy", "John", "Emily", "Susan", "Julie", "Amelia", "Katherine" };
string[] idari = { "Julie", "Amelia", "Katherine" };
string[] teknik = { "Jane", "Tammy", "John", "Emily", "Susan" };
if (chklstbx_bolum.GetItemChecked(0) == true)
{
//if the first box in the first check list box is checked then do this
//this part of the code works fine but it doesnt check if there are //duplicates of the same name in the list box
chklstbx_sonuc.Items.Clear();
for (int i = 0; i < 5;i++ )
{
chklstbx_sonuc.Items.Add(teknik[i]);
chklstbx_sonuc.SetItemChecked(i, true);
lstbx_sonuc.Items.Add(teknik[i]);
}
}
else if (chklstbx_bolum.GetItemChecked(1) == true){
//do this if the second box in the first check box list is checked
//Here the program checks to see if there are duplicates when adding from the second check list box but the program just freezes.
chklstbx_sonuc.Items.Clear();
int x=0;
do
{
for (int i = 0; i < 3; i++)
{
chklstbx_sonuc.Items.Add(idari[i]);
chklstbx_sonuc.SetItemChecked(i, true);
lstbx_sonuc.Items.Add(idari[i]);
}
} while ( x== 0);
x++;
for (int t = 0; t < lstbx_sonuc.Items.Count; t++)
{
for (int s = 0; s < chklstbx_sonuc.Items.Count; s++)
{
if (chklstbx_sonuc.Items[s].ToString() != lstbx_sonuc.Items[t].ToString())
lstbx_sonuc.Items.Add(chklstbx_sonuc.Items[s]);
}
}
}
}
So now the second question is why does the second else if statement create a problem and makes the program not respond? And we still want to know how are we able to transfer the items in the string array without having to check and uncheck but directly transfer when its checked?
The click event is not the right one. you should use the check event and then verify in the event arguments if the checkbox has been checked or unchecked.
EDIT
I've read your other problem. There is an easier solution than manually checking which has been checked.
You need to assign a value to each checkbox with the ID of the list you want to transfer. You can then check in your code which value has been checked (likely through the sender) and use FindControl(checkboxvalue) to find the list that you want to transfer. Then you first transfer all the items, and then you check them. As I mentioned above, you also need to verify in your checkedeventargs (or whatever the eventargs parameter of the checked event is) if the checkbox is currently checked or unchecked.

Use a variable to reference a web server control in C#

In C#, I want to assign all 10 Checklistboxes on my page with a value (e.g. 1). What is the syntax so that I can use a variable? I.e. checklistbox(i).selectedIndex = 1;
for (int i = 1; i < 11; i++)
{
checklistbox1.selectedIndex = 1;
checklistbox2.selectedIndex = 1;
checklistbox3.selectedIndex = 1;
...
checklistbox10.selectedIndex = 1;
}
I guess you should use "FindControl" method inorder to do that as shown below.
for (int i = 1; i <= 10; i++)
{
(Page.FindControl("checklistbox" + i) as CheckBox).SelectedIndex = 1;
}
"checklistbox" is assumed as "ID" that is prefixed for all the checkboxes.
Hope this helps!!
You can loop over all the controls on the page and pick out the ones you need as described in this blog post by Kris Steele:
foreach (Control masterControl in Page.Controls)
{
if (masterControl is MasterPage)
{
foreach (Control formControl in masterControl.Controls)
{
if (formControl is System.Web.UI.HtmlControls.HtmlForm)
{
foreach (Control contentControl in formControl.Controls)
{
if (contentControl is ContentPlaceHolder)
{
foreach (Control childControl in contentControl.Controls)
{
if(childControl is CheckBoxList)
{
((CheckBoxList)childControl).SelectedIndex = 1;
}
}
}
}
}
}
}
}
This may not be a good idea if you have a lot of controls on the page.
You should create a List<CheckBoxList>:
var cbs = new List<CheckBoxList> { thingy, otherThingy, ... };
foreach (var cb in cbs)
cb.SelectedIndex = 0;

Categories