应用场景:便携式设备和检测设备等小型设备经常使用触摸屏来代替键盘鼠标的使用,因此在查询和输入界面的文本或者数字输入控件中使用软件盘来代替真正键盘的输入。
软键盘界面:软键盘界面实质上就是一个普通的窗体上面摆放了很多图片按钮,拖拉对应的文本、按钮等控件到设计界面即可、按键的命名遵循简单好记的规则。
字母键盘界面:26个字母按键的命名从btna-btnz。
字母按键处理代码如下:
#region 字母按钮事件this.btna.Click += new System.EventHandler(this.btna_Click);this.btnb.Click += new System.EventHandler(this.btnb_Click);this.btnc.Click += new System.EventHandler(this.btnc_Click);this.btnd.Click += new System.EventHandler(this.btnd_Click);this.btne.Click += new System.EventHandler(this.btne_Click);this.btnf.Click += new System.EventHandler(this.btnf_Click);this.btng.Click += new System.EventHandler(this.btng_Click);this.btnh.Click += new System.EventHandler(this.btnh_Click);this.btni.Click += new System.EventHandler(this.btni_Click);this.btnj.Click += new System.EventHandler(this.btnj_Click);this.btnk.Click += new System.EventHandler(this.btnk_Click);this.btnl.Click += new System.EventHandler(this.btnl_Click);this.btnm.Click += new System.EventHandler(this.btnm_Click);this.btnn.Click += new System.EventHandler(this.btnn_Click);this.btno.Click += new System.EventHandler(this.btno_Click);this.btnp.Click += new System.EventHandler(this.btnp_Click);this.btnq.Click += new System.EventHandler(this.btnq_Click);this.btnr.Click += new System.EventHandler(this.btnr_Click);this.btns.Click += new System.EventHandler(this.btns_Click);this.btnt.Click += new System.EventHandler(this.btnt_Click);this.btnu.Click += new System.EventHandler(this.btnu_Click);this.btnv.Click += new System.EventHandler(this.btnv_Click);this.btnw.Click += new System.EventHandler(this.btnw_Click);this.btnx.Click += new System.EventHandler(this.btnx_Click);this.btny.Click += new System.EventHandler(this.btny_Click);this.btnz.Click += new System.EventHandler(this.btnz_Click);#endregion
对应的字母按钮点击事件处理代码
#region 单个字符按钮点击事件#region 第一排字母按钮点击事件private void btnq_Click(object sender, EventArgs e){if (!(Caps_Lock ^ ShiftFlag))txtResult.Text += "q";elsetxtResult.Text += "Q"; txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}private void btnw_Click(object sender, EventArgs e){if (!(Caps_Lock ^ ShiftFlag))txtResult.Text += "w";elsetxtResult.Text += "W"; txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}private void btne_Click(object sender, EventArgs e){if (!(Caps_Lock ^ ShiftFlag))txtResult.Text += "e";elsetxtResult.Text += "E"; txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}private void btnr_Click(object sender, EventArgs e){if (!(Caps_Lock ^ ShiftFlag))txtResult.Text += "r";elsetxtResult.Text += "R"; txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}private void btnt_Click(object sender, EventArgs e){if (!(Caps_Lock ^ ShiftFlag))txtResult.Text += "t";elsetxtResult.Text += "T"; txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}private void btny_Click(object sender, EventArgs e){if (!(Caps_Lock ^ ShiftFlag))txtResult.Text += "y";elsetxtResult.Text += "Y"; txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}private void btnu_Click(object sender, EventArgs e){if (!(Caps_Lock ^ ShiftFlag))txtResult.Text += "u";elsetxtResult.Text += "U"; txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}private void btni_Click(object sender, EventArgs e){if (!(Caps_Lock ^ ShiftFlag))txtResult.Text += "i";elsetxtResult.Text += "I"; txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}private void btno_Click(object sender, EventArgs e){if (!(Caps_Lock ^ ShiftFlag))txtResult.Text += "o";elsetxtResult.Text += "O"; txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}private void btnp_Click(object sender, EventArgs e){if (!(Caps_Lock ^ ShiftFlag))txtResult.Text += "p";elsetxtResult.Text += "P"; txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}#endregion#region 第二排字母按钮点击事件private void btna_Click(object sender, EventArgs e){if (InputType == "English"){txtResult.Text += this.btna.Text;}else if (InputType == "Char"){txtResult.Text += this.btna.Text;}else if (InputType == "Chinese"){lblPinyin.Text += this.btna.Text;}//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}private void btns_Click(object sender, EventArgs e){if (!(Caps_Lock ^ ShiftFlag))txtResult.Text += "s";elsetxtResult.Text += "S"; txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}private void btnd_Click(object sender, EventArgs e){if (!(Caps_Lock ^ ShiftFlag))txtResult.Text += "d";elsetxtResult.Text += "D"; txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}private void btnf_Click(object sender, EventArgs e){if (!(Caps_Lock ^ ShiftFlag))txtResult.Text += "f";elsetxtResult.Text += "F"; txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}private void btng_Click(object sender, EventArgs e){if (!(Caps_Lock ^ ShiftFlag))txtResult.Text += "g";elsetxtResult.Text += "G"; txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}private void btnh_Click(object sender, EventArgs e){if (!(Caps_Lock ^ ShiftFlag))txtResult.Text += "h";elsetxtResult.Text += "H"; txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}private void btnj_Click(object sender, EventArgs e){if (!(Caps_Lock ^ ShiftFlag))txtResult.Text += "j";elsetxtResult.Text += "J"; txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}private void btnk_Click(object sender, EventArgs e){if (!(Caps_Lock ^ ShiftFlag))txtResult.Text += "k";elsetxtResult.Text += "K"; txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}private void btnl_Click(object sender, EventArgs e){if (!(Caps_Lock ^ ShiftFlag))txtResult.Text += "l";elsetxtResult.Text += "L"; txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}#endregion#region 第三排字母按钮点击事件private void btnz_Click(object sender, EventArgs e){if (!(Caps_Lock ^ ShiftFlag))txtResult.Text += "z";elsetxtResult.Text += "Z"; txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}private void btnx_Click(object sender, EventArgs e){if (!(Caps_Lock ^ ShiftFlag))txtResult.Text += "x";elsetxtResult.Text += "X"; txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}private void btnc_Click(object sender, EventArgs e){if (!(Caps_Lock ^ ShiftFlag))txtResult.Text += "c";elsetxtResult.Text += "C"; txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}private void btnv_Click(object sender, EventArgs e){if (!(Caps_Lock ^ ShiftFlag))txtResult.Text += "v";elsetxtResult.Text += "V"; txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}private void btnb_Click(object sender, EventArgs e){if (!(Caps_Lock ^ ShiftFlag))txtResult.Text += "b";elsetxtResult.Text += "B"; txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}private void btnn_Click(object sender, EventArgs e){if (!(Caps_Lock ^ ShiftFlag))txtResult.Text += "n";elsetxtResult.Text += "N"; txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}private void btnm_Click(object sender, EventArgs e){if (!(Caps_Lock ^ ShiftFlag))txtResult.Text += "m";elsetxtResult.Text += "M"; txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}#endregion#endregion
数字键盘界面:0-9数字按键命名分别是btn0-btn9
数字按钮点击事件绑定
#region 数字按钮点击事件this.btn0.Click += new System.EventHandler(this.btn0_Click);this.btn1.Click += new System.EventHandler(this.btn1_Click);this.btn2.Click += new System.EventHandler(this.btn2_Click);this.btn3.Click += new System.EventHandler(this.btn3_Click);this.btn4.Click += new System.EventHandler(this.btn4_Click);this.btn5.Click += new System.EventHandler(this.btn5_Click);this.btn6.Click += new System.EventHandler(this.btn6_Click);this.btn7.Click += new System.EventHandler(this.btn7_Click);this.btn8.Click += new System.EventHandler(this.btn8_Click);this.btn9.Click += new System.EventHandler(this.btn9_Click);#endregion
数字按钮点击事件代码
#region 数字键盘点击事件private void btn0_Click(object sender, EventArgs e){txtResult.Text += "0";txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}private void btn1_Click(object sender, EventArgs e){txtResult.Text += "1";txtResult.Focus();//设置光标的位置到文本尾txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处txtResult.ScrollToCaret();}private void btn2_Click(object sender, EventArgs e){txtResult.Text += "2";txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}private void btn3_Click(object sender, EventArgs e){txtResult.Text += "3"; txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}private void btn4_Click(object sender, EventArgs e){txtResult.Text += "4"; txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}private void btn5_Click(object sender, EventArgs e){txtResult.Text += "5"; txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}private void btn6_Click(object sender, EventArgs e){txtResult.Text += "6"; txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}private void btn7_Click(object sender, EventArgs e){txtResult.Text += "7"; txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}private void btn8_Click(object sender, EventArgs e){txtResult.Text += "8"; txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}private void btn9_Click(object sender, EventArgs e){txtResult.Text += "9"; txtResult.Focus();//设置光标的位置到文本尾 txtResult.Select(txtResult.Text.Length, 0);//滚动到控件光标处 txtResult.ScrollToCaret();}#endregion