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 }

 

 

 

------------------------------------------------------------------------------------------git

做者:龐輝web

出處:http://www.cnblogs.com/pang123hui/ui

本文基於署名 2.5 中國大陸許可協議發佈,歡迎轉載,演繹或用於商業目的,可是必須保留本文的署名龐輝(包含連接).spa

------------------------------------------------------------------------------------------code

相關文章
相關標籤/搜索