代码之家  ›  专栏  ›  技术社区  ›  noyruto88

向中的列表视图添加搜索功能。网络C++

  •  0
  • noyruto88  · 技术社区  · 8 年前

    我试着从这里学习一个教程: https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-add-search-capabilities-to-a-listview-control ,它成功地在listview中显示数据,但搜索功能不起作用。

    有人知道这个吗?

    这是我的全部密码。

    #pragma once
    
    namespace ListviewSearch {
    
        using namespace System;
        using namespace System::ComponentModel;
        using namespace System::Collections;
        using namespace System::Windows::Forms;
        using namespace System::Data;
        using namespace System::Drawing;
    
        /// <summary>
        /// Summary for MyForm
        /// </summary>
        public ref class MyForm : public System::Windows::Forms::Form
        {
        public:
            MyForm(void)
            {
                InitializeComponent();
                //
                //TODO: Add the constructor code here
                //
            }
    
        protected:
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            ~MyForm()
            {
                if (components)
                {
                    delete components;
                }
            }
    
        private:
            /// <summary>
            /// Required designer variable.
            /// </summary>
            System::ComponentModel::Container ^components;
        private: System::Windows::Forms::ListView^  textListView;
        private: System::Windows::Forms::TextBox^  searchBox;
    
    #pragma region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            void InitializeComponent(void)
            {
                this->searchBox = (gcnew System::Windows::Forms::TextBox());
                this->textListView = (gcnew System::Windows::Forms::ListView());
                this->SuspendLayout();
                // 
                // searchBox
                // 
                this->searchBox->Location = System::Drawing::Point(12, 12);
                this->searchBox->Multiline = true;
                this->searchBox->Name = L"searchBox";
                this->searchBox->Size = System::Drawing::Size(251, 35);
                this->searchBox->TabIndex = 0;
                this->searchBox->TextChanged += gcnew System::EventHandler(this, &MyForm::searchBox_TextChanged);
                // 
                // textListView
                // 
                this->textListView->Location = System::Drawing::Point(12, 53);
                this->textListView->Name = L"textListView";
                this->textListView->Size = System::Drawing::Size(251, 215);
                this->textListView->TabIndex = 1;
                this->textListView->UseCompatibleStateImageBehavior = false;
                this->textListView->View = View::List;
    
                // Populate the ListViewWithItems
                textListView->Items->AddRange(gcnew array<ListViewItem^>{
                    gcnew ListViewItem("Amy Alberts"),
                    gcnew ListViewItem("Amy Recker"),
                    gcnew ListViewItem("Erin Hagens"),
                    gcnew ListViewItem("Barry Johnson"),
                    gcnew ListViewItem("Jay Hamlin"),
                    gcnew ListViewItem("Brian Valentine"),
                    gcnew ListViewItem("Brian Welker"),
                    gcnew ListViewItem("Daniel Weisman") });
    
                // Handle the TextChanged to get the text for our search.
                searchBox->TextChanged += gcnew EventHandler(this, &MyForm::searchBox_TextChanged);
    
                // 
                // MyForm
                // 
                this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
                this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
                this->ClientSize = System::Drawing::Size(276, 281);
                this->Controls->Add(this->textListView);
                this->Controls->Add(this->searchBox);
                this->Name = L"MyForm";
                this->Text = L"MyForm";
                this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);
                this->ResumeLayout(false);
                this->PerformLayout();
    
            }
    
    
    #pragma endregion
        private: System::Void MyForm_Load(System::Object^  sender, System::EventArgs^  e) {
        }
    
        private:
            void searchBox_TextChanged(Object^ sender, EventArgs^ e)
            {
                // Call FindItemWithText with the contents of the textbox.
                ListViewItem^ foundItem = textListView->FindItemWithText(searchBox->Text, false, 0, true);
                if (foundItem != nullptr)
                {
                    textListView->TopItem = foundItem;
                }
            }
    
    };
    }
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Jeff R.    8 年前

    在InitializeComponent的代码中,以下行包含两次:

    this->searchBox->TextChanged += gcnew System::EventHandler(this, &MyForm::searchBox_TextChanged);
    

    不确定这是否会导致您陈述的问题,但它会导致您的搜索代码在文本更改时运行两次。