根据窗体自动调整控件及文本框记住上次填写内容Demo
第一次写文章,组词难免没有不通之处。。。
最近常用到Winform根据窗体大小自动调整空间大小及字体、文本框记住上次填写内容待下次输入某一段时候自动跳出上次输入内容。于是就随便把两个问题放到同一个demo上。
一、运行效果如下:
1、 启动时:

2、改变窗体大小时:


3、输入文本时:


二、代码:
1、缩放代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO; namespace myListSelect
{
public partial class ListSelect : UserControl
{
TextBox tbx; public ListSelect()
{
InitializeComponent(); this.tbxInputBox.TextChanged += new EventHandler(tbxInputBox_TextChanged);
this.tbxInputBox.KeyUp += new KeyEventHandler(tbxInputBox_KeyUp);
this.tbxInputBox.DoubleClick += new EventHandler(tbxInputBox_DoubleClick);
this.tbxInputBox.KeyDown += new KeyEventHandler(tbxInputBox_KeyDown);
this.lbxListSelect.MouseClick += new MouseEventHandler(lbxListSelect_MouseClick);
this.lbxListSelect.MouseMove += new MouseEventHandler(lbxListSelect_MouseMove); } void lbxListSelect_MouseMove(object sender, MouseEventArgs e)
{
lbxListSelect.SelectedIndex = lbxListSelect.IndexFromPoint(new Point(e.X, e.Y));
} void lbxListSelect_MouseClick(object sender, MouseEventArgs e)
{
try
{
if (tbx != null)
{
tbxInputBox.Text = lbxListSelect.Text.ToString();
lbxListSelect.Visible = false;
}
}
catch (Exception)
{
return;
}
} void tbxInputBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//记录本次所写内容
if (!File.Exists("listSelectSetup.ini"))
{
File.WriteAllText("listSelectSetup.ini", tbxInputBox.Text + "\r\n");
}
else
{
//读出所有行 string[] fileTextStr = File.ReadAllLines("listSelectSetup.ini"); if (!fileTextStr.Contains(tbxInputBox.Text))
{
File.AppendAllText("listSelectSetup.ini", tbxInputBox.Text + "\r\n");
}
}
}
} void tbxInputBox_DoubleClick(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(tbxInputBox.Text))
{
DisplayOldInput(tbxInputBox.Text, tbxInputBox);
}
} void tbxInputBox_KeyUp(object sender, KeyEventArgs e)
{
if (lbxListSelect.Items.Count < )
{
return;
}
if (e.KeyCode == Keys.Up)
{
int idx = lbxListSelect.SelectedIndex;
if (idx == -)
{ lbxListSelect.SelectedItem = lbxListSelect.Items[lbxListSelect.Items.Count - ];
}
else
{
if (idx == )
{
lbxListSelect.SelectedItem = lbxListSelect.Items[lbxListSelect.Items.Count - ];
idx = lbxListSelect.Items.Count;
}
lbxListSelect.SelectedItem = lbxListSelect.Items[idx - ];
}
}
else if (e.KeyCode == Keys.Down)
{ int idx = lbxListSelect.SelectedIndex;
if (idx == -)
{ lbxListSelect.SelectedItem = lbxListSelect.Items[]; }
else
{
if (idx == lbxListSelect.Items.Count - )
{
lbxListSelect.SelectedItem = lbxListSelect.Items[];
}
else
{
lbxListSelect.SelectedItem = lbxListSelect.Items[idx + ];
}
}
idx = lbxListSelect.SelectedIndex;
}
else if (e.KeyCode == Keys.Enter)
{
try
{
if (tbx != null)
{
tbxInputBox.Text = lbxListSelect.Text.ToString();
lbxListSelect.Visible = false;
}
}
catch (Exception)
{
return;
}
}
} void tbxInputBox_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(tbxInputBox.Text))
{
DisplayOldInput(tbxInputBox.Text, tbxInputBox);
}
} private void DisplayOldInput(string str, TextBox tb)
{
List<string> list = new List<string>();
List<string> listSource = new List<string>(); if (File.Exists("listSelectSetup.ini"))
{
list.AddRange(File.ReadAllLines("listSelectSetup.ini"));
listSource=list.FindAll((line) => { return line.IndexOf(str) == ; });
}
Point p = lbxListSelect.Location;
p.X = tb.Location.X;
lbxListSelect.Location = p;
tbx = tb;
lbxListSelect.DataSource = listSource;
if (listSource != null && listSource.Count != )
{
lbxListSelect.Visible = true;
} }
}
}
2、显示上次输入文本代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO; namespace myListSelect
{
public partial class ListSelect : UserControl
{
TextBox tbx; public ListSelect()
{
InitializeComponent(); this.tbxInputBox.TextChanged += new EventHandler(tbxInputBox_TextChanged);
this.tbxInputBox.KeyUp += new KeyEventHandler(tbxInputBox_KeyUp);
this.tbxInputBox.DoubleClick += new EventHandler(tbxInputBox_DoubleClick);
this.tbxInputBox.KeyDown += new KeyEventHandler(tbxInputBox_KeyDown);
this.lbxListSelect.MouseClick += new MouseEventHandler(lbxListSelect_MouseClick);
this.lbxListSelect.MouseMove += new MouseEventHandler(lbxListSelect_MouseMove); } void lbxListSelect_MouseMove(object sender, MouseEventArgs e)
{
lbxListSelect.SelectedIndex = lbxListSelect.IndexFromPoint(new Point(e.X, e.Y));
} void lbxListSelect_MouseClick(object sender, MouseEventArgs e)
{
try
{
if (tbx != null)
{
tbxInputBox.Text = lbxListSelect.Text.ToString();
lbxListSelect.Visible = false;
}
}
catch (Exception)
{
return;
}
} void tbxInputBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//记录本次所写内容
if (!File.Exists("listSelectSetup.ini"))
{
File.WriteAllText("listSelectSetup.ini", tbxInputBox.Text + "\r\n");
}
else
{
//读出所有行 string[] fileTextStr = File.ReadAllLines("listSelectSetup.ini"); if (!fileTextStr.Contains(tbxInputBox.Text))
{
File.AppendAllText("listSelectSetup.ini", tbxInputBox.Text + "\r\n");
}
}
}
} void tbxInputBox_DoubleClick(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(tbxInputBox.Text))
{
DisplayOldInput(tbxInputBox.Text, tbxInputBox);
}
} void tbxInputBox_KeyUp(object sender, KeyEventArgs e)
{
if (lbxListSelect.Items.Count < )
{
return;
}
if (e.KeyCode == Keys.Up)
{
int idx = lbxListSelect.SelectedIndex;
if (idx == -)
{ lbxListSelect.SelectedItem = lbxListSelect.Items[lbxListSelect.Items.Count - ];
}
else
{
if (idx == )
{
lbxListSelect.SelectedItem = lbxListSelect.Items[lbxListSelect.Items.Count - ];
idx = lbxListSelect.Items.Count;
}
lbxListSelect.SelectedItem = lbxListSelect.Items[idx - ];
}
}
else if (e.KeyCode == Keys.Down)
{ int idx = lbxListSelect.SelectedIndex;
if (idx == -)
{ lbxListSelect.SelectedItem = lbxListSelect.Items[]; }
else
{
if (idx == lbxListSelect.Items.Count - )
{
lbxListSelect.SelectedItem = lbxListSelect.Items[];
}
else
{
lbxListSelect.SelectedItem = lbxListSelect.Items[idx + ];
}
}
idx = lbxListSelect.SelectedIndex;
}
else if (e.KeyCode == Keys.Enter)
{
try
{
if (tbx != null)
{
tbxInputBox.Text = lbxListSelect.Text.ToString();
lbxListSelect.Visible = false;
}
}
catch (Exception)
{
return;
}
}
} void tbxInputBox_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(tbxInputBox.Text))
{
DisplayOldInput(tbxInputBox.Text, tbxInputBox);
}
} private void DisplayOldInput(string str, TextBox tb)
{
List<string> list = new List<string>();
List<string> listSource = new List<string>(); if (File.Exists("listSelectSetup.ini"))
{
list.AddRange(File.ReadAllLines("listSelectSetup.ini"));
listSource=list.FindAll((line) => { return line.IndexOf(str) == ; });
}
Point p = lbxListSelect.Location;
p.X = tb.Location.X;
lbxListSelect.Location = p;
tbx = tb;
lbxListSelect.DataSource = listSource;
if (listSource != null && listSource.Count != )
{
lbxListSelect.Visible = true;
} }
}
}
三、源码下载:
根据窗体自动调整控件及文本框记住上次填写内容Demo的更多相关文章
- 用MVC的辅助方法自定义了两个控件:“可编辑的下拉框控件”和“文本框日历控件”
接触MVC也没多长时间,一开始学的时候绝得MVC结构比较清晰.后来入了门具体操作下来感觉MVC控件怎么这么少还不可以像ASP.net form那样拖拽.这样设计界面来,想我种以前没学过JS,Jquer ...
- 在GridView控件内文本框实现TextChanged事件
本篇是教你实现GridView控件内的TextBox文本框实现自身的TextChanged事件.由于某些功能的需求,GridView控件内嵌TextBox,当TextBox值发生变化时,触发TextC ...
- 转:zTree树控件扩展篇:巧用zTree控件实现文本框输入关键词自动模糊查找zTree树节点实现模糊匹配下拉选择效果
是否可以借助于zTree实现文本框输入关键词自动模糊匹配zTree下拉树,然后选择下拉树内节点显示在文本框内且隐藏下拉树. 看到这个需求脑子里头大致已经想到了要如何实现这样一个需求,当时是限于时间问题 ...
- c# winform 根据窗体自动调整控件
一.概述 本文要实现的功能是:当窗体最大化时,控件的大小可以随窗体一起变化.开发环境,vs2010 c# winform,窗体名称采用默认的Form1. 2.把调整控件大小的方法放到一个类中:Form ...
- jquery easyui 日历控件和文本框结合使用生成日期
html部分---等待接收所选日期的文本框 <td> <input name='input_date' required class='easyui-textbox' id='xiw ...
- EasyUI 的日期控件单击文本框显示日历
注意:可 用 ctrl+f 搜索 "_outerWidth():0" 1. jQuery.easyui.min.js1.3.2 版本 function _745(_746,_7 ...
- 在C#中子线程如何操作主线程中窗体上控件
在C#中,直接在子线程中对窗体上的控件操作是会出现异常,这是由于子线程和运行窗体的线程是不同的空间,因此想要在子线程来操作窗体上的控件,是不可能 简单的通过控件对象名来操作,但不是说不能进行操作,微软 ...
- 最佳实践扩展Windows窗体DataGridView控件 .net 4.5 附示例代码
Windows窗体DataGridView控件的性能调优.net 4.5 在处理大量数据时, DataGridView 控制可以消耗大量的内存开销,除非你仔细地使用它. 在客户有限的内存,你可以避 ...
- 背水一战 Windows 10 (30) - 控件(文本类): AutoSuggestBox
[源码下载] 背水一战 Windows 10 (30) - 控件(文本类): AutoSuggestBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(文本类) AutoSug ...
随机推荐
- Object Pascal 语言基础
Delphi 是以Object Pascal 语言为基础的可视化开发工具,所以要学好Delphi,首先要掌握的就是Object Pascal 语言.Object Pascal语言是Pascal之父在1 ...
- iOS开发之Xcode6 之手动添加Pch预编译文件
参考文档 http://blog.csdn.net/crazyzhang1990/article/details/44243343 红色部分为本人自己补充注意事项 在Xcode6之前,创建一个新工程x ...
- Request 接收参数乱码原理解析
起因: 今天早上被同事问了一个问题:说接收到的参数是乱码,让我帮着解决一下. 实际情景: 同事负责的平台是Ext.js框架搭建的,web.config配置文件里配置了全局为“GB2312”编码: &l ...
- ajax请求超时时间
http://www.cnblogs.com/charling/p/3356216.html get post 请求 http://www.cnblogs.com/oneword/archive/20 ...
- C/C++中float和double的存储结构
int main (int argc, char **argv) { float a = 1.0f; cout <<"(int&)a = "<<(i ...
- mac 下 parallels 虚拟机 ubuntuServer 安装 parallels tools
mac 下 parallels 虚拟机 ubuntuServer 安装 parallels tools 1. 先点击ubuntu 虚拟机右下角的设置 -----安装 parallels tools - ...
- MySQL和OneSQL并行插入性能对比
按照我的作风,没图说个啥 环境 下面是受叶金荣老师的启发把相关环境交代清楚 MySQL和OneSQL的关键参数配置如下 数据库 sync_binlog innodb_flush_log_at_trx_ ...
- nginx配置 php 单入口
location / { root html; index index.html index.htm index.php; if (!-e $requ ...
- Qt之保持GUI响应
简述 Qter们经常遇到由于耗时操作造成GUI阻塞的问题.其实,这个问题并不难克服,可以采用许多不同的方式,下面我会列举一些可选范围,根据使用情况进行处理. 简述 执行耗时操作 手动事件处理 使用一个 ...
- android入门之: SharedPreferences
读取数据: 保存数据: +++++++++++++++++++方法详解++++++++++++++++++++++++++++++ SharedPreferences综述: 使用getSharedPr ...