代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
WinForm TextBox猜想输入和历史记录输入
作者:
zhaoblogs
/ 发布于
2011/12/7
/
635
当我们在使用查询应用时,输入某个关键字后文本框下面就会自动弹出跟此关键字相附的词组供我们选择,为我们节省了输入的时间,这就是猜想输入
<div><strong>AutoCompleteMode</strong> 获取或设置控制自动完成如何作用于TextBox的选项。 <strong>属性值</strong> <strong><span class="unresolvedLink">Append </span></strong>将最可能的候选字符串的其余部分追加到现有的字符,并突出显示追加的字符。 <strong><span class="unresolvedLink">Suggest </span></strong><span id="xn5_3620b7e19b33a5d027504f94dbad298c" class="sentence">显示与编辑控件关联的辅助下拉列表。</span> <span id="xn6_fb418939926624672a085477f99e5251" class="sentence">此下拉列表填充了一个或多个建议完成字符串。</span> <strong><span class="unresolvedLink">SuggestAppend </span></strong>追加 <span class="input">Suggest</span> 和 <span class="input">Append</span> 选项。 <strong><span class="unresolvedLink">None </span></strong><span id="xn8_9d9e07b4f2e4225fb1b272fac5f803d9" class="sentence">禁用自动完成</span> <span id="xn9_63875f1e6ab973e1c90612022d7d4d93" class="sentence">这是默认值。</span> <strong>AutoCompleteSource </strong>获取或设置一个值,该值指定用于自动完成的完整字符串的源。 <strong>AutoCompleteCustomSource</strong> <span>获取或设置在 </span><span>AutoCompleteSource</span><span> 属性设置为 </span><span><span class="input">CustomSource</span></span><span> 时使用的自定义 </span><span>System.Collections.Specialized<span>.</span>StringCollection</span><span>。</span> <span> </span> <span>如下图:</span> <img alt="" src="http://pic002.cnblogs.com/images/2011/205276/2011120711100279.jpg" /> <strong>代码设计</strong> <hr /> <strong>TextBoxRemind</strong> <div class="cnblogs_Highlighter"> <div> <div id="highlighter_729010" class="syntaxhighlighter collapsed nogutter csharp ie"> <div class="toolbar"><span><a class="toolbar_item command_expandSource expandSource" href="http://www.cnblogs.com/zhaoblogs/archive/2011/12/07/2278938.html#">expand source</a></span><span><a class="toolbar_item command_help help" href="http://www.cnblogs.com/zhaoblogs/archive/2011/12/07/2278938.html#">?</a></span></div> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="code"> <div class="container"> <div class="line number1 index0 alt2">"csharp keyword"><font color="#0000ff">public</font> "csharp keyword"><font color="#0000ff">class</font> "csharp plain"><font color="#000000">TextBoxRemind</font></div> <div class="line number2 index1 alt1">"csharp plain"><font color="#000000">{</font></div> <div class="line number3 index2 alt2">"csharp keyword"><font color="#0000ff">private</font> "csharp keyword"><font color="#0000ff">string</font>"csharp plain"><font color="#000000">[] array = </font>"csharp keyword"><font color="#0000ff">null</font>"csharp plain"><font color="#000000">;</font></div> <div class="line number4 index3 alt1">"csharp keyword"><font color="#0000ff">public</font> "csharp keyword"><font color="#0000ff">void</font> "csharp plain"><font color="#000000">InitAutoCompleteCustomSource(TextBox textBox)</font></div> <div class="line number5 index4 alt2">"csharp plain"><font color="#000000">{</font></div> <div class="line number6 index5 alt1">"csharp plain"><font color="#000000">array = ReadTxt();</font></div> <div class="line number7 index6 alt2">"csharp keyword"><font color="#0000ff">if</font> "csharp plain"><font color="#000000">(array != </font>"csharp keyword"><font color="#0000ff">null</font> "csharp plain"><font color="#000000">&& array.Length > 0)</font></div> <div class="line number8 index7 alt1">"csharp plain"><font color="#000000">{</font></div> <div class="line number9 index8 alt2">"csharp plain"><font color="#000000">AutoCompleteStringCollection ACSC = </font>"csharp keyword"><font color="#0000ff">new</font> "csharp plain"><font color="#000000">AutoCompleteStringCollection();</font></div> <div class="line number10 index9 alt1"></div> <div class="line number11 index10 alt2">"csharp keyword"><font color="#0000ff">for</font> "csharp plain"><font color="#000000">(</font>"csharp keyword"><font color="#0000ff">int</font> "csharp plain"><font color="#000000">i = 0; i < array.Length; i++)</font></div> <div class="line number12 index11 alt1">"csharp plain"><font color="#000000">{</font></div> <div class="line number13 index12 alt2">"csharp plain"><font color="#000000">ACSC.Add(array[i]);</font></div> <div class="line number14 index13 alt1">"csharp plain"><font color="#000000">}</font></div> <div class="line number15 index14 alt2"></div> <div class="line number16 index15 alt1">"csharp plain"><font color="#000000">textBox.AutoCompleteCustomSource = ACSC;</font></div> <div class="line number17 index16 alt2">"csharp plain"><font color="#000000">}</font></div> <div class="line number18 index17 alt1">"csharp plain"><font color="#000000">}</font></div> <div class="line number19 index18 alt2"></div> <div class="line number20 index19 alt1">"csharp keyword"><font color="#0000ff">string</font>"csharp plain"><font color="#000000">[] ReadTxt()</font></div> <div class="line number21 index20 alt2">"csharp plain"><font color="#000000">{</font></div> <div class="line number22 index21 alt1">"csharp keyword"><font color="#0000ff">try</font></div> <div class="line number23 index22 alt2">"csharp plain"><font color="#000000">{</font></div> <div class="line number24 index23 alt1">"csharp keyword"><font color="#0000ff">if</font> "csharp plain"><font color="#000000">(!File.Exists(</font>"csharp string"><font color="#0000ff">"Remind.txt"</font>"csharp plain"><font color="#000000">))</font></div> <div class="line number25 index24 alt2">"csharp plain"><font color="#000000">File.Create(</font>"csharp string"><font color="#0000ff">"Remind.txt"</font>"csharp plain"><font color="#000000">);</font></div> <div class="line number26 index25 alt1"></div> <div class="line number27 index26 alt2">"csharp keyword"><font color="#0000ff">return</font> "csharp plain"><font color="#000000">File.ReadAllLines(</font>"csharp string"><font color="#0000ff">"Remind.txt"</font>"csharp plain"><font color="#000000">, Encoding.Default);</font></div> <div class="line number28 index27 alt1">"csharp plain"><font color="#000000">}</font></div> <div class="line number29 index28 alt2">"csharp keyword"><font color="#0000ff">catch</font></div> <div class="line number30 index29 alt1">"csharp plain"><font color="#000000">{</font></div> <div class="line number31 index30 alt2">"csharp keyword"><font color="#0000ff">return</font> "csharp keyword"><font color="#0000ff">null</font>"csharp plain"><font color="#000000">;</font></div> <div class="line number32 index31 alt1">"csharp plain"><font color="#000000">}</font></div> <div class="line number33 index32 alt2">"csharp plain"><font color="#000000">}</font></div> <div class="line number34 index33 alt1"></div> <div class="line number35 index34 alt2">"csharp keyword"><font color="#0000ff">public</font> "csharp keyword"><font color="#0000ff">void</font> "csharp plain"><font color="#000000">Remind(</font>"csharp keyword"><font color="#0000ff">string</font> "csharp plain"><font color="#000000">str)</font></div> <div class="line number36 index35 alt1">"csharp plain"><font color="#000000">{</font></div> <div class="line number37 index36 alt2">"csharp plain"><font color="#000000">StreamWriter writer = </font>"csharp keyword"><font color="#0000ff">null</font>"csharp plain"><font color="#000000">;</font></div> <div class="line number38 index37 alt1">"csharp keyword"><font color="#0000ff">try</font></div> <div class="line number39 index38 alt2">"csharp plain"><font color="#000000">{</font></div> <div class="line number40 index39 alt1">"csharp keyword"><font color="#0000ff">if</font> "csharp plain"><font color="#000000">(array != </font>"csharp keyword"><font color="#0000ff">null</font> "csharp plain"><font color="#000000">&& !array.Contains(str))</font></div> <div class="line number41 index40 alt2">"csharp plain"><font color="#000000">{</font></div> <div class="line number42 index41 alt1">"csharp plain"><font color="#000000">writer = </font>"csharp keyword"><font color="#0000ff">new</font> "csharp plain"><font color="#000000">StreamWriter(</font>"csharp string"><font color="#0000ff">"Remind.txt"</font>"csharp plain"><font color="#000000">, </font>"csharp keyword"><font color="#0000ff">true</font>"csharp plain"><font color="#000000">, Encoding.Default);</font></div> <div class="line number43 index42 alt2">"csharp plain"><font color="#000000">writer.WriteLine(str);</font></div> <div class="line number44 index43 alt1">"csharp plain"><font color="#000000">}</font></div> <div class="line number45 index44 alt2">"csharp plain"><font color="#000000">}</font></div> <div class="line number46 index45 alt1">"csharp keyword"><font color="#0000ff">finally</font></div> <div class="line number47 index46 alt2">"csharp plain"><font color="#000000">{</font></div> <div class="line number48 index47 alt1">"csharp keyword"><font color="#0000ff">if</font> "csharp plain"><font color="#000000">(writer != </font>"csharp keyword"><font color="#0000ff">null</font>"csharp plain"><font color="#000000">)</font></div> <div class="line number49 index48 alt2">"csharp plain"><font color="#000000">{</font></div> <div class="line number50 index49 alt1">"csharp plain"><font color="#000000">writer.Close();</font></div> <div class="line number51 index50 alt2">"csharp plain"><font color="#000000">writer = </font>"csharp keyword"><font color="#0000ff">null</font>"csharp plain"><font color="#000000">;</font></div> <div class="line number52 index51 alt1">"csharp plain"><font color="#000000">}</font></div> <div class="line number53 index52 alt2">"csharp plain"><font color="#000000">}</font></div> <div class="line number54 index53 alt1">"csharp plain"><font color="#000000">}</font></div> <div class="line number55 index54 alt2">"csharp plain"><font color="#000000">}</font></div> </div> </td> </tr> </tbody> </table> </div> </div> </div> <strong>历史记录</strong> 由于存储的都是文本数据,所以采用了文本文档进行存储,轻量级操作方便,系统会自动创建Remind.txt文件。<!--EndFragment--> 加载窗体时首先初始化InitAutoCompleteCustomSource,添加AutoCompleteStringCollection,当我们点击确认后重新初始化数据源,加载之前录入的关键字。 <div class="cnblogs_Highlighter"> <div> <div id="highlighter_685719" class="syntaxhighlighter collapsed nogutter csharp ie"> <div class="toolbar"><span><a class="toolbar_item command_expandSource expandSource" href="http://www.cnblogs.com/zhaoblogs/archive/2011/12/07/2278938.html#">expand source</a></span><span><a class="toolbar_item command_help help" href="http://www.cnblogs.com/zhaoblogs/archive/2011/12/07/2278938.html#">?</a></span></div> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="code"> <div class="container"> <div class="line number1 index0 alt2">"csharp keyword"><font color="#0000ff">public</font> "csharp keyword"><font color="#0000ff">partial</font> "csharp keyword"><font color="#0000ff">class</font> "csharp plain"><font color="#000000">Form1 : Form</font></div> <div class="line number2 index1 alt1">"csharp plain"><font color="#000000">{</font></div> <div class="line number3 index2 alt2">"csharp keyword"><font color="#0000ff">private</font> "csharp plain"><font color="#000000">TextBoxRemind remind = </font>"csharp keyword"><font color="#0000ff">null</font>"csharp plain"><font color="#000000">;</font></div> <div class="line number4 index3 alt1">"csharp keyword"><font color="#0000ff">public</font> "csharp plain"><font color="#000000">Form1()</font></div> <div class="line number5 index4 alt2">"csharp plain"><font color="#000000">{</font></div> <div class="line number6 index5 alt1">"csharp plain"><font color="#000000">InitializeComponent();</font></div> <div class="line number7 index6 alt2">"csharp plain"><font color="#000000">remind = </font>"csharp keyword"><font color="#0000ff">new</font> "csharp plain"><font color="#000000">TextBoxRemind();</font></div> <div class="line number8 index7 alt1">"csharp plain"><font color="#000000">InitTextBoxRemind();</font></div> <div class="line number9 index8 alt2">"csharp plain"><font color="#000000">}</font></div> <div class="line number10 index9 alt1"></div> <div class="line number11 index10 alt2">"csharp keyword"><font color="#0000ff">void</font> "csharp plain"><font color="#000000">InitTextBoxRemind()</font></div> <div class="line number12 index11 alt1">"csharp plain"><font color="#000000">{</font></div> <div class="line number13 index12 alt2">"csharp plain"><font color="#000000">remind.InitAutoCompleteCustomSource(textBox1);</font></div> <div class="line number14 index13 alt1">"csharp plain"><font color="#000000">}</font></div> <div class="line number15 index14 alt2"></div> <div class="line number16 index15 alt1">"csharp keyword"><font color="#0000ff">private</font> "csharp keyword"><font color="#0000ff">void</font> "csharp plain"><font color="#000000">button1_Click(</font>"csharp keyword"><font color="#0000ff">object</font> "csharp plain"><font color="#000000">sender, EventArgs e)</font></div> <div class="line number17 index16 alt2">"csharp plain"><font color="#000000">{</font></div> <div class="line number18 index17 alt1">"csharp keyword"><font color="#0000ff">if</font> "csharp plain"><font color="#000000">(textBox1.Text.Trim() != </font>"csharp string"><font color="#0000ff">""</font>"csharp plain"><font color="#000000">)</font></div> <div class="line number19 index18 alt2">"csharp plain"><font color="#000000">{</font></div> <div class="line number20 index19 alt1">"csharp plain"><font color="#000000">remind.Remind(textBox1.Text.Trim());</font></div> <div class="line number21 index20 alt2">"csharp plain"><font color="#000000">InitTextBoxRemind();</font></div> <div class="line number22 index21 alt1">"csharp plain"><font color="#000000">}</font></div> <div class="line number23 index22 alt2">"csharp plain"><font color="#000000">}</font></div> <div class="line number24 index23 alt1">"csharp plain"><font color="#000000">}</font></div> </div> </td> </tr> </tbody> </table> </div> </div> </div> <strong>最后</strong> <hr /> 大家经常遇到使用StreamReader和StreamWriter时中文不能正常显示,一个简单的方法就是将Encoding设置为Default即可。 </div>
试试其它关键字
猜想输入
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
zhaoblogs
贡献的其它代码
(
1
)
.
WinForm TextBox猜想输入和历史记录输入
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3