基本信息
源码名称:C# winform 输入提示(自动完成)例子源码下载
源码大小:0.09M
文件格式:.zip
开发语言:C#
更新时间:2016-07-27
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
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.IO;
namespace Demo
{
public partial class Demo : Form
{
public Demo()
{
InitializeComponent();
}
/// <summary>
/// 始发站 文本框 键盘按下松开事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static TextBox txtStation_Name, txtStation_Value;
private static ListBox ltb_Stations;
/// <summary>
/// 站点文本框 键盘按下松开事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtStation_KeyUp(object sender, KeyEventArgs e)
{
TextBox eObj = sender as TextBox; //事件源对象
txtStation_Name = eObj; //当前事件出发对象
if (eObj.Name == "txtStation_S_Name")
{
txtStation_Value = this.txtStation_S_Value; //保存值的textbox
ltb_Stations = this.lb_Start_Stations; //始发站 展示数据的
}
else
{
//到站 控件
txtStation_Value = this.txtStation_E_Value; //保存值的textbox
ltb_Stations = this.lb_End_Stations; //始发站 展示数据的
}
//上下左右
if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Left)
{
if (ltb_Stations.SelectedIndex > 0)
ltb_Stations.SelectedIndex--;
}
else if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Right)
{
if (ltb_Stations.SelectedIndex < ltb_Stations.Items.Count - 1)
ltb_Stations.SelectedIndex ;
}
//回车
else if (e.KeyCode == Keys.Enter)
{
StationInfo info = ltb_Stations.SelectedItem as StationInfo;
txtStation_Name.Text = info.StationName_CN;
txtStation_Value.Text = info.StationValue;
ltb_Stations.Visible = false;
}
else
{
if (txtStation_Name.Text != "")
{
IList<StationInfo> dataSource = StationInfo.GetStations(txtStation_Name.Text.Trim());
if (dataSource.Count > 0)
{
ltb_Stations.DataSource = dataSource;
ltb_Stations.DisplayMember = "StationName_CN";
ltb_Stations.ValueMember = "StationValue";
ltb_Stations.Visible = true;
}
else
ltb_Stations.Visible = false;
}
else
{
ltb_Stations.Visible = false;
}
}
txtStation_Name.Select(txtStation_Name.Text.Length, 1); //光标定位到文本框最后
}
/// <summary>
/// 展示站点列表的listbox的点击事件,为了给textbox赋值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ListBox_StationDatas_Click(object sender, EventArgs e)
{
ListBox eObj = sender as ListBox;
StationInfo info = eObj.SelectedItem as StationInfo;
txtStation_Name.Text = info.StationName_CN;
txtStation_Value.Text = info.StationValue;
eObj.Visible = false;
txtStation_Name.Select(txtStation_Name.Text.Length, 1); //光标定位到最后
}
/// <summary>
/// 展示站点列表的listbox, 鼠标在该控件上移动事件,
/// 为了鼠标移动选项
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ListBox_StationDatas_MouseMove(object sender, MouseEventArgs e)
{
ListBox eObj = sender as ListBox;
eObj.SelectedIndex = eObj.IndexFromPoint(e.Location);
}
/// <summary>
/// 展示站点列表的listbox, 各项绘制,为了 可以调整 Item的高度
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ListBox_StationDatas_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
StationInfo info = (sender as ListBox).Items[e.Index] as StationInfo;
e.Graphics.DrawString(info.StationName_CN, e.Font, new SolidBrush(e.ForeColor),
e.Bounds);
}
private void txtStation_S_Name_Enter(object sender, EventArgs e)
{
lb_Start_Stations.Visible = false;
lb_End_Stations.Visible = false;
}
}
/// <summary>
/// 站点信息
/// </summary>
public class StationInfo
{
/// <summary>
/// 站点名称 - 中文
/// </summary>
public string StationName_CN { get; set; }
/// <summary>
/// 站点名称 - 英文
/// </summary>
public string StationName_EN { get; set; }
/// <summary>
/// 站点名称 - 简写
/// </summary>
public string StationName_JX { get; set; }
/// <summary>
/// 站点的值
/// </summary>
public string StationValue { get; set; }
/// <summary>
/// 模糊查询站点
/// </summary>
/// <param name="filter"></param>
/// <returns></returns>
private static IList<StationInfo> StationData;
public static IList<StationInfo> GetStations(string filter)
{
IList<StationInfo> results = new List<StationInfo>();
if (StationData == null)
{
string stations = GetAllStations();
string[] datas = stations.Split('@');
foreach (var item in datas)
{
string[] tempArray = item.Split('|');
StationInfo info = new StationInfo()
{
StationName_CN = tempArray[0],
StationValue = tempArray[1],
StationName_EN = tempArray[2],
StationName_JX = tempArray[3]
};
results.Add(info);
}
StationData = results;
}
else
{
results = StationData;
}
return results.Where(
f =>
(f.StationName_CN.Length >= filter.Length && f.StationName_CN.Substring(0, filter.Length) == filter) ||
(f.StationName_EN.Length >= filter.Length && f.StationName_EN.Substring(0, filter.Length) == filter) ||
(f.StationName_JX.Length >= filter.Length && f.StationName_JX.Substring(0, filter.Length) == filter) ||
(f.StationValue.Length >= filter.Length && f.StationValue.Substring(0, filter.Length) == filter)).ToList<StationInfo>();
}
/// <summary>
/// 读取站点数据文件
/// </summary>
/// <returns></returns>
public static string GetAllStations()
{
string stationStrs;
try
{
FileStream fileStream = new FileStream(@"Resource\station_data.txt", FileMode.Open);
StreamReader sr = new StreamReader(fileStream, UnicodeEncoding.GetEncoding("GB2312"));
stationStrs = sr.ReadToEnd().TrimEnd('@');
sr.Close();
fileStream.Close();
return stationStrs;
}
catch (IOException ex)
{
return "站点文件读取失败!";
}
}
}
}