winform-基于键入的历史记录的自动完成文本框 [英] winform - autocomplete textbox based on typed history

查看:143
本文介绍了winform-基于键入的历史记录的自动完成文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的专家,

我想在winform中为文本框实现自动完成功能,就像SAP一样,SAP将基于用户输入而不是外部文件/数据库或硬编码集合.


谢谢,

--- Anil Kumar

Dear Experts,

I want to implement autocomplete feature in my winform for textbox just like SAP which will be based on user input instead of external file/database or hard-coded collections.


Thanks,

--- Anil Kumar

推荐答案

在列表中存储以前键入的条目(经过适当过滤,因此您不会在其中用一个无意义的字符或部分完成的条目填充) (请参阅下文).打开 AutoCompleteMode [ AutoCompleteSource [ AutoCompleteCustomSource [^ ].

(注意:我实际上并没有做到这一点,只是听了MSDN的话.)

如果希望历史记录在两次应用程序调用之间保持不变,则必须将其存储在文件或数据库中.如果希望它在应用程序的多个并发实例之间同步,则每次都必须重新读取它,或者在列表更改时使用进程间通信来通知其他实例.
Store previously typed entries (suitably filtered, so you don''t fill it with one character nonsense or partially completed entries) in a list (see later). Turn on AutoCompleteMode[^] for the text box, set AutoCompleteSource[^] to CustomSource, make an AutoCompleteStringCollection (use this as the storage location for your history) and use that as the AutoCompleteCustomSource[^].

(NB: I''ve not actually done this, just taking MSDN''s word for it.)

If you want the history to persist between invocations of the application, you will have to store it in a file or database. If you want it to synchronise between multiple concurrent instances of the application, you will have to re-read it each time, or use inter-process communication to notify other instances when the list is changed.


在按框解决方案"之外/作为替代,我将添加以下内容:

1)对于历史记录存储,请使用System.Collections.Generic.SortedList,它将在过滤重复项时提供更好的性能.

2)不要通过诸如LostFocus之类的任何UI原始"事件来触发向历史记录添加项目.当您编码实际使用文本框中的数据时,即使是通过语义也要触发.
3)对于运行时之间的持久性,数据库可能有点过大.仅使用表示应用程序状态的XML文件(可能是所有有限状态机,而不仅仅是历史记录).该文件应放置在通过调用System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData)找到的目录中.有关序列化的信息,请参见DataContract.参见System.Runtime.Serialization.DataContractSerializerSystem.Runtime.Serialization.DataContractAttributeSystem.Runtime.Serialization.DataMemberAttribute.这种方法同时具有非侵入性和灵活性.

—SA
In addition/alternative to the Solution by Box I would add the following:

1) For history storage, use System.Collections.Generic.SortedList, it will provide better performance in filtering our duplicates.

2) Do not trigger adding an item to the history by any UI "raw" events like LostFocus, etc. Trigger if by a semantic even, when you code actually uses the data from a text box.

3) For persistence between run-times, a database could be bit of overkill. Use just an XML file representing state of the application (maybe all Finite State Machine, not just history). The file should be placed in the directory found by the call System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData). For serialization, see DataContract; see System.Runtime.Serialization.DataContractSerializer, System.Runtime.Serialization.DataContractAttribute, System.Runtime.Serialization.DataMemberAttribute. This method is most non-intrusive an flexible at the same time.

—SA


我认为BobJanova的回答非常好(遵循他关于存储同步的建议).读一小段msdn应该可以解决您的问题(但是这里的许多问题都是如此).
无论如何,我为您创建了一个可执行示例(只需将其复制到新的WinForms项目中,并用以下代码替换Program.cs :)

I think BobJanova''s answer is very good (follow his advices for storing an syncronizing). A little msdn reading should solve your problem (but that is true for a lot of questions here).
Anyway I created an executable example for you (just copy in a new WinForms Project and replace Program.cs with following code:)

using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace AutoComplete
{
    static class Program
    {
        [STAThread] // You have to set STA when using AutoCompleteCustomSource !!!!
        static void Main()
        {
            // Create a Form 
            Form form = new Form();
            // ... and a description Label 
            Label label = new Label();
            label.Text = "I will remember";
            label.Dock = DockStyle.Top;
            // Now create a TexBox  
            TextBox textbox = new TextBox();
            textbox.Dock = DockStyle.Top;
            // ... and enable AutoComplete
            textbox.AutoCompleteMode = AutoCompleteMode.SuggestAppend; // Set the AutoCompleteMode you like
            textbox.AutoCompleteSource = AutoCompleteSource.CustomSource; // we will use a custome source (the entered text)
            // create a button to simulate validate/lost focus (whatever your real strategy will be)
            Button button = new Button();
            button.Dock = DockStyle.Top;
            button.Text = "Add to history";
            // if the button is clicked the text should be added to history 
            button.Click += delegate(object sender, EventArgs e)
            {
                // add a new text to the AutoCompleteCustomSource list
                string strTextToAdd = textbox.Text;
                if (!String.IsNullOrWhiteSpace(strTextToAdd))// in reality you will want filter or validate the text
                {
                    textbox.AutoCompleteCustomSource.Add(strTextToAdd);
                }
                // lets clear the textbox after that in this example
                textbox.Text = String.Empty;
                // ...  and refocus it
                textbox.Focus();
            };
            // Add all the Controls to the Form
            form.Controls.Add(button);
            form.Controls.Add(textbox);
            form.Controls.Add(label);
            // ... and show the form
            Application.Run(form);
        }
    }
}


这篇关于winform-基于键入的历史记录的自动完成文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆