CheckedListBox - Search for an item by text - c#

I have a CheckedListBox bound to a DataTable. Now I need to check some items programmatically, but I find that the SetItemChecked(...) method only accepts the item index.
Is there a practical way to get an item by text/label, without knowing the item index?
(NOTE: I've got limited experience with WinForms...)

You can implement your own SetItemChecked(string item);
private void SetItemChecked(string item)
{
int index = GetItemIndex(item);
if (index < 0) return;
myCheckedListBox.SetItemChecked(index, true);
}
private int GetItemIndex(string item)
{
int index = 0;
foreach (object o in myCheckedListBox.Items)
{
if (item == o.ToString())
{
return index;
}
index++;
}
return -1;
}
The checkListBox uses object.ToString() to show items in the list. You you can implement a method that search across all objects.ToString() to get an item index. Once you have the item index, you can call SetItemChecked(int, bool);
Hope it helps.

You may try to browse your Datatable. YOu can do a foreach on the DataTabke.Rows property or use SQL syntax as below:
DataTable dtTable = ...
DataRow[] drMatchingItems = dtTable.Select("label = 'plop' OR label like '%ploup%'"); // I assumed there is a "label" column in your table
int itemPos = drMatchingItems[0][id]; // take first item, TODO: do some checking of the length/matching rows
Cheers,

I am answering it very late, I hope it will help someone. If you want to find any item by name we can do it in two steps. First get index of item by text and then we can get actual item with the help of index.
var selectedItemIndex = cbxList.Items.IndexOf("sometext");
var selectedItem = cbxList.Items[selectedItemIndex];

Related

Trying to remove row(s) from listview

My code correctly identifies listview rows based on a textbox search criteria. However when I try to remove the unwanted row(s) I get an error message:-
InvalidArgument=Value of '-1' is not valid for 'index'.
I would be very grateful if someone could help me resolve this issue.. Thanks in advance.
foreach (ListViewItem item in listView1.Items){
foreach (ListViewItem.ListViewSubItem subItem in item.SubItems){
if (subItem.Text.ToLower().StartsWith(textBox1.Text.ToLower())){
var index = item.Index;
int.TryParse(listView1.Items[index].SubItems[4].Text, out val);
store[pos] = val;
pos++;
count++;
}else{
listView1.Items[index].Remove();
}
}
}
The problem is that you do not calculate your index in clause 'else'
}else{
listView1.Items[index].Remove();
}
You can't remove items from a collection you are iterating through Foreach, c# will throw an error because the collection changed, use for instead, and do it from the end to beginning (so you won't jump items when removing)
Try this approach:
Comments are in the code. Feel free to ask if you need additional explanation.
solution w/ Linq
using System.Linq;
...
//cast ListViewItemCollection as List<ListViewItem> for easier manipulation
var items = listView1.Items.Cast<ListViewItem>();
//first, get all items that don't have text matching with textBox text
List<int> itemIndexes =
items
.Where(item => !item.Text.ToLower().StartsWith(textBox1.Text.ToLower()))
.Select(item => item.Index).ToList();
int val;
//now, with all those indexes do your stuff
itemIndexes.ForEach(index =>
{
int.TryParse(listView1.Items[index].SubItems[4].Text, out val);
store[pos] = val;
pos++;
count++;
});
//lastly, sort indexes descending (for example: 10, 5,4,1) and remove them from listview
itemIndexes
.OrderByDescending(i=>i)
.ToList()
.ForEach(index => listView1.Items.RemoveAt(index));
EDIT: chaged code that it removes items that don't start with text box' text (like OP wanted).
EDIT 2: since you don't want to use Linq, you can solve your problem using plain old lists and loops
Main thing is, as I said in a comment, not to remove items inside of foreach loop but after you found all items that need to be removed.
Code is tested and working (without 4 lines with missing variables store, val etc ...)
solution w/o Linq
//list to hold intem indexes
List<int> itemIndexes = new List<int>();
foreach (ListViewItem item in listView1.Items)
{
foreach (ListViewItem.ListViewSubItem subItem in item.SubItems)
{
var index = item.Index;
if (subItem.Text.ToLower().StartsWith(textBox1.Text.ToLower()))
{
//your code (which doesnt compile since you didn't paste whole code)
//to test this, I had to remove following 4 lines.
int.TryParse(listView1.Items[index].SubItems[4].Text, out val);
store[pos] = val;
pos++;
count++;
}
else
{
itemIndexes.Add(index);
}
}
}
//lastly, sort indexes descending (for example: 10, 5,4,1)
//because if you remove item 1, whole list will shorten and
//you'll get index out of bounds error (in last step when
//trying to remove item with 10 but last index is 7)...
itemIndexes.Reverse();
//... and remove them from listview
foreach (int index in itemIndexes)
{
listView1.Items.RemoveAt(index);
}
tested with sample data - code will remove items that don't start with a, like bbb and dd)
ListView listView1 = new ListView();
listView1.Items.Add("aaa");
listView1.Items.Add("bbb");
listView1.Items.Add("ax");
listView1.Items.Add("dd");
TextBox textBox1 = new TextBox();
textBox1.Text = "a";

How to set Selectedvalue in Combobox c#

I have one combobox in which I have set DataSource Value, but when I try to set SelectedValue, the ComboBox returns null. so please help.
BindingList<KeyValuePair<string, int>> m_items =
new BindingList<KeyValuePair<string, int>>();
for (int i = 2; i <= 12; i++)
m_items.Add(new KeyValuePair<string, int>(i.ToString(), i));
ComboBox cboGridSize = new ComboBox();
cboGridSize.DisplayMember = "Key";
cboGridSize.ValueMember = "Value";
cboGridSize.DataSource = m_items;
cboGridSize.SelectedValue = 4;
when I set SelectedValue with 4 then it returns NULL.
Agree with #Laazo change to string.
cboGridSize.SelectedValue = "4";
or somthing similar to this
int selectedIndex = comboBox1.SelectedIndex;
Object selectedItem = comboBox1.SelectedItem;
MessageBox.Show("Selected Item Text: " + selectedItem.ToString() + "\n" +
"Index: " + selectedIndex.ToString());
and refer to this looks as if it would be good for your issue:
https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selecteditem(v=vs.110).aspx
How do I set the selected item in a comboBox to match my string using C#?
Getting selected value of a combobox
I came across this question while also trying to solve this problem. I solved it by creating the following extension method.
public static void ChooseItem<T>(this ComboBox cb, int id) where T : IDatabaseTableClass
{
// In order for this to work, the item you are searching for must implement IDatabaseTableClass so that this method knows for sure
// that there will be an ID for the comparison.
/* Enumerating over the combo box items is the only way to set the selected item.
* We loop over the items until we find the item that matches. If we find a match,
* we use the matched item's index to select the same item from the combo box.*/
foreach (T item in cb.Items)
{
if (item.ID == id)
{
cb.SelectedIndex = cb.Items.IndexOf(item);
}
}
}
I also created an interface called IDatabaseTableClass (probably not the best name). This interface has one property, int ID { get; set; } To ensure that we actually have an ID to compare to the int id from the parameter.

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.

Finding an item in asp.ListView with Value when paging is enabled

I'm trying to find the selected Item in an aspx ListView that is on a separate page, then switch the page and select the item. I have the value property of the ListViewItem I am looking for, but cannot seem to get it to work. Here is what I tried:
for (int i = 0; i < lvProject.Items.Count; i++)
{
if (((Label)lvProject.Items[i].FindControl("Project_IDLabel")).Text == project.ToString())
{
lvProject.SelectItem(i);
break;
}
}
So lvProject is my list view. The project Variable is an Int64 which represents the UID of my Project. This is also the Value of my ListViewItems. The problem with the code above is that when paging is enabled, and the item is on a different page this will not work because the listView.Items.Count is set to the # of Items on the current page only.
My goal is to find the item, set the listview to display the correct page, and finally select the item. You would figure that I could just set the SelectedValue property, but this is not that simple as it is read only. Any ideas would help greatly, thanks in advance.
--Roman
In order to get the total record count from the object data source, you should use the Selected event as follows:
protected void ObjectDataSource1_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
// Get total count from the ObjectDataSource
DataTable dt = e.ReturnValue as DataTable;
if (dt != null) recordCount = dt.Rows.Count; // recordCount being declared outside the method
}
You would then be able to search for the item as follows:
for (int i = 0; i < recordCount; i++)
{
Label lblItem = (Label)lvProject.Items[i].FindControl("IdLabel");
if (lblItem.Text.Equals(itemToSearch))
{
lvProject.SelectedIndex = i;
break;
}
}
Hope it helps!
How do you bind ListView Items?
If you are using database level paging (stored procedure, query) - you should do search in the same way - using database query/stored procedure by passing a search criteria.
If you are bind ListView Items to a collection of items which are provided by a business/data layer - you have to create search method on layer which provides items so this method will be able to loop through the items.
You should set the SelectedIndex property to i
for (int i = 0; i < lvProject.Items.Count; i++)
{
if (((Label)lvProject.Items[i].FindControl("Project_IDLabel")).Text == project.ToString())
{
lvProject.SelectedIndex = i;
break;
}
}

Select the specific column of ListView and print it in a new messagebox in C#.net

I've just started to use ListView in C#.net.
I got to know how to add items and subitems. Going through the listview I wanted to fetch all the data from a whole column with multiple rows.
I want to know how to do this.
I found this code to list a specific selected data from a row:
ListView.SelectedIndexCollection sel = listView1.SelectedIndices;
if (sel.Count == 1)
{
ListViewItem selItem = listView1.Items[sel[0]];
MessageBox.Show(selItem.SubItems[2].Text);
}
That was helpful but i want to list all the items in a row, may be i want to add all the column items in array?
private string[] GetListViewItemColumns(ListViewItem item) {
var columns = new string[item.SubItems.Count];
for (int column = 0; column < columns.Length; column++) {
columns[column] = item.SubItems[column].Text;
}
return columns;
}
I would recommend some caution against doing this. A ListView is really meant to display information, it is not a great collection class. Getting the data out of it is slow and crummy, it can only store strings. Keep the data in your program in its original form, maybe a List<Foo>. Now it is simple and fast.
foreach (ListViewItem item in listView1.Items) {
// Do something with item
}
you could do this by
foreach(ListViewItem item in listView1.Items)
{
foreach(var subtem in item.SubItems)
{
// Do what ever you want to do with the items.
}
}

Categories

Resources