/// <summary>
/// TextBox限制只能输入十六进制,且只能输入6个
/// </summary>
/// <param name="sender"></param> /// <param name="e"></param>
private void textBoxAddFilter_KeyPress(object sender, KeyPressEventArgs e)
{
const int ci_input_limit = ;
/////////////////////////////////////////////////
TextBox textbox = (TextBox)sender; if (textbox.Text.Length >= ci_input_limit && e.KeyChar != ) /* 限制输入个数 */
{
MessageBox.Show("输入字符不得超过3 bytes");
e.Handled = true;
} if (e.KeyChar != /* 允许使用退格符 */
&& !Char.IsDigit(e.KeyChar)
&& !(((int)e.KeyChar >= 'A' && (int)e.KeyChar <= 'F'))
&& !(((int)e.KeyChar >= 'a' && (int)e.KeyChar <= 'f')))
{
MessageBox.Show("只允许输入0-9和A-F和a-f,这几个字符");
e.Handled = true;
}
}
 /// <summary>
/// 保存文本文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonSaveFile_Click(object sender, EventArgs e)
{
// 保存文件对话框
SaveFileDialog saveFileDialog = new SaveFileDialog();
// 保存类型
saveFileDialog.Filter = "文本文档(*.txt)|*.txt";
// 默认文件名
saveFileDialog.FileName = "file.txt";
// 打开选择文件对话框
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
// 选择的文件的绝对路径,只要文件名,自己去分割
txtFileName.Text = saveFileDialog.FileName;
} FileStream fs2; try
{
fs2 = File.Create(txtFileName.Text);
}
catch
{
MessageBox.Show("建立文件时出错。", "错误",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Warning);
return;
} byte[] content = new UTF8Encoding(true).GetBytes(txGet.Text); try
{
fs2.Write(content, , content.Length);
fs2.Flush();
MessageBox.Show("保存成功", "保存",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information);
}
catch
{
MessageBox.Show("写入文件时出错。", "错误",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Warning);
}
finally
{
fs2.Close();
}
}
 ///////////////////////////////////////////////////////
// string和byte[]转换
///////////////////////////////////////////////////////
Using System.Text;
// byte[ ] 转换为string
byte[ ] image;
string ll = Encoding.Default.GetString(image);
// string 转换为byte[ ]
string ss;
byte[] b = Encoding.Default.GetBytes(ss);
 /// <summary>
/// 插入一个String到ListBox的下一行,并滚动到最后
/// </summary>
/// <param name="listbox"></param>
/// <param name="position"></param>
/// <param name="str"></param>
private void insertListBoxNextLineAutoBelow(ListBox listbox, String str)
{
bool scroll = false;
if (listbox.Items.Count - (int)(listbox.Height / listbox.ItemHeight) > )
{
scroll = true;
} listbox.Items.Insert(listbox.Items.Count, str);
//listbox.SelectedIndex = listbox.Items.Count - 1; // auto select last one if (scroll)
{
listbox.TopIndex = listbox.Items.Count - (int)(listbox.Height / listbox.ItemHeight);
}
}

------------------------------------------------------------------------------------------

作者:庞辉

出处:http://www.cnblogs.com/pang123hui/

本文基于署名 2.5 中国大陆许可协议发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的署名庞辉(包含链接).

------------------------------------------------------------------------------------------

C# 个人常用代码积累的更多相关文章

  1. JS date常用代码积累

    Date.prototype.Format = function(fmt) { var o = { "M+" : this.getMonth()+1, //月份 "d+& ...

  2. shell常用代码积累

    1.使用getopts从命令行接收参数 例: while getopts h:u:p: OPTION do case $OPTION in h) echo "主机地址:$OPTARG&quo ...

  3. python常用代码积累

    一.文件操作 1.判断一个目录是否存在,若不存在则创建 if not os.path.isdir(new_path): os.makedirs(new_path) 2.新建一个文件 f=open(&q ...

  4. GCD 常用代码

    GCD 常用代码 体验代码 异步执行任务 - (void)gcdDemo1 { // 1. 全局队列 dispatch_queue_t q = dispatch_get_global_queue(0, ...

  5. 转--Android实用的代码片段 常用代码总结

    这篇文章主要介绍了Android实用的代码片段 常用代码总结,需要的朋友可以参考下     1:查看是否有存储卡插入 复制代码 代码如下: String status=Environment.getE ...

  6. 刀哥多线程之03GCD 常用代码

    GCD 常用代码 体验代码 异步执行任务 - (void)gcdDemo1 { // 1. 全局队列 dispatch_queue_t q = dispatch_get_global_queue(0, ...

  7. jquery常用代码集锦

    1. 如何修改jquery默认编码(例如默认GB2312改成 UTF-8 ) 1 2 3 4 5 $.ajaxSetup({     ajaxSettings : {         contentT ...

  8. Mysql:常用代码

    C/S: Client Server B/S: Brower Server Php主要实现B/S .net IIS Jave TomCat LAMP:L Mysql:常用代码 Create table ...

  9. javascript常用代码大全

    http://caibaojian.com/288.html    原文链接 jquery选中radio //如果之前有选中的,则把选中radio取消掉 $("#tj_cat .pro_ca ...

随机推荐

  1. ubuntu 14.04下 horizon openstack_dashboard 的开发环境搭建

    序:公司要在openstack的基础上,做开发做产品,网上资料也不是很多,很多都是在来回copy,在此做个blog,慢慢更新,推动自己进步. 首先老话题:开发环境的搭建.     一个纯净的ubunt ...

  2. fgets和fputs函数

    1 函数输入 下面两个函数提供每次输入一行的功能. #include <stdio.h> char *fgets( char *restrict buf, int n, FILE *res ...

  3. struts2的运行原理及配置文件

    struts2官方运行原理图: 1,客户发送请求(url地址就是请求),tomcat接到请求会找到相应的应用web.xml配置文件. 2,web.xml中filter拦截器把你的请求接收到,并进入Fi ...

  4. 关于mysql占用CPU过高,问题解决

    使用SHOW PROCESSLIST 查看 原因: 使用了 一个触发器 不断的去删除日志,保证每个用户的日志只有10条 去掉之后,CPU使用率从97% 降到了 %. 利用show columns fr ...

  5. PHP部分字符串函数汇总

    PHP部分字符串函数汇总 提交 我的评论 加载中 已评论 PHP部分字符串函数汇总 2015-03-10 PHP100中文网 PHP100中文网 PHP100中文网 微信号 功能介绍 互联网开发者社区 ...

  6. blob及行外数据

    本文中我们假设innodb_page_size为16k,记录格式为compact. 1 大字段 大字段的类型可以参看这里, Data Type Storage Required TINYBLOB, T ...

  7. 使用C#和OpenPop.dll开发读取POP3邮件程序

    制作一个ASP.NET MVC4论坛项目,该项目需要将以往十多年的Mail List(邮件列表)内容都导入到新的论坛中,因此需要能够将邮件的标题.发布时间.发布人及邮件内容导入到论坛的数据库内..Ne ...

  8. C#分布式缓存一:Couchbase的安装与简单使用

    一.简介 目前C#业界使用得最多的 Cache 系统主要是 Memcached和 Redis. 这两个 Cache 系统可以说是比较成熟的解决方案,也是很多系统当然的选择. Memcache的开发团队 ...

  9. Asp.Net Web API 2第十四课——Content Negotiation(内容协商)

    前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.html 本文描述ASP.NET W ...

  10. 深入理解java虚拟机【类加载机制】

    Java虚拟机类加载过程是把Class类文件加载到内存,并对Class文件中的数据进行校验.转换解析和初始化,最终形成可以被虚拟机直接使用的java类型的过程. 在加载阶段,java虚拟机需要完成以下 ...