zoukankan      html  css  js  c++  java
  • C# 个人常用代码积累

     1 /// <summary>
     2 /// TextBox限制只能输入十六进制,且只能输入6个
     3 /// </summary>
     4 /// <param name="sender"></param> /// <param name="e"></param>
     5 private void textBoxAddFilter_KeyPress(object sender, KeyPressEventArgs e)
     6 {
     7         const int ci_input_limit = 6;
     8         /////////////////////////////////////////////////
     9         TextBox textbox = (TextBox)sender;
    10 
    11         if (textbox.Text.Length >=  ci_input_limit  && e.KeyChar != 8)  /* 限制输入个数 */
    12         {
    13             MessageBox.Show("输入字符不得超过3 bytes");
    14             e.Handled = true;
    15         }
    16             
    17         if (e.KeyChar != 8  /* 允许使用退格符 */
    18          && !Char.IsDigit(e.KeyChar) 
    19          && !(((int)e.KeyChar >= 'A' && (int)e.KeyChar <= 'F'))
    20          && !(((int)e.KeyChar >= 'a' && (int)e.KeyChar <= 'f')))
    21         {
    22             MessageBox.Show("只允许输入0-9和A-F和a-f,这几个字符");
    23             e.Handled = true;
    24         }
    25 }
     1 /// <summary>
     2 /// 保存文本文件
     3 /// </summary>
     4 /// <param name="sender"></param>
     5 /// <param name="e"></param>
     6 private void buttonSaveFile_Click(object sender, EventArgs e)
     7 {
     8     // 保存文件对话框
     9     SaveFileDialog saveFileDialog = new SaveFileDialog();
    10     // 保存类型
    11     saveFileDialog.Filter = "文本文档(*.txt)|*.txt";
    12     // 默认文件名
    13     saveFileDialog.FileName = "file.txt";
    14     // 打开选择文件对话框
    15     if (saveFileDialog.ShowDialog() == DialogResult.OK)
    16     {
    17         // 选择的文件的绝对路径,只要文件名,自己去分割
    18         txtFileName.Text = saveFileDialog.FileName;
    19     }
    20     
    21     FileStream fs2;
    22 
    23     try
    24     {
    25         fs2 = File.Create(txtFileName.Text);
    26     }
    27     catch
    28     {
    29         MessageBox.Show("建立文件时出错。", "错误",
    30         System.Windows.Forms.MessageBoxButtons.OK,
    31         System.Windows.Forms.MessageBoxIcon.Warning);
    32         return;
    33     }
    34 
    35     byte[] content = new UTF8Encoding(true).GetBytes(txGet.Text);
    36 
    37     try
    38     {
    39         fs2.Write(content, 0, content.Length);
    40         fs2.Flush();
    41         MessageBox.Show("保存成功", "保存",
    42         System.Windows.Forms.MessageBoxButtons.OK,
    43         System.Windows.Forms.MessageBoxIcon.Information);
    44     }
    45     catch
    46     {
    47         MessageBox.Show("写入文件时出错。", "错误",
    48         System.Windows.Forms.MessageBoxButtons.OK,
    49         System.Windows.Forms.MessageBoxIcon.Warning);
    50     }
    51     finally
    52     {
    53         fs2.Close();
    54     }
    55 }
     1 ///////////////////////////////////////////////////////
     2 // string和byte[]转换
     3 ///////////////////////////////////////////////////////
     4 Using System.Text;
     5 // byte[ ] 转换为string
     6 byte[ ] image;
     7 string ll = Encoding.Default.GetString(image);
     8 // string 转换为byte[ ]
     9 string ss;
    10 byte[] b = Encoding.Default.GetBytes(ss);
     1 /// <summary>
     2 /// 插入一个String到ListBox的下一行,并滚动到最后
     3 /// </summary>
     4 /// <param name="listbox"></param>
     5 /// <param name="position"></param>
     6 /// <param name="str"></param>
     7 private void insertListBoxNextLineAutoBelow(ListBox listbox, String str)
     8 {
     9     bool scroll = false;
    10     if (listbox.Items.Count - (int)(listbox.Height / listbox.ItemHeight) > 0)
    11     {
    12         scroll = true;
    13     }
    14 
    15     listbox.Items.Insert(listbox.Items.Count, str);
    16     //listbox.SelectedIndex = listbox.Items.Count - 1; // auto select last one
    17 
    18     if (scroll)
    19     {
    20         listbox.TopIndex = listbox.Items.Count - (int)(listbox.Height / listbox.ItemHeight);
    21     }
    22 }

     

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

    作者:庞辉

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

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

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

  • 相关阅读:
    野生的男人,家养的猪
    能在xcode5中开发基于IOS7sdk的应用程序兼容ios4.3之后的系统吗?
    ios开发怎样才能做到代码和界面彻底分离,方便换肤?
    如何解决iOS6、iOS7 3.5寸和4.0寸屏的适配问题?不要写两个xib文件
    哪些听起来很牛逼的互联网理念!
    iOS 使用宏 常量 报错 expected expression
    ios测试宏指令出错:“Expected identefier”
    某个 页面覆盖了 UITabBar 的tabItem的解决办法
    ios(包括6、7)应用程序引用系统通讯录的方法 [亲测可行]
    ios 获得通讯录中联系人的所有属性 亲测,可行 兼容io6 和 ios 7
  • 原文地址:https://www.cnblogs.com/pang123hui/p/3418432.html
Copyright © 2011-2022 走看看