/// <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. 「2014-2-6」TokuMX and MongoDB related materials collection

    简介参考 TokuMX 和 MongoDB 各自的官方站点.       ##  Tokutek 最重要的特点和 marketing word 是所谓 fractal tree indexing te ...

  2. Poj-1157-LITTLE SHOP OF FLOWERS

    题意为从每行取一瓶花,每瓶花都有自己的审美价值 第 i+1 行取的花位于第 i 行的右下方 求最大审美价值 dp[i][j]:取到第 i 行,第 j 列时所获得的最大审美价值 动态转移方程:dp[i] ...

  3. Oracle EBS的BIP报表中显示特殊字体

    http://oracleseeker.com/2009/08/25/font_mapping_setup_for_special_character_print_in_oracle_ebs_bip/ ...

  4. PHP访问数据库

    1.原生mysql方式 <?php //最原生态的访问方式,不过SQL注入是个麻烦事 $con = mysql_connect("localhost","root& ...

  5. C语言 线性表 顺序表结构 实现

    一个能够自动扩容的顺序表 ArrList (GCC编译). #include <stdio.h> #include <stdlib.h> #include <string ...

  6. Kali Linux Web 渗透测试视频教程—第十四课-arp欺骗、嗅探、dns欺骗、session劫持

    Kali Linux Web 渗透测试视频教程—第十四课-arp欺骗.嗅探.dns欺骗.session劫持 文/玄魂 目录 Kali Linux Web 渗透测试—第十四课-arp欺骗.嗅探.dns欺 ...

  7. Linux:多文件编辑

    多文件编辑 1.使用vim编辑多个文件 编辑多个文件有两种形式,一种是在进入vim前使用的参数就是多个文件.另一种就是进入vim后再编辑其他的文件. 同时创建两个新文件并编辑 $ vim 1.txt ...

  8. Minifying Angular应用时产生的问题

    一.产生的问题 如果你正在进行AngularJS的项目开发,生产时Minified JS文件有没有遇到下面问题: angular.module("myApp", []) .cont ...

  9. AngularJS快速入门指南01:导言

    AngularJS使用新的attributes扩展了HTML AngularJS对单页面应用的支持非常好(SPAs) AngularJS非常容易学习 现在就开始学习AngularJS吧! 关于本指南 ...

  10. 如何在ASP.NET中用C#将XML转换成JSON

    本文旨在介绍如果通过C#将获取到的XML文档转换成对应的JSON格式字符串,然后将其输出到页面前端,以供JavaScript代码解析使用.或许你可以直接利用JavaScript代码通过Ajax的方式来 ...