using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace SoftKeyBoard { public partial class SoftkKeyBoard : Form { private Button[] NumBtns = null; private Button[] CharBtns = null; private TextBox textbox1_; // 大小写字母标识 private int lettetflg = 0; public SoftkKeyBoard(TextBox textbox1) { InitializeComponent(); init(); SetRandomNum(NumBtns, 10, 0, 9); SetRandomChar(CharBtns, 26); this.textbox1_ = textbox1; } #region init /// <summary> /// 初始化 /// </summary> private void init() { // 按键初始化 NumBtns = new Button[10]; NumBtns[0] = btn0; NumBtns[1] = btn1; NumBtns[2] = btn2; NumBtns[3] = btn3; NumBtns[4] = btn4; NumBtns[5] = btn5; NumBtns[6] = btn6; NumBtns[7] = btn7; NumBtns[8] = btn8; NumBtns[9] = btn9; CharBtns = new Button[26]; CharBtns[0] = btna; CharBtns[1] = btnb; CharBtns[2] = btnc; CharBtns[3] = btnd; CharBtns[4] = btne; CharBtns[5] = btnf; CharBtns[6] = btng; CharBtns[7] = btnh; CharBtns[8] = btni; CharBtns[9] = btnj; CharBtns[10] = btnk; CharBtns[11] = btnl; CharBtns[12] = btnm; CharBtns[13] = btnn; CharBtns[14] = btno; CharBtns[15] = btnp; CharBtns[16] = btnq; CharBtns[17] = btnr; CharBtns[18] = btns; CharBtns[19] = btnt; CharBtns[20] = btnu; CharBtns[21] = btnv; CharBtns[22] = btnw; CharBtns[23] = btnx; CharBtns[24] = btny; CharBtns[25] = btnz; } #endregion #region SetRandomNum /// <summary> /// 生成不重复的随机数 /// </summary> /// <param name="Number">个数</param> /// <param name="minNum">最小值</param> /// <param name="maxNum">最大值</param> private void SetRandomNum(Button[] b, int Number, int minNum, int maxNum) { int j; Random r = new Random(); for (j = 0; j < Number; j++) { int i = r.Next(minNum, maxNum + 1); int num = 0; for (int k = 0; k < j; k++) { if (b[k].Text == i.ToString()) { num = num + 1; } } if (num == 0) { b[j].Text = i.ToString(); } else { j = j - 1; } } } #endregion #region SetRandomNum /// <summary> /// 生成不重复的随机数 /// </summary> /// <param name="Number">个数</param> /// <param name="minNum">最小值</param> /// <param name="maxNum">最大值</param> private void SetRandomChar(Button[] b, int Number) { int j; Random r = new Random(); for (j = 0; j < Number; j++) { int i = r.Next(0, 26); string letter = NumToLetter(i); int num = 0; for (int k = 0; k < j; k++) { if (b[k].Text == letter) { num = num + 1; } } if (num == 0) { b[j].Text = letter; } else { j = j - 1; } } } #endregion #region NumToLetter /// <summary> /// 数字转换成字母 /// </summary> /// <param name="num">要转换的数字</param> /// <returns>转换后的字母</returns> private string NumToLetter(int num) { string result = string.Empty; switch (num) { case 0: result = "a"; break; case 1: result = "b"; break; case 2: result = "c"; break; case 3: result = "d"; break; case 4: result = "e"; break; case 5: result = "f"; break; case 6: result = "g"; break; case 7: result = "h"; break; case 8: result = "i"; break; case 9: result = "j"; break; case 10: result = "k"; break; case 11: result = "l"; break; case 12: result = "m"; break; case 13: result = "n"; break; case 14: result = "o"; break; case 15: result = "p"; break; case 16: result = "q"; break; case 17: result = "r"; break; case 18: result = "s"; break; case 19: result = "t"; break; case 20: result = "u"; break; case 21: result = "v"; break; case 22: result = "w"; break; case 23: result = "x"; break; case 24: result = "y"; break; case 25: result = "z"; break; } return result; } #endregion #region 字符键 /// <summary> /// ~ /// </summary> /// <param name="sender"></param> /// <param name="e"><
c#实现软键盘
最新推荐文章于 2024-05-23 14:16:47 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)