C#自定义软键盘控件及应用
自定义控件代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp软键盘应用
{
public partial class SoftKey : UserControl
{
public SoftKey()
{
InitializeComponent();
}
//属性
private string str = "";
public string Str
{
get { return str; }
set { str = value; Invalidate(); }
}
void btn_A_Click(object sender, EventArgs e)
{
textBox1.Text += "-";
textBox1.SelectionStart = textBox1.Text.Length;
}
void btn_B_Click(object sender, EventArgs e)
{
textBox1.Text += ".";
textBox1.SelectionStart = textBox1.Text.Length;
}
void btn_Click(object sender, EventArgs e)
{
Button B = (Button)sender;
textBox1.Text += B.Name.Substring(4, 1);
textBox1.SelectionStart = textBox1.Text.Length;
}
void btnESC_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length > 0)
{
textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1, 1);
}
}
void btnCLR_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
public event EventHandler BtnTestClick;
void btnENT_Click(object sender, EventArgs e)
{
str = textBox1.Text;
textBox1.Text = "";
this.Hide();
if (BtnTestClick != null)
{
BtnTestClick(sender, e);
}
}
}
}
窗体应用:
将生成的控件拖入窗体中
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp软键盘应用
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
softKey1.Hide();
}
private void textBox1_Click(object sender, EventArgs e)
{
softKey1.Str = "";
softKey1.Show();
}
private void softKey1_BtnTestClick(object sender, EventArgs e)
{
textBox1.Text += softKey1.Str;
textBox1.SelectionStart = textBox1.Text.Length;
}
}
}