【问题标题】:Adding search capabilities to a listview in .net c++在 .net c++ 中向列表视图添加搜索功能
【发布时间】:2018-07-10 02:49:36
【问题描述】:

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

有人对此有想法吗?

这是我的全部代码。

#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;
            }
        }

};
}

【问题讨论】:

  • 你说它“行不通”是什么意思?它没有找到文本吗?所以“amy”什么也没找到并返回null?它是否找到了某些东西但未能将其显示为 TopItem?
  • 似乎 searchBox_TextChanged() 不起作用。

标签: .net winforms listview c++-cli


【解决方案1】:

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

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

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

【讨论】:

  • 哦..我没看到。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-13
  • 2017-07-19
  • 1970-01-01
  • 2012-09-04
  • 2023-03-26
相关资源
最近更新 更多