列表框和文本框匹配单词 [英] Listbox and Textbox matching word
本文介绍了列表框和文本框匹配单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个文本框和一个列表框。在文本框的textchanges事件中,从数据库中填充列表框
然后从列表框中选择值并调用另一个值然后保存数据..我希望如果用户没有从列表框中选择任何值并离开文本框输入false值哪个列表框不包含我想阻止他...
我试过的代码是< br $>
btnclick
if (listbox1.items.contains(textbox1))
{
// 调用另一个值。来自数据库
}
else
messagebox.show( 请从搜索框中选择一个值);
提前致谢....
解决方案
// 代码设计文件的代码
命名空间 WindowsApplication1
{
partial class Form1
{
/// < 摘要 >
/// 必需的设计变量。
/// < / summary >
private System.ComponentModel.IContainer components = null ;
/// < 摘要 >
/// 清理正在使用的所有资源。
/// < / summary >
/// < param < span class =code-summarycomment> name =disposing > 如果管理资源应予以处理;否则,false。< / param >
protected 覆盖 void Dispose( bool 处理)
{
if (处置&&(components!= null ))
{
components.Dispose();
}
base .Dispose(disposing);
}
#region Windows窗体设计器生成的代码
/// < 摘要 >
/// Designer支持所需的方法 - 不要修改
/// 使用代码编辑器的方法的内容。
/// < / summary >
private void InitializeComponent()
{
this .txt = new System.Windows.Forms.TextBox();
this .lst = new System.Windows.Forms.ListBox();
this .SuspendLayout();
//
// txt
//
此 .txt.Location = new System.Drawing.Point( 40 , 21 );
此 .txt.Name = txt 跨度>;
this .txt.Size = new System.Drawing.Size( 325 , 20 );
this .txt.TabIndex = 0 ;
this .txt.TextChanged + = new System.EventHandler(此跨度> .txt_TextChanged);
this .txt.KeyDown + = new System.Windows.Forms.KeyEventHandler(此跨度> .txt_KeyDown);
this .txt.Leave + = new System.EventHandler(此跨度> .txt_Leave);
this .txt.Enter + = new System.EventHandler(此跨度> .txt_Enter);
//
// lst
//
此 .lst.FormattingEnabled = true ;
this .lst.Location = new System.Drawing.Point( 12 , 37 );
this .lst.Name = lst 跨度>;
this .lst.Size = new System.Drawing.Size( 10 , 4 );
this .lst.TabIndex = 1 ;
此 .lst.Visible = false ;
//
// Form1
//
this .AutoScaleDimensions = new System.Drawing.SizeF(6F,13F) ;
this .AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this .ClientSize = new System.Drawing.Size( 406 , 309 );
this .Controls.Add( this .lst);
this .Controls.Add( this .txt);
this .Name = Form1< /跨度>;
this .Text = Form1< /跨度>;
this .Load + = new System.EventHandler(此跨度> .Form1_Load);
this .ResumeLayout( false );
this .PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox txt;
private System.Windows.Forms.ListBox lst;
}
}
// cs文件代码
使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Text;
使用 System.Windows.Forms;
命名空间 WindowsApplication1
{
public partial class Form1:Form
{
DataTable tblList = new DataTable();
DataView dvList = null ;
public Form1()
{
InitializeComponent();
}
private void txt_TextChanged( object sender,EventArgs e)
{
dvList.RowFilter = Item Like' + txt.Text + %';
}
private void txt_Enter( object sender,EventArgs e)
{
lst.Width = txt.Width;
lst.Height = 100 ;
lst.Top = txt.Top + txt.Height;
lst.Left = txt.Left;
if (!lst.Visible)
lst.Visible = true 跨度>;
}
private void txt_Leave( object sender,EventArgs e)
{
if (lst.SelectedIndex!= -1)
{
txt.Text = lst.Text;
}
else
{
txt.Text = ;
MessageBox.Show( 请仅从列表中选择!);
}
lst.Visible = false ;
}
private void Form1_Load( object sender,EventArgs e)
{
tblList.Columns.Add( 项跨度>);
tblList.Rows.Add( ONE);
tblList.Rows.Add( TWO);
tblList.Rows.Add( THREE);
tblList.Rows.Add( FOUR);
tblList.Rows.Add( FIVE);
tblList.Rows.Add( SIX);
tblList.Rows.Add( SEVEN);
tblList.Rows.Add( EIGHT);
tblList.Rows.Add( NINE);
tblList.Rows.Add( TEN);
lst.DisplayMember = Item;
lst.DataSource = tblList;
dvList = tblList.DefaultView;
}
private void txt_KeyDown( object sender,KeyEventArgs e)
{
try
{
if (e.KeyValue == 38 ) // UP KEY
{
lst.SelectedIndex = lst.SelectedIndex - 1 ;
}
else if (e.KeyValue == 40 ) // DOWN KEY
{
lst.SelectedIndex = lst.SelectedIndex + 1 ;
}
}
catch {}
}
}
}
// 代码设计器文件
命名空间 WindowsApplication1
{
部分 class Form1
{
private System.ComponentModel.IContainer components = 空跨度>;
受保护 覆盖 void Dispose( bool 处理)
{
if (disposing& &(components!= null ))
{
components.Dispose();
}
base .Dispose(disposing);
}
#region Windows窗体设计器生成的代码
private void InitializeComponent()
{
this .txt = new System.Windows.Forms.TextBox();
this .lst = new System.Windows.Forms.ListBox();
this .SuspendLayout();
//
// txt
//
此 .txt.Location = new System.Drawing.Point( 40 , 21 );
此 .txt.Name = txt 跨度>;
this .txt.Size = new System.Drawing.Size( 325 , 20 );
this .txt.TabIndex = 0 ;
this .txt.TextChanged + = new System.EventHandler(此跨度> .txt_TextChanged);
this .txt.KeyDown + = new System.Windows.Forms.KeyEventHandler(此跨度> .txt_KeyDown);
this .txt.Leave + = new System.EventHandler(此跨度> .txt_Leave);
this .txt.Enter + = new System.EventHandler(此跨度> .txt_Enter);
//
// lst
//
此 .lst.FormattingEnabled = true ;
this .lst.Location = new System.Drawing.Point( 12 , 37 );
this .lst.Name = lst 跨度>;
this .lst.Size = new System.Drawing.Size( 10 , 4 );
this .lst.TabIndex = 1 ;
此 .lst.Visible = false ;
//
// Form1
//
this .AutoScaleDimensions = new System.Drawing.SizeF(6F,13F) ;
this .AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this .ClientSize = new System.Drawing.Size( 406 , 309 );
this .Controls.Add( this .lst);
this .Controls.Add( this .txt);
this .Name = Form1< /跨度>;
this .Text = Form1< /跨度>;
this .Load + = new System.EventHandler(此跨度> .Form1_Load);
this .ResumeLayout( false );
this .PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox txt;
private System.Windows.Forms.ListBox lst;
}
}
// ---------- -------------------------------------------------- -----------------------------------
// cs文件代码
使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Text;
使用 System.Windows.Forms;
命名空间 WindowsApplication1
{
public partial class Form1:Form
{
DataTable tblList = new DataTable();
DataView dvList = null ;
public Form1()
{
InitializeComponent();
}
private void txt_TextChanged( object sender,EventArgs e)
{
dvList.RowFilter = Item Like' + txt.Text + %';
}
private void txt_Enter( object sender,EventArgs e)
{
lst.Width = txt.Width;
lst.Height = 100 ;
lst.Top = txt.Top + txt.Height;
lst.Left = txt.Left;
if (!lst.Visible)
lst.Visible = true 跨度>;
}
private void txt_Leave( object sender,EventArgs e)
{
if (lst.SelectedIndex!= -1)
{
txt.Text = lst.Text;
}
else
{
txt.Text = ;
MessageBox.Show( 请仅从列表中选择!);
}
lst.Visible = false ;
}
private void Form1_Load( object sender,EventArgs e)
{
tblList.Columns.Add( 项跨度>);
tblList.Rows.Add( ONE);
tblList.Rows.Add( TWO);
tblList.Rows.Add( THREE);
tblList.Rows.Add( FOUR);
tblList.Rows.Add( FIVE);
tblList.Rows.Add( SIX);
tblList.Rows.Add( SEVEN);
tblList.Rows.Add( EIGHT);
tblList.Rows.Add( NINE);
tblList.Rows.Add( TEN);
lst.DisplayMember = Item;
lst.DataSource = tblList;
dvList = tblList.DefaultView;
}
private void txt_KeyDown( object sender,KeyEventArgs e)
{
try
{
if (e.KeyValue == 38 ) // UP KEY
{
lst.SelectedIndex = lst.SelectedIndex - 1 ;
}
else if (e.KeyValue == 40 ) // DOWN KEY
{
lst.SelectedIndex = lst.SelectedIndex + 1 ;
}
}
catch {}
}
}
}
// ------------------------ -------------------------------------------------- --------------------- 跨度>
i have a textbox and a list box. In textchanges event of textbox i m filling listbox from database
and then select value from listbox and calling another value and then saving data.. i want that if user doesn''t select any value from listbox and leaving textbox by typing false value which listbox doesn''t contains i want to stop him...
the code i tried is
btnclick
if(listbox1.items.contains(textbox1))
{
//calling another value. from database
}
else
messagebox.show("Please select a value from searchbox");
Thanks in advance....
解决方案
// CODE FOR designer FILE namespace WindowsApplication1 { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (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.txt = new System.Windows.Forms.TextBox(); this.lst = new System.Windows.Forms.ListBox(); this.SuspendLayout(); // // txt // this.txt.Location = new System.Drawing.Point(40, 21); this.txt.Name = "txt"; this.txt.Size = new System.Drawing.Size(325, 20); this.txt.TabIndex = 0; this.txt.TextChanged += new System.EventHandler(this.txt_TextChanged); this.txt.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_KeyDown); this.txt.Leave += new System.EventHandler(this.txt_Leave); this.txt.Enter += new System.EventHandler(this.txt_Enter); // // lst // this.lst.FormattingEnabled = true; this.lst.Location = new System.Drawing.Point(12, 37); this.lst.Name = "lst"; this.lst.Size = new System.Drawing.Size(10, 4); this.lst.TabIndex = 1; this.lst.Visible = false; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(406, 309); this.Controls.Add(this.lst); this.Controls.Add(this.txt); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.TextBox txt; private System.Windows.Forms.ListBox lst; } }
// CODE FOR cs FILE using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form1 : Form { DataTable tblList = new DataTable(); DataView dvList = null; public Form1() { InitializeComponent(); } private void txt_TextChanged(object sender, EventArgs e) { dvList.RowFilter = "Item Like '" + txt.Text + "%'"; } private void txt_Enter(object sender, EventArgs e) { lst.Width = txt.Width; lst.Height = 100; lst.Top = txt.Top + txt.Height; lst.Left = txt.Left; if (!lst.Visible) lst.Visible = true; } private void txt_Leave(object sender, EventArgs e) { if (lst.SelectedIndex != -1) { txt.Text = lst.Text; } else { txt.Text = ""; MessageBox.Show("Please select from list only !"); } lst.Visible = false; } private void Form1_Load(object sender, EventArgs e) { tblList.Columns.Add("Item"); tblList.Rows.Add("ONE"); tblList.Rows.Add("TWO"); tblList.Rows.Add("THREE"); tblList.Rows.Add("FOUR"); tblList.Rows.Add("FIVE"); tblList.Rows.Add("SIX"); tblList.Rows.Add("SEVEN"); tblList.Rows.Add("EIGHT"); tblList.Rows.Add("NINE"); tblList.Rows.Add("TEN"); lst.DisplayMember = "Item"; lst.DataSource = tblList; dvList = tblList.DefaultView; } private void txt_KeyDown(object sender, KeyEventArgs e) { try { if (e.KeyValue == 38) // UP KEY { lst.SelectedIndex = lst.SelectedIndex - 1; } else if (e.KeyValue == 40) // DOWN KEY { lst.SelectedIndex = lst.SelectedIndex + 1; } } catch { } } } }
// CODE FOR designer FILE namespace WindowsApplication1 { partial class Form1 { private System.ComponentModel.IContainer components = null; protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code private void InitializeComponent() { this.txt = new System.Windows.Forms.TextBox(); this.lst = new System.Windows.Forms.ListBox(); this.SuspendLayout(); // // txt // this.txt.Location = new System.Drawing.Point(40, 21); this.txt.Name = "txt"; this.txt.Size = new System.Drawing.Size(325, 20); this.txt.TabIndex = 0; this.txt.TextChanged += new System.EventHandler(this.txt_TextChanged); this.txt.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_KeyDown); this.txt.Leave += new System.EventHandler(this.txt_Leave); this.txt.Enter += new System.EventHandler(this.txt_Enter); // // lst // this.lst.FormattingEnabled = true; this.lst.Location = new System.Drawing.Point(12, 37); this.lst.Name = "lst"; this.lst.Size = new System.Drawing.Size(10, 4); this.lst.TabIndex = 1; this.lst.Visible = false; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(406, 309); this.Controls.Add(this.lst); this.Controls.Add(this.txt); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.TextBox txt; private System.Windows.Forms.ListBox lst; } } //----------------------------------------------------------------------------------------------- // CODE FOR cs FILE using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form1 : Form { DataTable tblList = new DataTable(); DataView dvList = null; public Form1() { InitializeComponent(); } private void txt_TextChanged(object sender, EventArgs e) { dvList.RowFilter = "Item Like '" + txt.Text + "%'"; } private void txt_Enter(object sender, EventArgs e) { lst.Width = txt.Width; lst.Height = 100; lst.Top = txt.Top + txt.Height; lst.Left = txt.Left; if (!lst.Visible) lst.Visible = true; } private void txt_Leave(object sender, EventArgs e) { if (lst.SelectedIndex != -1) { txt.Text = lst.Text; } else { txt.Text = ""; MessageBox.Show("Please select from list only !"); } lst.Visible = false; } private void Form1_Load(object sender, EventArgs e) { tblList.Columns.Add("Item"); tblList.Rows.Add("ONE"); tblList.Rows.Add("TWO"); tblList.Rows.Add("THREE"); tblList.Rows.Add("FOUR"); tblList.Rows.Add("FIVE"); tblList.Rows.Add("SIX"); tblList.Rows.Add("SEVEN"); tblList.Rows.Add("EIGHT"); tblList.Rows.Add("NINE"); tblList.Rows.Add("TEN"); lst.DisplayMember = "Item"; lst.DataSource = tblList; dvList = tblList.DefaultView; } private void txt_KeyDown(object sender, KeyEventArgs e) { try { if (e.KeyValue == 38) // UP KEY { lst.SelectedIndex = lst.SelectedIndex - 1; } else if (e.KeyValue == 40) // DOWN KEY { lst.SelectedIndex = lst.SelectedIndex + 1; } } catch { } } } } //-----------------------------------------------------------------------------------------------
这篇关于列表框和文本框匹配单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文