简体   繁体   English

C#-从用户输入中搜索子字符串

[英]C# - Search for Substring from User Input

I wish to add a search function to my application, I've seen many examples however, all of the data is added by the programmer. 我希望在我的应用程序中添加一个搜索功能,但是我看到了很多示例,但是所有数据都是由程序员添加的。 How would I implement a search function where the parameters/values aren't pre-defined. 如果参数/值未预定义,我将如何实现搜索功能。 I'm guessing I'd have to create another instance of a List<> as I'm already using one? 我猜我必须创建List <>的另一个实例,因为我已经在使用它了?

        public Form1()
    {
        InitializeComponent();
    }

    List<Book> books = new List<Book>();


    private void Form1_Load(object sender, EventArgs e)
    {

    }

    //Settng Values
    public void button1_Click(object sender, EventArgs e)
    {

        Book b = new Book();
        b.Title = textBox1.Text;
        b.ISBN = textBox2.Text;
        b.Onloan = trueCheckBox.Checked;
        listView1.Items.Add(b.Title + ", " + b.ISBN + ", " + b.Onloan);
        books.Add(b);
        textBox1.Text = null;
        textBox2.Text = null;

    }

    //Method to check if item is aviable or note - boolean type
    void avaiable()
    {
        if (trueCheckBox.Checked = true)
        {



            bool onloan = true;
        }

        else
        {
            bool onloan = false;
        }
    }


    private void button2_Click(object sender, EventArgs e)
    {
        Remove();
    }

    //Remove item from both the List & Listview
    void Remove()
    {

        try
        {

            listView1.Items.Remove(listView1.SelectedItems[0]);
            books.RemoveAt(listView1.SelectedItems[0].Index);

        }
        catch
        {

        }
    }


    //Display  information within their field when selected an item is selected
    private void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (listView1.SelectedItems.Count == 0)
        {
            return;
        }
        else
        {


            textBox1.Text = books[listView1.SelectedItems[0].Index].Title;
            textBox2.Text = books[listView1.SelectedItems[0].Index].ISBN;
            trueCheckBox.Checked = books[listView1.SelectedItems[0].Index].Onloan;
        }
    }


    //Update the values without having to re-add
    private void button3_Click(object sender, EventArgs e)
    {
        if (listView1.SelectedItems.Count == 0)
        {
            return;
        }
        else
        {
            books[listView1.SelectedItems[0].Index].Title = textBox1.Text;
            books[listView1.SelectedItems[0].Index].ISBN = textBox2.Text;
            books[listView1.SelectedItems[0].Index].Onloan = trueCheckBox.Checked;
            listView1.SelectedItems[0].Text = textBox1.Text + ", " + textBox2.Text + ", "+ trueCheckBox.Checked;
        }
    }

    private void searchBox_TextChanged(object sender, EventArgs e)
    {
        //Here's where I am stuck, I've added a textField and labelled it search box
    }
}


//Class - set & get methods
class Book
{

    public string isbn;
    public string title;
    private Boolean onloan;


    public Book()
    {
        this.isbn = isbn;
        this.title = title;
    }

    public string ISBN
    {
        get { return isbn; }
        set { isbn = value; }
    }
    public string Title
    {

        get { return title; }
        set { title = value; }
    }
    public Boolean Onloan
    {
        get { return onloan; }
        set { onloan = value; }
    }

}

} }

Thank you. 谢谢。

EDIT- 编辑-

Essentially, the search function should allow the user to search for a book using just a substring ie "Mars" should return a the book with the title "The Programmer From Mars" < (Just an example). 本质上,搜索功能应允许用户仅使用子字符串来搜索书籍,即“火星”应返回标题为“来自火星的程序员” <(仅作为示例)的书籍。 I'm guessing I'd have to use the .Contain method? 我猜我必须使用.Contain方法?

Correct, you will need to use the "Contains" method. 正确,您将需要使用“包含”方法。 Note that "Contains" is case sensitive so you would need to cater for that. 请注意,“包含”区分大小写,因此您需要满足此要求。

So you would have something like: 因此,您将获得以下内容:

var textToSearch = searchBox.Text.ToLower();

var foundBooks = books.Where(book => book.Title.ToLower().Contains(textToSearch)).ToList();

Assuming books is the list of books to search from: 假设books是要搜索的书籍列表:

List<Book> searchResult = books.Where(b => b.Title.Contains(searchTerm)).ToList();

This would return a list of books where the input string is found in the title. 这将返回在标题中找到输入字符串的书籍列表。 You can do exact matching with b.Title == searchTerm , and similar with the other properties of Book . 您可以使用b.Title == searchTerm进行精确匹配,并与Book的其他属性相似。

The syntax above is LINQ and can be a little confusing at first, but works very well for things like this. 上面的语法是LINQ ,起初可能会有些混乱,但是对于这种情况非常有效。

Edit: 编辑:

To use this, you'll want using System.Linq; 要使用此功能,您需要using System.Linq;

Since your search seems to be on TextChanged , you'd want to put this code in your searchBox_TextChanged() method: 由于您的搜索似乎在TextChanged ,因此您希望将此代码放入searchBox_TextChanged()方法中:

This gets the textbox the user is typing into: 这将获得用户输入的文本框:

TextBox searchTerm = sender as TextBox;

And this filters your list of books by the current search: 这会根据当前搜索过滤您的图书清单:

List<Book> searchResult = books.Where(b => b.Title.Contains(searchTerm.Text)).ToList();

Note as in the other answer you may want to do ToLower() (or upper) to make searches case-insensitive. 请注意,在其他答案中,您可能需要执行ToLower() (或upper)以使搜索不区分大小写。

All that's left to do now is display the filtered result searchResult back to the user. 现在剩下要做的就是将过滤后的结果searchResult返回给用户。

Managed to do it, this is my code and it works. 设法做到这一点,这是我的代码,并且可以正常工作。 If anyone wants to use it for future reference here it is. 如果有人想将其用作将来的参考,那就是。

        private void button4_Click(object sender, EventArgs e)
    {
        listView1.SelectedItems.Clear();

        for (int  i = listView1.Items.Count -1; i >= 0; i--)
        {
            if (listView1.Items[i].ToString().ToLower().Contains(searchBox.Text.ToLower())) {

                listView1.Items[i].Selected = true;

            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM