• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
张勇涛
博客园    首页    新随笔    联系   管理    订阅  订阅

用txt做数据源和winfrom textbox输入智能提示

百度输入提示

1.用txt做数据源连接字符串

//text作为数据源的链接字符串
            string strConnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c://test;
Extended Properties=""text;HDR=Yes;FMT=Delimited"";";

 

注意:c://test是个文件夹,里面有相应的txt文档做数据源。

 

2.winfrom textbox输入智能提示

核心代码:

//下面3行用设置智能提示
            this.textBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
            this.textBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.CustomSource;
            this.textBox1.AutoCompleteCustomSource = ac;//设置数据源

其中     AutoCompleteStringCollection ac = new AutoCompleteStringCollection();

 

 

全部代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace TestAsSource
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        AutoCompleteStringCollection ac = new AutoCompleteStringCollection();

        private void Form1_Load(object sender, EventArgs e)
        {
            //下面3行用设置智能提示
            this.textBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
            this.textBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.CustomSource;
            this.textBox1.AutoCompleteCustomSource = ac;//设置数据源

            //text作为数据源的链接字符串
            string strConnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c://test;
Extended Properties=""text;HDR=Yes;FMT=Delimited"";";

            using (OleDbConnection objConnection = new OleDbConnection(strConnection))
            {
                objConnection.Open();

                //b.txt是c://test下面的一个文件,如果是a.txt ,只要把下面3处b.txt 换成a.txt就可以了
                OleDbDataAdapter myCommandd = new OleDbDataAdapter("select * from b.txt", objConnection);
                DataSet ds = new DataSet();
                myCommandd.Fill(ds, "b_txt");
                DataTable dt = ds.Tables["b_txt"];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ac.Add(dt.Rows[i][0].ToString());
                }

                objConnection.Close();
            }
            
        }
    }
}

posted @ 2011-10-13 20:29  张勇涛  阅读(567)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3