C# 个人常用代码积累
/// <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# 个人常用代码积累的更多相关文章
- JS date常用代码积累
Date.prototype.Format = function(fmt) { var o = { "M+" : this.getMonth()+1, //月份 "d+& ...
- shell常用代码积累
1.使用getopts从命令行接收参数 例: while getopts h:u:p: OPTION do case $OPTION in h) echo "主机地址:$OPTARG&quo ...
- python常用代码积累
一.文件操作 1.判断一个目录是否存在,若不存在则创建 if not os.path.isdir(new_path): os.makedirs(new_path) 2.新建一个文件 f=open(&q ...
- GCD 常用代码
GCD 常用代码 体验代码 异步执行任务 - (void)gcdDemo1 { // 1. 全局队列 dispatch_queue_t q = dispatch_get_global_queue(0, ...
- 转--Android实用的代码片段 常用代码总结
这篇文章主要介绍了Android实用的代码片段 常用代码总结,需要的朋友可以参考下 1:查看是否有存储卡插入 复制代码 代码如下: String status=Environment.getE ...
- 刀哥多线程之03GCD 常用代码
GCD 常用代码 体验代码 异步执行任务 - (void)gcdDemo1 { // 1. 全局队列 dispatch_queue_t q = dispatch_get_global_queue(0, ...
- jquery常用代码集锦
1. 如何修改jquery默认编码(例如默认GB2312改成 UTF-8 ) 1 2 3 4 5 $.ajaxSetup({ ajaxSettings : { contentT ...
- Mysql:常用代码
C/S: Client Server B/S: Brower Server Php主要实现B/S .net IIS Jave TomCat LAMP:L Mysql:常用代码 Create table ...
- javascript常用代码大全
http://caibaojian.com/288.html 原文链接 jquery选中radio //如果之前有选中的,则把选中radio取消掉 $("#tj_cat .pro_ca ...
随机推荐
- 【kd-tree】bzoj2648 SJY摆棋子
#include<cstdio> #include<cmath> #include<algorithm> using namespace std; #define ...
- jQuery学习总结(一)
jQuery当中独有的对象:jQuery对象: jQuery对象的缩写形式“$”:所以在使用时,我们都是用$来代替jQuery. 所以我们在页面元素选择或执行功能函数的时候可以这么写:$(functi ...
- iOS 汉字转拼音
- (NSString *)getFirstString:(ICCustom *)custom { NSMutableString *source = [custom.merchantAbbr ...
- Windows 8.1 应用再出发 - 创建我的第一个应用
转眼间Windows 8.1已经发布了四个多月,之前因为开发需要对Windows 8.1新特性进行过零散的学习和使用,一直没有静下心来系统的学习过.近日部门有几名新同事加入,需要进行Windows 商 ...
- 『TCP/IP详解——卷一:协议』读书笔记——07
2013-08-20 17:51:49 第三章 IP:网际协议 3.1 引言 IP是TCP/IP协议族中最为核心的协议.所有的TCP.UDP.ICMP和IGMP数据都以IP数据报格式传输.再来看一下图 ...
- Linux splint命令
一.简介 splint是一个针对C语言的开源程序静态分析工具. 二.安装配置 1)yum安装 yum install -y splint 2)源码安装 http://www.splint.org/ 配 ...
- Dynamic CRM 2013学习笔记(三十二)自定义审批流3 - 节点及实体配置
上次介绍了<Dynamic CRM 2013学习笔记(十九)自定义审批流1 - 效果演示> 以及如何配置自定义审批流的按钮:<Dynamic CRM 2013学习笔记(二十一)自定义 ...
- 【Leetcode】【Medium】Pow(x, n)
Implement pow(x, n). 解题思路: 求浮点数的幂次方,注意可能为负数次幂: 可以使用二分搜索的思想,当n为偶数时,x^n = x^(n/2) * x^(n/2),因此只需要求得一半的 ...
- C++中虚析构函数作用
我们知道,用C++开发的时候,用来做基类的类的析构函数一般都是虚函数.可是,为什么要这样做呢?下面用一个小例子来说明: 有下面的两个类: class ClxBase{public: ...
- libevent (二) 接收TCP连接
libevent 接收TCP连接 Evconnlistener 机制为您提供了侦听和接受传入的 TCP 连接的方法.下面的函数全部包含在`<event2/listener.h>`中. ev ...