如何在C#中listbox控件中输入值立即跳到该选项 本帖最后由 xiaop1108 于 2009-11-24 13:57:17 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 listbox 没用过,貌似 DropDownList 就有你要的效果? listbox有楼主说的效果,不过只能判断输入的1位 楼主用combobox吧. 将combobox的AutoCompleteMode设为SuggestAppend AutoCompleteSource设为ListItems 就能实现你要的自动完成效果 int count = 0; string str = null; private void listBox1_KeyPress(object sender, KeyPressEventArgs e) { if(e.KeyChar>='0'&&e.KeyChar<='9') { count++; if (count <3) { str += e.KeyChar.ToString(); } else { str += "1"; listBox1.Items.Add(str); } } }这个行吧 int count = 0; string str = null; private void listBox1_KeyPress(object sender, KeyPressEventArgs e) { if(e.KeyChar>='0'&&e.KeyChar<='9') { count++; if (count <3) { str += e.KeyChar.ToString(); } else { str += "1"; listBox1.Items.Add(str); str = null; count = 0; } } }修正一下 private _recordKeypressChar=""; private void listBox1_KeyPress(object sender, KeyPressEventArgs e) { _recordKeypressChar+=e.keyChar;//记录键盘按下的数字 string reg=string.Format("^{0}",_recordKeypressChar);//对比字符串 bool isFind=true; for(int i=0;i<listBox.Item.Count;i++){ //比对listbox中的item,看看是否匹配,匹配则改变listbox的选择项,否则清空键盘记录字符串 if(Regex.IsMatch(listBox.Item[i].ToString(),reg)){ listBox.SelectIndex=i; isFind=true; break; }else isFind=false; } if(!isFind) _recordKeypressChar==""; } 应该是我没说清楚吧,是这样的,我是在vs2005环境下做的一个wince下的一个小程序,listbox中的值是客户的代码,可能会有好几百个,这些代码呢我是放在了wince上的一个txt文件中,方便用户自己增加或删除客户,用户在listbox上输入代码的前几个字符那么客户代码前几个字符符合的就显示出来让用户自己选,如果用户输入的客户代码不存在,那么就不会在listbox中显示 我写的代码应该能实现吧,不就是在listbox中按下键盘上的数字,listbox就跳到相应的item上去么,没有就不跳,应该就是这个意思吧。如果不是这个意思的话,那我就是没有看懂了。 楼主用combobox吧. 将combobox的AutoCompleteMode设为SuggestAppend AutoCompleteSource设为ListItems 就能实现你要的自动完成效果 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace WindowsApplication170{ public partial class Form1 : Form { ListBox LB = new ListBox(); List<String> InputChars = new List<String>(); public Form1() { InitializeComponent(); LB.Parent = this; LB.Items.AddRange(new Object[] { 100, 121, 122, 133, 250, 231, 202, 301, 304, 302 }); LB.KeyDown += new KeyEventHandler(LB_KeyDown); } void LB_KeyDown(object sender, KeyEventArgs e) { LocateIndex(e.KeyValue); if (LB.SelectedIndex == -1) { InputChars.Clear(); LocateIndex(e.KeyValue); // 如果找不到就用刚输入的字符开头再找一遍 } e.SuppressKeyPress = true; } void LocateIndex(int C) { InputChars.Add(((Char)C).ToString()); String InputString = String.Join(String.Empty, InputChars.ToArray()); LB.SelectedIndex = LB.FindString(InputString); } }} e.SuppressKeyPress这个属性也没有啊,你用的C#是哪个版本的啊 我懂楼主的意思了。楼主说的是LISTBOX,不是COMBOBOX,后者有AutoCompleteMode和AutoCompleteSource设置可以解决这个问题。但listbox没有这两项。listbox本身有的功能是只能一次性的捕捉键盘,比如说输入“23”,光标先跑到2开头的项那里去,然后紧接着跑到3开头的项那里去。我估计要编程才能实现。楼主试一下楼上编程实现的看行不行,我没有试。【原创家庭记账簿1.1.1,简单实用,欢迎工薪阶层、家庭主妇下载使用http://download.csdn.net/source/1849003】 定义 // // 摘要: // 查找 System.Windows.Forms.ListBox 中以指定字符串开始的第一个项。 // // 参数: // s: // 要搜索的文本。 // // 返回结果: // 找到的第一个项的从零开始的索引;如果找不到匹配项,则返回 ListBox.NoMatches。 // // 异常: // System.ArgumentOutOfRangeException: // s 参数的值小于 -1,或者大于或等于项的计数。 public int FindString(string s); // // 摘要: // 获取或设置一个值,该值指示键事件是否应传递到基础控件。 // // 返回结果: // 如果键事件不应该发送到该控件,则为 true;否则为 false。 public bool SuppressKeyPress { get; set; }http://msdn.microsoft.com/en-us/kh590ewz(zh-cn).aspxFindString 方法 (String, Int32).NET Framework 类库ListBox..::.FindString 方法 (String, Int32)更新:2007 年 11 月查找 ListBox 中以指定字符串开头的第一个项。搜索从特定的起始索引处开始。命名空间: System.Windows.Forms程序集: System.Windows.Forms(在 System.Windows.Forms.dll 中)你该不会是webform里的listbox,combobox吧,或者你是用.NET Compact Framework ? public partial class Form1 : Form { private string _recordKeypressChar; List<int> matchedIndices; public Form1() { InitializeComponent(); matchedIndices = new List<int>(new int[listBox.Items.Count]); for (int i = 0; i < listBox.Items.Count; i++) matchedIndices[i] = i; } private void listBox_KeyPress(object sender, KeyPressEventArgs e) { _recordKeypressChar += e.KeyChar;//记录键盘按下的数字 string reg = string.Format("^{0}", _recordKeypressChar);//对比字符串;^表示开头 int i = 0; do { if (!Regex.IsMatch(listBox.Items[matchedIndices[i]].ToString(), reg)) matchedIndices.RemoveAt(i);//排除法,留下符合条件的项的索引 else i++; } while (i < matchedIndices.Count); if (matchedIndices.Count == 0) { for (int j = 0; j < listBox.Items.Count; j++) matchedIndices.Add(j); listBox.SelectedIndex = -1; _recordKeypressChar = string.Empty; } else listBox.SelectedIndex = matchedIndices[0]; e.Handled = true; //foreach (int m in matchedIndices) // Debug.WriteLine(m); //Debug.WriteLine("键:" + _recordKeypressChar); } } public partial class Form1 : Form { private string _recordKeypressChar; List <int> matchedIndices; public Form1() { InitializeComponent(); matchedIndices = new List <int>(new int[listBox.Items.Count]); for (int i = 0; i < listBox.Items.Count; i++) matchedIndices[i] = i; } private void listBox_KeyPress(object sender, KeyPressEventArgs e) { _recordKeypressChar += e.KeyChar;//记录键盘按下的数字 string reg = string.Format("^{0}", _recordKeypressChar);//对比字符串;^表示开头 int i = 0; do { if (!Regex.IsMatch(listBox.Items[matchedIndices[i]].ToString(), reg)) matchedIndices.RemoveAt(i);//排除法,留下符合条件的项的索引 else i++; } while (i < matchedIndices.Count); if (matchedIndices.Count == 0) { for (int j = 0; j < listBox.Items.Count; j++) matchedIndices.Add(j); listBox.SelectedIndex = -1; _recordKeypressChar = string.Empty; } else listBox.SelectedIndex = matchedIndices[0]; e.Handled = true; //foreach (int m in matchedIndices) // Debug.WriteLine(m); //Debug.WriteLine("键:" + _recordKeypressChar); } } 不好意思,我还真的是用的webform里面的listbox,请问应该是用什么地方的listbox或combobox呢? 刚才看错了,我用的winform里面的listbox,不是webform里面的,我做的是wince里面的程序,用的只有winform里面的 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO;namespace mc1000_lgj{ public partial class ReadBarCode : Form { public ReadBarCode() { InitializeComponent(); } private void ReadBarCode_Load(object sender, EventArgs e) { if (this.InitReader()) { this.StartRead(); } else { this.Close(); return; } } protected override void OnClosing(CancelEventArgs e) { base.OnClosing(e); } private bool InitReader() { listBox1.Items.Clear(); string strContent; FileStream fsMyfile = new FileStream(@"\myfile.txt", FileMode.OpenOrCreate , FileAccess.Read ); StreamReader sr = new StreamReader(fsMyfile); while (sr.Peek() >= 0) { strContent = sr.ReadLine(); listBox1.Items.Add(strContent); } sr.Close(); listBox1.SelectedIndex = 0; this.listBox1.Focus(); this.Activated += new EventHandler(ReadBarCode_Activated); this.Deactivate += new EventHandler(ReadBarCode_Deactivate); return true; } private void ReadBarCode_Deactivate(object sender, EventArgs e) { this.StopRead(); } private void ReadBarCode_Activated(object sender, EventArgs e) { if (!this.MyReaderData.IsPending) this.StartRead(); } private void listBox1_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)13) { textBox2.SelectAll(); textBox2.Focus(); } } }}这个是我的代码,当我用到e.SuppressKeyPresst和listbox1.SelectedIndex = listbox1.FindString(InputString)时,在e或者listbox1之后敲点之后不出现SuppressKeyPresst或FindString()方法或属性啊?我装的是VS2005的完整版,应该不会是.NET Compact Framework 各位是这样的,我在做其他winform程序时发现listbox或combobox是有那些方法或属性的,但是我说的是在我是要部署到win CE里面去的,也就是部署到智能设备/移动设备里面去的,那个里面的listbox或combobox是没有这个方法的 新手一个请教(Timer) execl.Save()里面要写什么参数,他才不会默认给我保存? vs2003,c#中listview问题 导入功能 关于反射动态获得类实例 新年快乐,提两个Excel的问题,在线 向acewang表示歉意,请斑竹保留 看到有人是这样写select语句的,是不是SQL语句效率的问题....................... 在C#中如何实现使文件夹成为共享或Web共享 寻找c#精彩教程(电子版) c# 用 mysql.data 无法更新mysql中数据 c#,在登陆界面输入帐号密码,进入主窗体,然后登陆界面关闭。该怎么做
AutoCompleteSource设为ListItems 就能实现你要的自动完成效果
string str = null;
private void listBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar>='0'&&e.KeyChar<='9')
{
count++;
if (count <3)
{
str += e.KeyChar.ToString();
}
else
{
str += "1";
listBox1.Items.Add(str);
}
}
}
这个行吧
string str = null;
private void listBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar>='0'&&e.KeyChar<='9')
{
count++;
if (count <3)
{
str += e.KeyChar.ToString();
}
else
{
str += "1";
listBox1.Items.Add(str);
str = null;
count = 0;
}
}
}
修正一下
{
_recordKeypressChar+=e.keyChar;//记录键盘按下的数字
string reg=string.Format("^{0}",_recordKeypressChar);//对比字符串
bool isFind=true;
for(int i=0;i<listBox.Item.Count;i++){
//比对listbox中的item,看看是否匹配,匹配则改变listbox的选择项,否则清空键盘记录字符串
if(Regex.IsMatch(listBox.Item[i].ToString(),reg)){
listBox.SelectIndex=i;
isFind=true;
break;
}else
isFind=false;
}
if(!isFind)
_recordKeypressChar=="";
}
AutoCompleteSource设为ListItems 就能实现你要的自动完成效果
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication170
{
public partial class Form1 : Form
{
ListBox LB = new ListBox();
List<String> InputChars = new List<String>(); public Form1()
{
InitializeComponent(); LB.Parent = this;
LB.Items.AddRange(new Object[] { 100, 121, 122, 133, 250, 231, 202, 301, 304, 302 });
LB.KeyDown += new KeyEventHandler(LB_KeyDown);
} void LB_KeyDown(object sender, KeyEventArgs e)
{
LocateIndex(e.KeyValue); if (LB.SelectedIndex == -1)
{
InputChars.Clear();
LocateIndex(e.KeyValue); // 如果找不到就用刚输入的字符开头再找一遍
} e.SuppressKeyPress = true;
} void LocateIndex(int C)
{
InputChars.Add(((Char)C).ToString());
String InputString = String.Join(String.Empty, InputChars.ToArray()); LB.SelectedIndex = LB.FindString(InputString);
}
}
}
这个属性也没有啊,你用的C#是哪个版本的啊
我懂楼主的意思了。楼主说的是LISTBOX,不是COMBOBOX,后者有AutoCompleteMode和AutoCompleteSource设置可以解决这个问题。但listbox没有这两项。listbox本身有的功能是只能一次性的捕捉键盘,比如说输入“23”,光标先跑到2开头的项那里去,然后紧接着跑到3开头的项那里去。我估计要编程才能实现。楼主试一下楼上编程实现的看行不行,我没有试。
【原创家庭记账簿1.1.1,简单实用,欢迎工薪阶层、家庭主妇下载使用
http://download.csdn.net/source/1849003】
定义
//
// 摘要:
// 查找 System.Windows.Forms.ListBox 中以指定字符串开始的第一个项。
//
// 参数:
// s:
// 要搜索的文本。
//
// 返回结果:
// 找到的第一个项的从零开始的索引;如果找不到匹配项,则返回 ListBox.NoMatches。
//
// 异常:
// System.ArgumentOutOfRangeException:
// s 参数的值小于 -1,或者大于或等于项的计数。
public int FindString(string s); //
// 摘要:
// 获取或设置一个值,该值指示键事件是否应传递到基础控件。
//
// 返回结果:
// 如果键事件不应该发送到该控件,则为 true;否则为 false。
public bool SuppressKeyPress { get; set; }
http://msdn.microsoft.com/en-us/kh590ewz(zh-cn).aspx
FindString 方法 (String, Int32)
.NET Framework 类库
ListBox..::.FindString 方法 (String, Int32)更新:2007 年 11 月查找 ListBox 中以指定字符串开头的第一个项。搜索从特定的起始索引处开始。命名空间: System.Windows.Forms
程序集: System.Windows.Forms(在 System.Windows.Forms.dll 中)
你该不会是webform里的listbox,combobox吧,或者你是用.NET Compact Framework ?
public partial class Form1 : Form
{
private string _recordKeypressChar;
List<int> matchedIndices; public Form1()
{
InitializeComponent(); matchedIndices = new List<int>(new int[listBox.Items.Count]);
for (int i = 0; i < listBox.Items.Count; i++)
matchedIndices[i] = i;
} private void listBox_KeyPress(object sender, KeyPressEventArgs e)
{
_recordKeypressChar += e.KeyChar;//记录键盘按下的数字 string reg = string.Format("^{0}", _recordKeypressChar);//对比字符串;^表示开头
int i = 0;
do
{
if (!Regex.IsMatch(listBox.Items[matchedIndices[i]].ToString(), reg))
matchedIndices.RemoveAt(i);//排除法,留下符合条件的项的索引
else
i++;
}
while (i < matchedIndices.Count); if (matchedIndices.Count == 0)
{
for (int j = 0; j < listBox.Items.Count; j++)
matchedIndices.Add(j);
listBox.SelectedIndex = -1;
_recordKeypressChar = string.Empty;
}
else
listBox.SelectedIndex = matchedIndices[0]; e.Handled = true;
//foreach (int m in matchedIndices)
// Debug.WriteLine(m);
//Debug.WriteLine("键:" + _recordKeypressChar);
} }
public partial class Form1 : Form
{
private string _recordKeypressChar;
List <int> matchedIndices; public Form1()
{
InitializeComponent(); matchedIndices = new List <int>(new int[listBox.Items.Count]);
for (int i = 0; i < listBox.Items.Count; i++)
matchedIndices[i] = i;
} private void listBox_KeyPress(object sender, KeyPressEventArgs e)
{
_recordKeypressChar += e.KeyChar;//记录键盘按下的数字 string reg = string.Format("^{0}", _recordKeypressChar);//对比字符串;^表示开头
int i = 0;
do
{
if (!Regex.IsMatch(listBox.Items[matchedIndices[i]].ToString(), reg))
matchedIndices.RemoveAt(i);//排除法,留下符合条件的项的索引
else
i++;
}
while (i < matchedIndices.Count); if (matchedIndices.Count == 0)
{
for (int j = 0; j < listBox.Items.Count; j++)
matchedIndices.Add(j);
listBox.SelectedIndex = -1;
_recordKeypressChar = string.Empty;
}
else
listBox.SelectedIndex = matchedIndices[0]; e.Handled = true;
//foreach (int m in matchedIndices)
// Debug.WriteLine(m);
//Debug.WriteLine("键:" + _recordKeypressChar);
} }
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;namespace mc1000_lgj
{
public partial class ReadBarCode : Form
{
public ReadBarCode()
{
InitializeComponent();
} private void ReadBarCode_Load(object sender, EventArgs e)
{
if (this.InitReader())
{
this.StartRead();
}
else
{
this.Close();
return;
}
} protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
} private bool InitReader()
{
listBox1.Items.Clear();
string strContent;
FileStream fsMyfile = new FileStream(@"\myfile.txt", FileMode.OpenOrCreate , FileAccess.Read );
StreamReader sr = new StreamReader(fsMyfile);
while (sr.Peek() >= 0)
{
strContent = sr.ReadLine();
listBox1.Items.Add(strContent);
}
sr.Close();
listBox1.SelectedIndex = 0;
this.listBox1.Focus(); this.Activated += new EventHandler(ReadBarCode_Activated);
this.Deactivate += new EventHandler(ReadBarCode_Deactivate); return true;
} private void ReadBarCode_Deactivate(object sender, EventArgs e)
{
this.StopRead();
} private void ReadBarCode_Activated(object sender, EventArgs e)
{
if (!this.MyReaderData.IsPending)
this.StartRead();
} private void listBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
textBox2.SelectAll();
textBox2.Focus();
}
}
}
}
这个是我的代码,当我用到e.SuppressKeyPresst和listbox1.SelectedIndex = listbox1.FindString(InputString)时,在e或者listbox1之后敲点之后不出现SuppressKeyPresst或FindString()方法或属性啊?
我装的是VS2005的完整版,应该不会是.NET Compact Framework