disable dropdownlist items - c#

I want to disable every fifth item in a dropdownlist.
dropdownlist1.items[i].Attributes.Add("disabled","disabled");
How do I write the logic to disable every fifth item in a Dropdownlist?
I am using two for loops: one for displaying items and one for disabling items in dropdownlist. How can I simplify my code?

Perhaps you should consider just not showing them?
As in:
if (i % 5 > 0) {
dropdownlist1.items[i].Attributes.Add("disabled","disabled");
}

foreach(Control c in this.Controls)
{
if(c is dropdownlist)
{
dropdownlist dl = (dropdownlist)c;
if (i % 5 > 0)
{
dl.items[i].Attributes.Add("disabled","disabled");
}
}
}
Check this out! same as castirng.
findout all dropdownlist on the form and
on every dropdown list it will disable.
let me know!!

If what you need is like an optgroup functionality to group your options. Here is a sample of what an optgroup looks like in case you don't know http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_optgroup.
There are ways to add an optgroup to a dropdown list http://weblogs.asp.net/jeff/archive/2006/12/27/dropdownlist-with-optgroup.aspx
then you could use it like this
ListItem item = new ListItem("some group name", String.Empty);
item.Attributes["optgroup"] = "optgroup";
myDropDown.Items.Add(item);
or in your case
dropdownlist1.items[i].Attributes["optgroup"] = "optgroup";

Using jQuery
var count = 0;
$('select option').each(function() {
count++;
if(count % 5 == 0)
$(this).attr('disabled', 'disabled');
}):

I googled for "disable dropdownlist items" and the first result provided the exact answers you're looking for.
It's not possible, but there are alternatives.

Related

How can I iterate through selected ComboBoxes controls with a loop in C#?

I am working in a simple C# form. I have a total of 20 ComboBoxes . 10 ComboBoxes will contain similar data type and will have very similar name (CB1, CB2, CB3, ... CB10). Each ComboBox was loaded with a list of 5 elements A,B,C,D,E (by this I meant that I added those values to each of the 10 "CB" ComboBoxes). My intend is that the user can have the ability to select items (one out of A,B,C,D,E) from either 1 combobox, or 2 comboboxes, . . . ., or all of them (10 comboboxes).
I wish to store the data from the ComboBoxes where an item was selected in a list or array. For that I would like to iterate through the 10 ComboBoxes (the comboboxes which names are CB1, CB2, CB3, ..., CB10), check if the an item was selected in the combobox, and store the value selected in the ComboBox into a list (in the code below the list is called symbols). At the end the length of my symbols list (number of items) will depend on how many ComboBoxes the user selected from. Here is the code I am using:
List<string> symbols = new List<string>();
for (int i = 1; i <= 10; i++)
{
var my_comboBox = this.Controls["CB" + i.ToString()];
if (null != my_comboBox.SelectedItem)
{ symbols.add(my_comboBox.Text); }
}
when I run the code I get the following error.
Object reference not set to an instance of an object.
Could anyone please explain what I am doing wrong? I got the code from another question that was posted and answered, below is the link to that question. Thanks in advance.
How can I iterate all ComboBoxes controls with a loop in C#?
I also tried the other alternative proposed on the answers to the questions cited. But it did not work. It does not go through the foreach loop (no error is shown though). Here is the code for option 2.
List<string> symbols = new List<string>();
var comboBoxes = this.Controls.OfType<ComboBox>().Where(x => x.Name.StartsWith("CB"));
foreach (var cmbBox in comboBoxes)
{
if (null != my_comboBox.SelectedItem)
{ symbols.add(my_comboBox.Text); }
}
Again if anyone can please provide me with ideas to what I am doing wrong that would be very nice. Thanks in advance once more.
The ComboBoxes belong to the form as shown in the picture below. In there the ComboBoxes are called component_CB1, component_CB2, component_CB3, ..., component_CB10 (I changed the name of the ComboBoxes in the question to CB to make it easier to understand).
screenshot of my solution explorer
Thanks to everyone who contributed to find the answer to this problem. Please read the comments to identify the contributors.
The answer is that you can iterate through selected ComboBoxes in C#. However for this to work you need to make sure you know to what container control your ComboBoxes are added.
To know to container control your ComboBoxes are added to, go to View → Other Windows → Document Outline. You can see if those controls are directly children of the form or they are children of another container control.
If the ComboBoxes are added to your form directly, then here there are two alternatives to iterate through the ComboBoxes:
ALTERNATIVE 1: (ComboBoxes added to the form directly)
List<string> symbols = new List<string>();
for (int i = 1; i <= 10; i++)
{ //CB is the begining of the name of the comboboxes CB1, CB2, ... CB10
var my_comboBox = this.Controls["CB" + i.ToString()];
if (null != my_comboBox.SelectedItem)
{ symbols.add(my_comboBox.Text); }
}
ALTERNATIVE 2: (ComboBoxes added to the form directly)
List<string> symbols = new List<string>();
//CB is the begining of the name of the comboboxes CB1, CB2, ... CB10
var comboBoxes = this.Controls.OfType<ComboBox>().Where(x=>x.Name.StartsWith("CB"));
foreach (var cmbBox in comboBoxes)
{
if (null != my_comboBox.SelectedItem)
{ symbols.add(my_comboBox.Text); }
}
If the ComboBoxes are added to a different control container (e.g. a tab in a TabControl as in my initial case), you must specify the control container name rather than "this". Here are two alternatives taking as example the ComboBoxes Cb1, CB2, CB3, ..., CB10 that are added to a tab called tab1.
ALTERNATIVE 1: (ComboBoxes added to a tab of a TabControl)
List<string> symbols = new List<string>();
for (int i = 1; i <= 10; i++)
{//CB is the begining of the name of the comboboxes CB1, CB2, ... CB10
var my_comboBox = tab1.Controls["CB" + i.ToString()];
if (null != my_comboBox.SelectedItem)
{ symbols.add(my_comboBox.Text); }
}
ALTERNATIVE 2: (ComboBoxes added to a tab of a TabControl)
List<string> symbols = new List<string>();
//CB is the begining of the name of the comboboxes CB1, CB2, ... CB10
var comboBoxes = tab1.Controls.OfType<ComboBox>().Where(x
=>x.Name.StartsWith("CB"));
foreach (var cmbBox in comboBoxes)
{
if (null != my_comboBox.SelectedItem)
{ symbols.add(my_comboBox.Text); }
}
Thanks for the help again. I hope this can be useful for others.

Count number of checkbox checked

I have multiple individual checkbox's (50).
like this:
<asp:CheckBox runat="server" ID="chkBirthDate" />
I need to know how many checkbox the user has selected (Count). If he has selected more than 3 i let him pass if not i present him an error message.
Thanks in advance!
LINQ Approach
You could take advantage of the querying capability of LINQ by using the OfType<T>() method to grab all of your individual CheckBox Controls and then use a Count() call to see how many were actually checked :
// Get the number of CheckBox Controls that are checked
var checkedBoxes = Form.Controls.OfType<CheckBox>().Count(c => c.Checked);
// Determine if your specific criteria is met
if(checkedBoxes > 3)
{
// You shall pass!
}
else
{
// None shall pass
}
You'll need to ensure that you have a reference to LINQ for this to work as well by including the following using statement :
using System.Linq;
Iterative Looping Approach
Alternatively, you could simply loop through and increment a count accordingly via a foreach loop as seen below :
// Store your count
var checkedBoxes = 0;
// Iterate through all of the Controls in your Form
foreach(Control c in Form.Controls)
{
// If one of the Controls is a CheckBox and it is checked, then
// increment your count
if(c is CheckBox && (c as CheckBox).Checked)
{
checkedBoxes++;
}
}
Example (with output)
You can find a GitHub Gist that fully reproduces this here and demonstrated below :

Removing an item from a list c#

I am having trouble with removing an item from a list in c#. My current code is below:
for (int i = current.Count - 1; i >= 0; i--)
{
foreach (ListItem li in sp_list.Items)
{
if (li.Text == current[i].uname)
{
current.RemoveAt(i);
}
}
}
The aim of the code is to compare the existing items in a list box with newly added items so I know which items have just been added. So currently I am getting the current list box items from the database (they are stored here, it is a databound list box), entering these into a list and for each of the list items, comparing them with the items in a list box and if they match, remove the item from the list.
Therefore, in the end, if I add two new entries, the list should only be storing the two newly added values.
The code does not work as the first item is removed fine, but then, the value of i is greater than current.count - and therefore I get an out of range exception!
Could someone help me with this issue? Apologies about the confusing question, it's hard to explain! Thanks
You can do it with Linq. Not sure if casting to ListItem needed (you can remove it)
current.RemoveAll(x => sp_list.Items.Cast<ListItem>()
.Any(li => li.Text == x.uname));
Once you've found the matching value, and removed it from the list, you want to break out of the inner loop to check the next item.
if (li.Text == current[i].uname)
{
current.RemoveAt(i);
break;
}
Your nesting is wrong, I think you wanted,
foreach (ListItem li in sp_list.Items)
{
for (int i = current.Count - 1; i >= 0; i--)
{
if (li.Text == current[i].uname)
{
current.RemoveAt(i);
}
}
}
alternatively, use linq,
// For lookup performance.
var items = new HashSet(sp_list.Items.Select(i => i.text));
current = current.Where(c => !items.Contains(c.uname)).ToList();
How about this:
foreach (ListItem li in sp_list.Items) {
if (current.Contains(li.Text)) {
current.Remove(li.Text);
}
}
Put a break statement after the RemoveAt so you don't remove that item again.
you can travel the list in reverse order and remove items using RemoveAt(i).
also for efficiency purposes you may want to put the ListItem texts in a Set so you can don't have to loop though the sp_list.Items for each of your current items.

Check all item in listview with huge list item?

I want to check about 3000 item in listview. This is a bit of code :
foreach (ListViewItem item in this.lvItem.Items)
{
item.Checked = !item.Checked;
}
But listview is very slow when item is checked. Please give me some ideas to solve this problem? Thanks.
I had the same problem but I found why.
I had an "ItemChecked" event handler attached to my listView that was doing some heavy stuff.
I removed the eventHandler and it solved my problem.
Try removing any "ItemChecked" eventhandler and see if the speed is better.
You need to call BeginUpdate before the loop and EndUpdate after the loop:
listView1.BeginUpdate();
foreach (ListViewItem item in listView1.Items)
item.Checked = true;
listView1.EndUpdate();
Calling BeginUpdate prevents the control from drawing until the EndUpdate method is called.
I heard a rumor that for large list items a for loop will work faster than a foreach loop
try
for(int i = 0; i = < this.1vItem.Items.Count; i++)
{
//Stuff
}
I also don't think it's wise to expect a user to click 3000 items. But something I did recently, when adding the items, knowing that there would never be many and by default they should be checked, is check the items before adding them to the list.
Something like this:
foreach (Recipient recipient in recipients)
{
var item = new ListViewItem(recipient.FirstName + " " + recipient.LastName);
item.Tag = recipient;
item.Checked = true;
lvRicipients.Items.Add(item);
}
Will something like this work for you? ...when checked, add the items to a Dictionary ...when unchecked, remove from the Dictionary. Not tested code but wondering if you could do something like this:
Dictionary<String, ListViewItem> Dic = listView.Items
.Cast<ListViewItem>()
.ToDictionary(x => x.Text, x => x.SubItems[0].Checked);
You asked how to better go about it. What I am saying is on your Check Event you will want to add items to your list view. I doubt that a user will actually check all 3000, so change your code to decide how you would want to handle checked items, the example that I have given you uses Lambda expression. If not familiar, then please alter your question to reflect what it is that you actually need and/or want...
for (int i = 0; i <= listView1.Items.Count - 1; i++)
{
if (!listView1.Items[i].Checked)
listView1.Items[i].Checked = true;
}

listbox; number of selected items

While using listbox in c#, how can learn the count of selecteditems?
Listbox items: A,B,C,D. For example, I select C and D.
I want to make a loop in order to assign selecteditems.
How can I achieve it? How can I learn the number of selected item?
Thank you
Maybe you are looking for this listbox1.GetSelectedIndices().Count();
Use the following code:
This return integer:
listBox.SelectedItems.Count
this will return the number as string :
listBox.SelectedItems.Count.ToString()
You ought to be able to achieve this using something like so:
var count = (from item in listBox.Items where item.Selected select item).Count();
The above is a way to get this using Linq (so you will need a reference to System.Linq) but could easily be expanded to use a more primitive means such as a loop.
int count = 0;
foreach(ListItem item in this.ListBox1.Items)
{
if(item.Selected)
{
count++;
}
}
int c = count;

Categories

Resources