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

最近常用到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. iOS开发之 几本书

    <object_c 编程之道书> <iOS 7 UI Transition Guide> iOS开发指南:从零基础到App Store上架[国内第一本iOS架构设计图书,涵盖i ...

  2. jquery定位

    1.$("div").offset().left ; div到文档的左距离(offset() 方法返回或设置匹配元素相对于文档的偏移) $("div").off ...

  3. TextView使用SpannableString设置复合文本

    TextView通常用来显示普通文本,但是有时候需要对其中某些文本进行样式.事件方面的设置.Android系统通过SpannableString类来对指定文本进行相关处理,具体有以下功能: 1.Bac ...

  4. 转:copy initialization

    转自: http://en.cppreference.com/w/cpp/language/copy_initialization copy initialization   C++   C++ la ...

  5. 数据字典 dba_free_space及相对文件号RELATIVE_FNO 小结

    1.1 dba_free_space 1.1.1 概述 SQL> desc dba_free_space; Name Type Nullable Default Comments ------- ...

  6. easyui-datebox 和easyui-datetimebox 设置默认时间当前时间

    //显示当前日期 formatterDate = function (date) { var day = date.getDate() > 9 ? date.getDate() : " ...

  7. 精简android4.2

    cd /system/apprm YouTube.*rm Settings.*rm VoiceDialer.*rm Phone.*rm QuickSearchBox.*rm RSSReader.*rm ...

  8. View绑定式监听器实现原理

    在我们开发android的时候,会经常重写自定义的View去满足一些需求 然后有时候view会提供一些回调,比如view某个部分被点击了,我们需要通知使用者然后再通过接口传一些参数过去. 对于我之前的 ...

  9. 【EasyX】RGB to Gray

    code: #include <graphics.h> #include <conio.h> void main() { initgraph(, ); // 读取图片 load ...

  10. 知名黑客组织Anonymous(匿名者)的装备库

    原文出处: infosecinstitute   译文出处:freebuf   欢迎分享原创到伯乐头条 本文关注的是世界著名的黑客组织Anonymous(匿名者).“我将描述他们的攻击方法和方式的计划 ...