应用场景:便携式设备和检测设备等小型设备经常使用触摸屏来代替键盘鼠标的使用,因此在查询和输入界面的文本或者数字输入控件中使用软件盘来代替真正键盘的输入。
软键盘界面:软键盘界面实质上就是一个普通的窗体上面摆放了很多图片按钮,拖拉对应的文本、按钮等控件到设计界面即可、按键的命名遵循简单好记的规则。
字母键盘界面: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";
else
txtResult.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";
else
txtResult.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";
else
txtResult.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";
else
txtResult.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";
else
txtResult.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";
else
txtResult.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";
else
txtResult.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";
else
txtResult.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";
else
txtResult.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";
else
txtResult.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";
else
txtResult.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";
else
txtResult.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";
else
txtResult.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";
else
txtResult.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";
else
txtResult.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";
else
txtResult.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";
else
txtResult.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";
else
txtResult.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";
else
txtResult.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";
else
txtResult.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";
else
txtResult.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";
else
txtResult.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";
else
txtResult.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";
else
txtResult.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";
else
txtResult.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