How do I search for ListView items?

Asked 2 years ago, Updated 2 years ago, 41 views

Programming ListView on C#.
Simply register and search for items and subitems.
The logic of the search doesn't work, so could someone please let me know?

For content, type the words (name) you want to search in the text box and
If you press the button (Button-Click) to find a match for the search item (name),
The focus is shifted to the items found and the contents are displayed in the text box.
An Index error occurs when the number of choices is set to 0.
Therefore, the number of choices should be greater than or equal to zero or
I tried to say for loop, but it didn't work.

I will write the code below, so I would appreciate it if you could let me know where the error is.

I look forward to your kind cooperation.

public void Button_Click (object sender, EventArgse)
{
    string name = Name.Text;
    if(ListView1.SelectedItems.Count>0)
    {

    variable=ListView1.SelectedItems[0];

    for (inti=0;i>=ListView1.SelectedItems.Count;i++)
    {
        if(ListView1.Items[0].Text.Contains(Name.Text))
        {
        if(ListView1.FocusedItem!=null)
            ListView1.FocusedItem.Focused=false;
        if(ListView1.SelectedItems.Count>0)
            ListView 1.SelectedItems[0].Selected=false;

        ListView 1.Items[i].Selected=true;
        } ListView1.Focus();
    }
    else
    {
        MessageBox.Show("Error");
    }
    }
}

c#

2022-09-29 21:58

1 Answers

TextBox is called Name?
The name must not be the same as the property that the Form has.(^_^;)

Do you mean to search among the selected items?

private void button1_Click(object sender, EventArgse){
    string name = txtName.Text;
    variables = listView1.SelectedItems;
    if(items.Count==0){
        MessageBox.Show("Select the scope you want to search.");
        return;
    }
    ListViewItem findItem=null;
    foreach(ListViewItem items){
        if(item.Text.Contains(name)){
            findItem=item;
            break;
        }
    }
    if(findItem!=null){
        listView1.SelectedItems.Clear();
        findItem.Selected=true;
        findItem.Focused=true;
        listView1.Focus();
    } else{
        MessageBox.Show("Error");
    }
}


2022-09-29 21:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.