【发布时间】:2010-11-24 09:04:18
【问题描述】:
我想要一个文本框控件,它可以在使用 C# 2008 和 LINQ 的 Windows 应用程序中建议和附加数据库中的值。
我用组合框来做,但我不能用文本框来做。
我该怎么做?
【问题讨论】:
-
对不起,我没有看到您使用的是 Windows 应用程序。
标签: c# winforms autocomplete
我想要一个文本框控件,它可以在使用 C# 2008 和 LINQ 的 Windows 应用程序中建议和附加数据库中的值。
我用组合框来做,但我不能用文本框来做。
我该怎么做?
【问题讨论】:
标签: c# winforms autocomplete
您可以附加到 KeyDown 事件,然后在数据库中查询用户已经输入的那部分文本。例如,如果用户输入“T”,则搜索以“T”开头的事物。然后,当他们输入下一个字母(例如“e”)时,在表格中搜索以“Te”开头的内容。
例如,可用项目可以显示在“浮动”列表框中。您需要将 ListBox 放在 TextBox 下方,以便他们可以看到可用的条目,然后在他们完成输入后移除 ListBox。
【讨论】:
查看AutoCompleteSource、AutoCompleteCustomSource 和AutoCompleteMode 属性。
textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
AutoCompleteStringCollection col = new AutoCompleteStringCollection();
col.Add("Foo");
col.Add("Bar");
textBox1.AutoCompleteCustomSource = col;
请注意,设计器允许您在不编写任何代码的情况下做到这一点...
【讨论】:
当然这取决于你如何实现它,但也许这是一个好的开始:
using System.Windows.Forms;
public class AutoCompleteTextBox : TextBox {
private string[] database;//put here the strings of the candidates of autocomplete
private bool changingText = false;
protected override void OnTextChanged (EventArgs e) {
if(!changingText && database != null) {
//searching the first candidate
string typed = this.Text.Substring(0,this.SelectionStart);
string candidate = null;
for(int i = 0; i < database.Length; i++)
if(database[i].Substring(0,this.SelectionStart) == typed) {
candidate = database[i].Substring(this.SelectionStart,database[i].Length);
break;
}
if(candidate != null) {
changingText = true;
this.Text = typed+candidate;
this.SelectionStart = typed.Length;
this.SelectionLength = candidate.Length;
}
}
else if(changingText)
changingText = false;
base.OnTextChanged(e);
}
}
我不确定这是否工作得很好,但我认为这段代码的基础已经足够好了。
【讨论】:
这可能不是最好的做事方式,但应该可以:
this.textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
this.textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
private void textBox1_TextChanged(object sender, EventArgs e)
{
TextBox t = sender as TextBox;
if (t != null)
{
//say you want to do a search when user types 3 or more chars
if (t.Text.Length >= 3)
{
//SuggestStrings will have the logic to return array of strings either from cache/db
string[] arr = SuggestStrings(t.Text);
AutoCompleteStringCollection collection = new AutoCompleteStringCollection();
collection.AddRange(arr);
this.textBox1.AutoCompleteCustomSource = collection;
}
}
}
【讨论】:
System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'
private void textBox1_TextChanged(object sender, EventArgs e)
{
try
{
textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
AutoCompleteStringCollection col = new AutoCompleteStringCollection();
con.Open();
sql = "select *from Table_Name;
cmd = new SqlCommand(sql, con);
SqlDataReader sdr = null;
sdr = cmd.ExecuteReader();
while (sdr.Read())
{
col.Add(sdr["Column_Name"].ToString());
}
sdr.Close();
textBox1.AutoCompleteCustomSource = col;
con.Close();
}
catch
{
}
}
【讨论】:
To AutoComplete TextBox Control in C#.net windows application using
wamp mysql database...
here is my code..
AutoComplete();
write this **AutoComplete();** text in form-load event..
private void Autocomplete()
{
try
{
MySqlConnection cn = new MySqlConnection("server=localhost;
database=databasename;user id=root;password=;charset=utf8;");
cn.Open();
MySqlCommand cmd = new MySqlCommand("SELECT distinct Column_Name
FROM table_Name", cn);
DataSet ds = new DataSet();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(ds, "table_Name");
AutoCompleteStringCollection col = new
AutoCompleteStringCollection();
int i = 0;
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
col.Add(ds.Tables[0].Rows[i]["Column_Name"].ToString());
}
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBox1.AutoCompleteCustomSource = col;
textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
cn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
【讨论】:
You can add a parameter in the query like @emailadd to be added in the aspx.cs file where the Stored Procedure is called with cmd.Parameter.AddWithValue.
The trick is that the @emailadd parameter doesn't exist in the table design of the select query, but being added and inserted in the table.
USE [DRDOULATINSTITUTE]
GO
/****** Object: StoredProcedure [dbo].[ReikiInsertRow] Script Date: 5/18/2016 11:12:33 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[ReikiInsertRow]
@Reiki varchar(100),
@emailadd varchar(50)
as
insert into dbo.ReikiPowerDisplay
select Reiki,ReikiDescription, @emailadd from ReikiPower
where Reiki=@Reiki;
Posted By: Aneel Goplani. CIS. 2002. USA
【讨论】:
有两种方法可以实现这个文本框效果:
使用图形用户界面 (GUI);或使用代码
使用图形用户界面:
转到:“属性”标签;然后设置以下属性:
但是;最好的方法是通过代码创建它。 请参阅下面的示例。
AutoCompleteStringCollection sourceName = new AutoCompleteStringCollection();
foreach (string name in listNames)
{
sourceName.Add(name);
}
txtName.AutoCompleteCustomSource = sourceName;
txtName.AutoCompleteMode = AutoCompleteMode.Suggest;
txtName.AutoCompleteSource = AutoCompleteSource.CustomSource;
【讨论】:
private void TurnOnAutocomplete()
{
textBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
AutoCompleteStringCollection collection = new AutoCompleteStringCollection();
string[] arrayOfWowrds = new string[];
try
{
//Read in data Autocomplete list to a string[]
string[] arrayOfWowrds = new string[];
}
catch (Exception err)
{
MessageBox.Show(err.Message, "File Missing", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
collection.AddRange(arrayOFWords);
textBox.AutoCompleteCustomSource = collection;
}
您只需在获得自动完成列表所需的数据后调用一次。一旦绑定它就留在文本框。您不需要或不想在每次更改文本框中的文本时调用它,这会杀死您的程序。
【讨论】: