当我输入字母时检索数据库中包含的字母的汉字,但是现在有几个小的问题,不知道怎么解决还请帮忙我现在逻辑是在KeyUp事件中写的,当用户在combox中输入字母时 我就检索相应的数据。
1。当我按键输入字母时,结果出来之后,有时鼠标会没有显示,但是还是可以选择的!
2。我想让上下左右键也可以选择,当我按上下左右时也触发了KeyUp 怎么可以把这个屏蔽掉!
3。我自己定义的字库,当我输入多个字母时就会出现问题,当我输入“AB“时就会出发辆次KeyUp事件无法完成查询!
我不知道大家听明白了吗?有疑问的话可以去运行一下程序!谢谢大家!!!!
请各位高手帮我改进一下!
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;namespace MingLingMS
{
/// <summary>
/// ziku 的摘要说明。
/// </summary>
public class ziku : System.Windows.Forms.Form
{
private System.Windows.Forms.ComboBox combox;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button2;
DataSet ds; public ziku()
{

InitializeComponent();
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// 
[STAThread]
static void Main() 
{
Application.Run(new ziku());
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.combox = new System.Windows.Forms.ComboBox();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
// 
// combox
// 
this.combox.Cursor = System.Windows.Forms.Cursors.Default;
this.combox.Location = new System.Drawing.Point(32, 16);
this.combox.Name = "combox";
this.combox.Size = new System.Drawing.Size(121, 20);
this.combox.TabIndex = 0;
this.combox.KeyUp += new System.Windows.Forms.KeyEventHandler(this.combox_KeyUp);
this.combox.DragEnter += new System.Windows.Forms.DragEventHandler(this.combox_DragEnter);
// 
// dataGrid1
// 
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(24, 56);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(240, 128);
this.dataGrid1.TabIndex = 1;
// 
// button1
// 
this.button1.Location = new System.Drawing.Point(184, 16);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
// 
// textBox1
// 
this.textBox1.Location = new System.Drawing.Point(32, 200);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 3;
this.textBox1.Text = "textBox1";
// 
// button2
// 
this.button2.Location = new System.Drawing.Point(168, 200);
this.button2.Name = "button2";
this.button2.TabIndex = 4;
this.button2.Text = "button2";
this.button2.Click += new System.EventHandler(this.button2_Click);
// 
// ziku
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.Color.Gray;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.combox);
this.Cursor = System.Windows.Forms.Cursors.Default;
this.Name = "ziku";
this.Text = "ziku";
this.Load += new System.EventHandler(this.ziku_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false); }
#endregion private void button1_Click(object sender, System.EventArgs e)
{
dataset();
}
private void combox_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyValue ==37 || e.KeyValue == 38 || e.KeyValue == 39 ||e.KeyValue == 40 )
{

}
else
{
dataset();
}

} private void ziku_Load(object sender, System.EventArgs e)
{

}
public void dataset()
{
string aa=this.combox.Text.Trim();
if(aa=="")
{
MessageBox.Show("请输入");
}
else
{
SqlConnection con=new SqlConnection("server=localhost;database=awsun;uid=sa;pwd=david;");

ds=new DataSet(); SqlCommand com=new SqlCommand("zkselecta",con);
com.CommandType=CommandType.StoredProcedure; con.Open(); SqlParameter par=new SqlParameter("@zkjx",SqlDbType.VarChar,20);
par.Value=aa;
com.Parameters.Add(par);

SqlDataAdapter da=new SqlDataAdapter("zkselecta",con);
da.SelectCommand=com;
da.Fill(ds,"ziku"); combox.DataSource=ds;
combox.DisplayMember="ziku.ziksy";
Cursor.Show();
combox.DroppedDown=true;
Cursor.Show();

dataGrid1.DataSource=ds;
dataGrid1.DataMember="ziku";
}
} private void button2_Click(object sender, System.EventArgs e)
{

} private void combox_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
Cursor.Show();
}
}
}

解决方案 »

  1.   

    还有一个问题就是当我出现查询结果以后 我想让combobox中保存原来输入的值,当我在原来的基础上再次查询 将会缩小查询的范围。当用户需要选择时用鼠标或者上下键进行选择!
      

  2.   

    private void combox_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
    {
    if(e.KeyValue ==37 || e.KeyValue == 38 || e.KeyValue == 39 ||e.KeyValue == 40 )
    {
    e.Handled=true;//取消按键
    }
    else
    {
    dataset();
    }

    }