/// <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. linux下如何查找需要的文件后并删除

    1.首先查找指定目录下的文件,默认为当前目录 使用命令:find . -name 'a.txt' 会得到当前目录下所有包括子孙目录下的所有后缀为txt的文件 2.查找后删除 使用命令:find . - ...

  2. eclipse里面构建maven项目详解(转载)

    本文来源于:http://my.oschina.net/u/1540325/blog/548530 eclipse里面构建maven项目详解 1       环境安装及分配 Maven是基于项目对象模 ...

  3. 【6_100】Same Tree

    Same Tree Total Accepted: 97481 Total Submissions: 230752 Difficulty: Easy Given two binary trees, w ...

  4. Linux内核分析之计算机是如何工作的

    一.计算机工作原理 本周实验主要是反汇编C代码,生成汇编程序.冯·诺依曼理论的要点是:数字计算机的数制采用二进制,计算机应该按照程序顺序执行.人们把冯·诺依曼的这个理论称为冯·诺依曼体系结构.CPU通 ...

  5. Windows Phone的简单学习

    这次我主要学习了Windows Phone的开发,以及一些简单代码及用处.我了解到了windowsphone的一些功能,下面是我了解到的一些信息. Image 设置好图片的长和宽.结合了Border效 ...

  6. Big Event in HDU(HDU1171)可用背包和母函数求解

    Big Event in HDU  HDU1171 就是求一个简单的背包: 题意:就是给出一系列数,求把他们尽可能分成均匀的两堆 如:2 10 1 20 1     结果是:20 10.才最均匀! 三 ...

  7. ASP.NET MVC 下拉列表使用小结

    ASP.NET MVC中下拉列表的用法很简单,也很方便,具体来说,主要是页面上支持两种Html帮助类的方法:DropDownList()和DropDownListFor().这篇博文主要作为个人的一个 ...

  8. Javascript 异步加载详解(转)

    本文总结一下浏览器在 javascript 的加载方式. 关键词:异步加载(async loading),延迟加载(lazy loading),延迟执行(lazy execution),async 属 ...

  9. [MSSQL2012]CUME_DIST函数

    CUME_DIST函数以某列作为基准,计算其它行相对于基准行数据的比例.差距比例,比较容易理解 先看下测试数据 DECLARE @TestData TABLE(     ID INT IDENTITY ...

  10. Installing SCM-Manager

    With SCM-Manager, people can share and manage Git, Mercurial and Subversion repositories over http e ...