I have a checkbox that when a checked checks all items in a CheckedListBox.
When the checkbox goes unchecked it should uncheck all items in the list.
Code:
if (checkBoxCheckAllPrivileges.Checked)
for (int i = 0; i < checkedListBoxUsersWhoSee.Items.Count; i++)
checkedListBoxUsersWhoSee.SetItemChecked(i, true);
else
for (int i = 0; i < listBoxUsers.Items.Count; i++)
checkedListBoxUsersWhoSee.SetItemChecked(i, false);
Is the problem in this code?
Does the .SetitemChecked work giving it the parameter as false?
Is there any other way to uncheck the items?
You have given wrong item in else part for loop,
if (checkBoxCheckAllPrivileges.Checked)
for (int i = 0; i < checkedListBoxUsersWhoSee.Items.Count; i++)
checkedListBoxUsersWhoSee.SetItemChecked(i, true);
else
for (int i = 0; i < checkedListBoxUsersWhoSee.Items.Count; i++)
checkedListBoxUsersWhoSee.SetItemChecked(i, false);
Use JavaScript Too
$(document).ready(function () {
$("#<%=checkBoxCheckAllPrivileges.ClientID%>").click(function () {
if ($(this).is(":checked"))
{
$("#<%=checkedListBoxUsersWhoSee.ClientID%> input[type=checkbox]").prop("checked", true);
}
else
{
$("#<%=checkedListBoxUsersWhoSee.ClientID%> input[type=checkbox]").prop("checked", false);
}
});
Related
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)
I am very new programmer, the user is able to input vales into a ListBox, there is also a option button where the user can sort the numbers in ascending order. However I am also asked to create a option button to unsort back into its original form, however I am not sure how I would do this. I am trying to do this without the use of any containers/arrays. here is my code to sort:
private void sorted()
{
int a = lstHoldValue.Items.Count;
for (int i = 0; i < a - 1; i++)
{
var k = 0;
for (var j = 1; j < a - i; j++)
{
if (Convert.ToInt32(lstHoldValue.Items[j]) < Convert.ToInt32(lstHoldValue.Items[k]))
{
var temp = lstHoldValue.Items[j];
lstHoldValue.Items[j] = lstHoldValue.Items[k];
lstHoldValue.Items[k] = temp;
k = j;
}
else
{
k++;
}
}
}
}
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.
I have manually added values in dropdown list using for loop.
for (int i = 1; i <= 31; i++)
{
date0.Items.Add(i.ToString());
date1.Items.Add(i.ToString());
date2.Items.Add(i.ToString());
date3.Items.Add(i.ToString());
}
for (int j = 1; j <= 12; j++)
{
month0.Items.Add(j.ToString());
month1.Items.Add(j.ToString());
month2.Items.Add(j.ToString());
month3.Items.Add(j.ToString());
}
for (int k = DateTime.Now.Year; k <= 2020; k++)
{
yyyy0.Items.Add(k.ToString());
yyyy1.Items.Add(k.ToString());
yyyy2.Items.Add(k.ToString());
yyyy3.Items.Add(k.ToString());
}
Now on clear button I want to clear out these values and set them to initial. I tried calling the function in which I have initialized them. But it's not happening. Can someone tell me why?
You will need to fill the dropdownlist only if the page is Not postback
if (! IsPostBack) {
//Fill out the dropdown list
}
and make sure to set SelectedIndex = 0 for those you want to set back to initial values
month0.SelectedIndex = 0;
You can reset it to the first record by setting the SelectedIndex to 0 so that the first element in the list will be selected:
date0.SelectedIndex = 0;
date1.SelectedIndex = 0;
date2.SelectedIndex = 0;
date3.SelectedIndex = 0;
month0.SelectedIndex = 0;
month1.SelectedIndex = 0;
month2.SelectedIndex = 0;
month3.SelectedIndex = 0;
yyyy0.SelectedIndex = 0;
yyyy1.SelectedIndex = 0;
yyyy2.SelectedIndex = 0;
yyyy3.SelectedIndex = 0;
You can put this on the Clear button.
I need to loop through a checked listbox, and for each of the items in it, I need to check them (basically like a "select all" function).
Is there a basic example you could give me to help me out please?
Use SetSelected and interate through all the Items
// Loop through and set all to selected.
for (int x = 0; x < listBox1.Items.Count; x++)
{
listBox1.SetSelected(x, true);
}
To check the items, use SetItemChecked
// Loop through and set all to checked.
for (int x = 0; x < listBox1.Items.Count; x++)
{
listBox1.SetItemChecked(x, true);
}
You can look through all the items as ListItems:
foreach (ListItem li in CheckBoxList1.Items)
{
li.Selected = true;
}