第一次写文章,组词难免没有不通之处。。。

最近常用到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;
} }
}
}

三、源码下载:

ConsoleApplication1.zip

根据窗体自动调整控件及文本框记住上次填写内容Demo的更多相关文章

  1. 用MVC的辅助方法自定义了两个控件:“可编辑的下拉框控件”和“文本框日历控件”

    接触MVC也没多长时间,一开始学的时候绝得MVC结构比较清晰.后来入了门具体操作下来感觉MVC控件怎么这么少还不可以像ASP.net form那样拖拽.这样设计界面来,想我种以前没学过JS,Jquer ...

  2. 在GridView控件内文本框实现TextChanged事件

    本篇是教你实现GridView控件内的TextBox文本框实现自身的TextChanged事件.由于某些功能的需求,GridView控件内嵌TextBox,当TextBox值发生变化时,触发TextC ...

  3. 转:zTree树控件扩展篇:巧用zTree控件实现文本框输入关键词自动模糊查找zTree树节点实现模糊匹配下拉选择效果

    是否可以借助于zTree实现文本框输入关键词自动模糊匹配zTree下拉树,然后选择下拉树内节点显示在文本框内且隐藏下拉树. 看到这个需求脑子里头大致已经想到了要如何实现这样一个需求,当时是限于时间问题 ...

  4. c# winform 根据窗体自动调整控件

    一.概述 本文要实现的功能是:当窗体最大化时,控件的大小可以随窗体一起变化.开发环境,vs2010 c# winform,窗体名称采用默认的Form1. 2.把调整控件大小的方法放到一个类中:Form ...

  5. jquery easyui 日历控件和文本框结合使用生成日期

    html部分---等待接收所选日期的文本框 <td> <input name='input_date' required class='easyui-textbox' id='xiw ...

  6. EasyUI 的日期控件单击文本框显示日历

    注意:可 用 ctrl+f 搜索 "_outerWidth():0" 1. jQuery.easyui.min.js1.3.2 版本   function _745(_746,_7 ...

  7. 在C#中子线程如何操作主线程中窗体上控件

    在C#中,直接在子线程中对窗体上的控件操作是会出现异常,这是由于子线程和运行窗体的线程是不同的空间,因此想要在子线程来操作窗体上的控件,是不可能 简单的通过控件对象名来操作,但不是说不能进行操作,微软 ...

  8. 最佳实践扩展Windows窗体DataGridView控件 .net 4.5 附示例代码

    Windows窗体DataGridView控件的性能调优.net 4.5   在处理大量数据时, DataGridView 控制可以消耗大量的内存开销,除非你仔细地使用它. 在客户有限的内存,你可以避 ...

  9. 背水一战 Windows 10 (30) - 控件(文本类): AutoSuggestBox

    [源码下载] 背水一战 Windows 10 (30) - 控件(文本类): AutoSuggestBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(文本类) AutoSug ...

随机推荐

  1. GO语言中间的derfer

    defer Go语言中有种不错的设计,即延迟(defer)语句,你可以在函数中添加多个defer语句.当函数执行到最后时,这些defer语句会按照逆序执行, 最后该函数返回.特别是当你在进行一些打开资 ...

  2. Java 默认/缺省 内存大小,如果没有 -Xms -Xmx

    命令 java -XX:+PrintCommandLineFlags -version 会直观的输出下面默认值 -XX:InitialHeapSize=16336768 -XX:MaxHeapSize ...

  3. 解决PhoneGap不支持viewport的几种方法

    前几天用phonegap编译GameBuilder+CanTK时,发现HTML里的viewport无效.CanTK根据devicePixelRatio检测设备的DPI,然后用viewport设置正确的 ...

  4. django 2

    创建一个管理员用户 首先,我们需要创建一个用户可以登录到管理网站. 运行 下面的命令: $ python manage.py createsuperuser 输入你想要的用户名,按回车. Userna ...

  5. robotframework笔记1

    机器人框架是一个基于Python的,可扩展的关键字驱动的自动化测试最终到终端的验收测试和验收测试驱动开发(ATDD)框架.它可用于检测分布式异类应用程序,其中,验证要求感人几种技术和接口. 下面列出了 ...

  6. border用处多

    1. 使用border属性实现梯形    给定一个div,通过设定div四个边框不同的颜色且设置比较粗的边框线条,可以看到div除了中间的content部分,四个边框均成梯形状,既然已经有了梯形的雏形 ...

  7. VI编辑器学习笔记

    VIM的使用 VI是Linux系统中的一种编辑器,它的使用方法和界面与Unix平台十分相似,掌握了VIM的特殊,你可以感觉到它强大的功能与高效.Vim 相对来说较小,无论你使用任何Linux系统,你总 ...

  8. System.OutOfMemoryException: 内存不足。(转)

    主要是网站生成水印图片的时候遇到的 原文地址:http://www.cnblogs.com/longgel/archive/2010/03/24/1693776.html 今天调试asp.net 程序 ...

  9. C语言知识整理(2):volatile与register

    1.volatile volatile是易变的,不稳定的意思,volatile是关键字,是一种类型修饰符,用它修饰的变量表示可以被某些编译器未知的因素更改,比如操作系统.硬件或者其他线程等,遇到这个关 ...

  10. Collecting Bugs(POJ 2096)

    Collecting Bugs Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 3064   Accepted: 1505 ...