当前位置:编程学习 > C#/ASP.NET >>

录入框如果联想呢?吃6元盒饭的人提问了。

如果在录入框中做到联想录入呢。如速达软件,在他们的录入下拉框中,如果你录入 “天”字,他会自动移到带有这个字的相应行去,方便人员的录入。是怎么做到的呢。请吃10元盒饭的人帮忙解决:)

具体情况见图:

--------------------编程问答-------------------- 有没有人之前做过类似的功能呢? --------------------编程问答-------------------- 目前还没做,有这个设想,想用.Select去实现或用查询实现,没做 --------------------编程问答-------------------- 把字典表查询到datatable里,在下拉框TextChanged事件里过滤关键字,把过滤后的结果重新绑定到下拉框里 --------------------编程问答-------------------- 应该不只可以过滤名称吧输代码也应该可以筛选.应该就用filter方式实现。“id like '" + combox.Text +”' or name like '" + combox.Text +"'" --------------------编程问答-------------------- 增加一个查询字典表,在TextChanged事件使用字典表。 --------------------编程问答-------------------- 这个现成的控件很多,也可以自己写一个

就是自动完成的功能,

AutoComplete 

有ajax的
有jquery的


--------------------编程问答-------------------- 编写查询文本框的TextChanged事件 --------------------编程问答-------------------- 根据你的图,这是一个组合控件,组合控件需要在其 text 文本变化的情况下判断下拉框是否处于打开状态,如果是,则触发你自定义的 AutoComplete 方法,这个方法所作的事情就是根据你输入的数据匹配最可能的 Item,并将它选中。 --------------------编程问答-------------------- k/v 集合
or
索引器 --------------------编程问答-------------------- --------------------编程问答-------------------- 把文本框的 AutoCompleteMode属性设为Suggest 
 AutoCompleteSouce 属性设为CustomSource
AutoCompleteCustomSource 属性设为你下拉列表的集合  --------------------编程问答-------------------- 应该不是用SQL的SELECT来实现。大家根据图可以他仔细看一下。谢谢大家提些好想法及思路。 --------------------编程问答-------------------- 哪里有这样现成的控件呢? --------------------编程问答-------------------- Ajax --------------------编程问答-------------------- CS的软件也能用AJAX,AJAX好象是BS的概念吧 --------------------编程问答-------------------- 我记得有人问过这个问题。。你可以搜索下帖子 --------------------编程问答-------------------- --------------------编程问答-------------------- 你用jQuery里的功能可以達到這個效果的. --------------------编程问答--------------------
引用 3 楼 gxingmin 的回复:
把字典表查询到datatable里,在下拉框TextChanged事件里过滤关键字,把过滤后的结果重新绑定到下拉框里


+1 

LZ可以用这个,,我也是用这种方法,用户输入"天"时,只显示包含天的客户名称..然后在选择,比LZ的方法更方便 --------------------编程问答-------------------- 吃不起盒饭的飘过 --------------------编程问答-------------------- 有jquery 的插件可以用,应该是auto开头的 --------------------编程问答-------------------- 不吃盒饭的能回答吗?

DEV的下拉控件自带这样的功能。 --------------------编程问答-------------------- combobox好像可以这样做的。
--------------------编程问答-------------------- 好象大家都没有做过像速达这样功能啊,他那种功能还是很有用。
另外我是要用C#做的C/S软件,不是B/S软件 --------------------编程问答-------------------- textchanged根据输入选中相应的selectindex --------------------编程问答-------------------- 先在数据库中 建立相关索引  然后 用  sql  like 语句 --------------------编程问答-------------------- 这个可以先查询出来放到集合里面,开始输入的时候
后面接着到这个集合里面去筛选,这样可以实现的 --------------------编程问答-------------------- --------------------编程问答-------------------- 要是用PB开发的话这个功能的话要比C#方便

在C#里开发,要考虑层,和相关的层查询

速度不是太快

总之这个问题有难度

关注。。。。。。。。。 --------------------编程问答-------------------- 那个功能我刚做过。http://community.csdn.net/  希望能对你有用。 --------------------编程问答-------------------- 给你个想法,
1.你的是c/s架构的,可以收集用户输入的历史记录用xml或者其他方式保存在用户的本地里,最好是分个线程去做这个事;
2.在用户输入一个字后,分一个线程去读取过滤出曾经输入过的内容,为什么要分出一个线程呢?想想,如果一个人字快的话,你就一个线程下去,不是逼他等你显示那些提示内容吗?对他来说没有这个必要啊。
--------------------编程问答-------------------- combox里就有这个功能的,只是有时候不能满足我们的要求。 --------------------编程问答-------------------- ComboBox里的 AutoCompleteMode 设置为Suggest
AutoCompleteSour 设置为 ListItems

让后在Items里添加数据就OK了。 --------------------编程问答-------------------- textchanged的时候,把不符合条件的都去掉,岂不是更好,但是要求搜索要更准确。 --------------------编程问答--------------------

           textbox1.AutoCompleteMode = AutoCompleteMode.Suggest;
            textbox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
            AutoCompleteStringCollection acsc = new AutoCompleteStringCollection();
            for (int i = 0; i < 100;i++ )
                acsc.Add("admin"+i.ToString());
            textbox1.AutoCompleteCustomSource = acsc;
--------------------编程问答--------------------

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Auto_Complete_ComboBox
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class frmAutoCompleteComboBox : System.Windows.Forms.Form
{
internal System.Windows.Forms.Label lblTest;
internal System.Windows.Forms.CheckBox chkLimitToList;
internal System.Windows.Forms.CheckBox chkAutoComplete;
internal System.Windows.Forms.ComboBox cbAutoComplete;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public frmAutoCompleteComboBox()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#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>
private void InitializeComponent()
{
this.lblTest = new System.Windows.Forms.Label();
this.chkLimitToList = new System.Windows.Forms.CheckBox();
this.chkAutoComplete = new System.Windows.Forms.CheckBox();
this.cbAutoComplete = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
// 
// lblTest
// 
this.lblTest.AutoSize = true;
this.lblTest.Location = new System.Drawing.Point(8, 151);
this.lblTest.Name = "lblTest";
this.lblTest.Size = new System.Drawing.Size(0, 16);
this.lblTest.TabIndex = 7;
// 
// chkLimitToList
// 
this.chkLimitToList.Checked = true;
this.chkLimitToList.CheckState = System.Windows.Forms.CheckState.Checked;
this.chkLimitToList.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.chkLimitToList.Location = new System.Drawing.Point(208, 39);
this.chkLimitToList.Name = "chkLimitToList";
this.chkLimitToList.Size = new System.Drawing.Size(80, 16);
this.chkLimitToList.TabIndex = 2;
this.chkLimitToList.Text = "Limit to List";
// 
// chkAutoComplete
// 
this.chkAutoComplete.Checked = true;
this.chkAutoComplete.CheckState = System.Windows.Forms.CheckState.Checked;
this.chkAutoComplete.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.chkAutoComplete.Location = new System.Drawing.Point(208, 15);
this.chkAutoComplete.Name = "chkAutoComplete";
this.chkAutoComplete.Size = new System.Drawing.Size(96, 16);
this.chkAutoComplete.TabIndex = 1;
this.chkAutoComplete.Text = "Auto Complete";
// 
// cbAutoComplete
// 
this.cbAutoComplete.Items.AddRange(new object[] {
"Aaron Bud",
"Aarts Jan",
"Abas S.J.",
"Abbey Michael",
"Abdelguerfi Mahdi",
"Abel David",
"Abel Peter",
"Abelson Amanda",

"Zernik Uri",
"Ziarko Wojciech P.",
"Zicari Roberto"});
this.cbAutoComplete.Location = new System.Drawing.Point(16, 15);
this.cbAutoComplete.MaxDropDownItems = 10;
this.cbAutoComplete.Name = "cbAutoComplete";
this.cbAutoComplete.Size = new System.Drawing.Size(176, 21);
this.cbAutoComplete.TabIndex = 0;
this.cbAutoComplete.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.cbAutoComplete_KeyPress);
// 
// frmAutoCompleteComboBox
// 
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(312, 182);
this.Controls.Add(this.lblTest);
this.Controls.Add(this.chkLimitToList);
this.Controls.Add(this.chkAutoComplete);
this.Controls.Add(this.cbAutoComplete);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "frmAutoCompleteComboBox";
this.Text = "Auto Complete ComboBox - Example";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new frmAutoCompleteComboBox());
}

private void cbAutoComplete_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (this.chkAutoComplete.Checked)
this.AutoComplete(this.cbAutoComplete, e, this.chkAutoComplete.Checked);
}

        

// AutoComplete
public void AutoComplete(ComboBox cb, System.Windows.Forms.KeyPressEventArgs e)
{
this.AutoComplete(cb, e, false);
}

public void AutoComplete(ComboBox cb, System.Windows.Forms.KeyPressEventArgs e, bool blnLimitToList)
{
string strFindStr = "";

if (e.KeyChar == (char)8) 
{
if (cb.SelectionStart <= 1) 
{
cb.Text = "";
return;
}

if (cb.SelectionLength == 0)
strFindStr = cb.Text.Substring(0, cb.Text.Length - 1);
else 
strFindStr = cb.Text.Substring(0, cb.SelectionStart - 1);
}
else 
{
if (cb.SelectionLength == 0)
strFindStr = cb.Text + e.KeyChar;
else
strFindStr = cb.Text.Substring(0, cb.SelectionStart) + e.KeyChar;
}

int intIdx = -1;

// Search the string in the ComboBox list.

intIdx = cb.FindString(strFindStr);
 
if (intIdx != -1)
{
cb.SelectedText = "";
cb.SelectedIndex = intIdx;
cb.SelectionStart = strFindStr.Length;
cb.SelectionLength = cb.Text.Length;
e.Handled = true;
}
else
{
e.Handled = blnLimitToList;
}

}


}
}




补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,