List for both checkedlist box and combobox - c#

I want to add all selected items of checkedlistbox and combobox to a list and use that list in a for each loop in c#
I tried this
List<String> list=new List<String>();
if (rbtnMultipleScenario.Checked == true)
{
foreach ( CheckedListBox str in clbScenario.SelectedItems)
{
lstitems.Add(str);
}
}
By using String, I am not able to add all the selected items of Checkedlistbox.
Which type of list I have to use?

List<string> list=new List<string>();
if (rbtnMultipleScenario.Checked == true)
{
foreach ( string str in clbScenario.SelectedItems)
{
lstitems.Add(str);
}
}
This assumes that SelectedItems contains a collection of strings (which by your exception it does)

Related

Return ComboBox From Function?

The below code has a list and loops through each string item in that list and adds it to a ComboBox. This functions correctly, but I am curious if there is a possible way to pass a string list and ComboBox into a function and return the ComboBox with each item in the string list being added.
Example: gets a string list, then adds each string item to the list. This is great if there's one ComboBox; but if there are 3 or more, to avoid code repetition, passing in a list and ComboBox would save code.
List<string> myList = getList();
foreach (string listItem in myList)
{
myComboBox.Items.Add(listItem);
}
you can make method like
private void FillCombo(ComboBox myComboBox, List<string> list);
{
foreach (string listItem in myList)
{
myComboBox.Items.Add(listItem);
}
//alternatively, you can add it like fubo suggested in comment
//myComboBox.Items.AddRange(myList.ToArray());
}
and call it from somewhere in code
List<string> myList = getList();
FillCombo(this.comboBox1, myList);
FillCombo(this.comboBox2, myList);
// etc...

C# - Remove items from List when button is clicked

My problem is; When click a button I input items in List, next in DropDownList. Problem is where i click button again exist items again into my DropDown.
How to solve this problem(sorry for image)?
List<string> companyList = new List<string>();
foreach (string item in companyList.ToList())
{
companyList.Remove(item); ----> this not working.......
}
foreach (SPListItem item in myItemCol)
{
companyList.Add(item["Company"].ToString());
}
companyList.Sort();
foreach (string item in companyList.Distinct())
{
ddComFilter.Items.Add(item.ToString());
}
You can use the Contains method to check if it is already there
if(!ddComFilter.Items.Contains(items.ToString())
{
ddComFilter.Items.Add(item.ToString());
}
This will only add the item if it is not already in the dropdown
You could check for existence of the item before add it to the list.
foreach (SPListItem item in myItemCol)
{
if(!companyList.Contains(item["Company"].ToString())
{
companyList.Add(item["Company"].ToString());
}
}
Then you need to clear the ddComFilter before adding the values to it:
companyList.Sort();
ddComFilter.Items.Clear();
foreach (string item in companyList.Distinct())
{
ddComFilter.Items.Add(item.ToString());
}
Alternate solution:
You can bind the ddComFilter using the generated list, instead for iterating the collection and add one-by-one. if so you need not to clear the collection, remove items etc. The code for this will be:
ddComFilter.Datasource = companyList;
ddComFilter.DataBind();
Here is an useful article for you
You should clear your dropdown before adding list as items:
companyList.Sort();
ddComFilter.Items.Clear(); // clear
foreach (string item in companyList.Distinct())
{
ddComFilter.Items.Add(item.ToString());
}

Easy way to get array of selected indexes of CheckBoxList in ASP.NET

Is there a property or a method on the CheckBoxList class that will return an array of ints that represents all of the selected indexes? This is in ASP.NET.
According to the MSDN documentation, there doesn't appear to be. You'll have to iterate the items yourself.
Here is a method that does just that. It iterates each item, checks if it is selected, then adds the index to a list. I use a list because a list is mutable whereas an array is not. Then to return an array, I just call ToArray() on the list.
public int[] selectedIndexesOfCheckBoxList(CheckBoxList chkList)
{
List<int> selectedIndexes = new List<int>();
foreach (ListItem item in chkList.Items)
{
if (item.Selected)
{
selectedIndexes.Add(chkList.Items.IndexOf(item));
}
}
return selectedIndexes.ToArray();
}
You could create an extension method to simulate the behavior you want. The benefit of that being that you could re-use it on any list control. Below is a rough example (I'm just returning the list of strings of the values, you could return anything though, the index, the value, the entire list item, etc.).
public static List<string> SelectedValues(this ListControl lst)
{
List<string> returnLst = new List<string>();
foreach (ListItem li in lst.Items)
{
if (li.Selected == true)
{
returnLst.Add(li.Value);
}
return returnLst;
}

c# Is there a option to filter a listbox

I want to know if there is a possibility to filter a listbox. I mean it in such a way that is i add an item and the name is already in the listbox that you get a messagebox.show that tells you"Item already in the listbox". And that it won't be added twice.
You don't need to iterate throug the items as the Items collection of the ListBox implements the "Contains" method.
if (listBox1.Items.Contains(Item))
{
MessageBox.Show("ListBox already contains Item");
}
"Item" is in this case the Item from the other ListBox
Update. You could write:
if (listBox1.Items.Contains(listBox2.SelectedItem))
{
MessageBox.Show("ListBox already contains Item");
}
else
{
listBox1.Items.Add(listBox2.SelectedItem);
}
Use data binding might be one of the solutions:
List<string> SomeData=...
var filtered=SomeData.Where(...); // <-- Your filtering condition here
listBox1.DataSource = new BindingSource(choices, null);
Inside the event/method which adds list items inside your listbox you can add something like:
// search for list item in the listbox which has the text
ListItem li = theListBox.Items.FindByText("yourListItemName");
if (li != null)
{
// if list item exists display message
MessageBox.Show("ListBox already contains item with the name");
}
else
{
theListBox.Items.Add("yourListItemName");
}
here is a sample code try and implement it in you code
ListBox.ObjectCollection ListItem1= ListBox1.Items;
if(!string.IsNullOrEmpty(SearchBox.Text))
{
foreach (string str in ListItem1)
{
if (str.Contains(SearchBox.Text))
{
msgbox;
}
}
}

How to add to a list multiple selected values of a ListBox ? C#, ASP.NET web site

I'm working on one of my first projects. I have a listbox where I select multiple values and I would like to add each selection (selectedItem.Text) to a list of strings.
so far what I was working on is something like ..
selectedItem = new List<string>();
var value = lstpdfList.SelectedItem.Text;
for (int i = 0; i < lstpdfList.SelectedValue.Count(); i++)
{
selectedItem.Add(value);
}
I would really appreciate any advice.
Iterate each item from ListBox.Items collection
foreach (ListItem item in ListBox1.Items)
{
if (item.Selected)
{
selectedItem.Add(item.Text); // selectedImte.Add(item.Value);
}
}
There is SelectedItems property of ListBox, try to iterate throught it. For example, if there is strings in your ListBox, then your code might look like this:
selectedItem = new List<string>();
foreach (string value in lstpdfList.SelectedValues)
selectedItem.Add(value);
You can just cast them to strings:
var selectedItems = listBox1.SelectedItems
.Cast<string>()
.ToList();
If you have populated your ListBox with something other than just strings, just cast to whichever type you need, like so:
var selectedItems = listBox1.SelectedItems
.Cast<WhateverYourTypeIs>()
.Select(item => item.ToString())
.ToList();

Categories