/// <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 *许可协议发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的署名庞辉(包含链接).
------------------------------------------------------------------------------------------