1. 前言
在使用C#编写上位机时,经常会遇到需要输入数值的场景。如读取汇川Easy320的D软元件时,界面一直在刷新(也可以判断数值变化再刷新),这时候我们通过输入框无法直接更改值并写入PLC中。
老师课程中的做法是打开界面读取一次PLC的数据,再设置一个读取按钮手动来读取PLC,这样就可以更改界面输入框中的数据,再设置写入按钮对PLC进行写入。但是这种做法不太优雅。
2. 数字键盘窗体类实现
为了实现与HMI类似的功能,即点击输入框控件时弹出一个键盘,键盘输入完后将数字传出到外部,可以用于写入到PLC中。
2.1 添加窗体类,命名为NumericKeypad.cs

2.2 在设计器界面添加控件,分别是标签、输入框、按钮

2.3 建立三个属性,用于存储输入框当前值,最小值,最大值
//属性
public string value { get; private set; }
public string min { get; private set; }
public string max { get; private set; }
2.4 在构造函数中传入值
public NumericKeypad(string inputValue,string inputMin,string inputMax )
{
InitializeComponent();
value = inputValue;
min = inputMin;
max = inputMax;
}
2.4 添加窗体Load事件,将属性值刷新到界面上
private void NumericKeypad_Load(object sender, EventArgs e)
{
textBox1.Text = value; //获取当前值
labelMinValue.Text = min;//获取最小值
labelMaxValue.Text = max;//获取最大值
}
2.5 界面按键处理
#region 按键处理
// 符号添加/删除
private void buttonSymbol_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text))
return;
string text = textBox1.Text;
// 检查文本开头是否有减号(考虑空格)
string trimmed = text.TrimStart();
if (trimmed.StartsWith("-") || trimmed.StartsWith("—"))
{
// 有减号:删除它
int minusIndex = text.IndexOf(trimmed[0]);
textBox1.Text = text.Remove(minusIndex, 1).TrimStart();
}
else
{
// 没有减号:添加它
textBox1.Text = "-" + text.TrimStart();
}
textBox1.Focus();
textBox1.SelectionStart = textBox1.Text.Length;
}
// 小数点添加
private void buttonDot_Click(object sender, EventArgs e)
{
// 检查是否已存在点号
if (!textBox1.Text.Contains("."))
{
textBox1.Focus(); // 确保文本框获得焦点
SendKeys.Send("{.}");//使用SendKeys
}
}
private void button0_Click(object sender, EventArgs e)
{
textBox1.Focus(); // 确保文本框获得焦点
SendKeys.Send("{0}");//使用SendKeys
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Focus(); // 确保文本框获得焦点
SendKeys.Send("{1}");//使用SendKeys
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Focus(); // 确保文本框获得焦点
SendKeys.Send("{2}");//使用SendKeys
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Focus(); // 确保文本框获得焦点
SendKeys.Send("{3}");//使用SendKeys
}
private void button4_Click(object sender, EventArgs e)
{
textBox1.Focus(); // 确保文本框获得焦点
SendKeys.Send("{4}");//使用SendKeys
}
private void button5_Click(object sender, EventArgs e)
{
textBox1.Focus(); // 确保文本框获得焦点
SendKeys.Send("{5}");//使用SendKeys
}
private void button6_Click(object sender, EventArgs e)
{
textBox1.Focus(); // 确保文本框获得焦点
SendKeys.Send("{6}");//使用SendKeys
}
private void button7_Click(object sender, EventArgs e)
{
textBox1.Focus(); // 确保文本框获得焦点
SendKeys.Send("{7}");//使用SendKeys
}
private void button8_Click(object sender, EventArgs e)
{
textBox1.Focus(); // 确保文本框获得焦点
SendKeys.Send("{8}");//使用SendKeys
}
private void button9_Click(object sender, EventArgs e)
{
textBox1.Focus(); // 确保文本框获得焦点
SendKeys.Send("{9}");//使用SendKeys
}
private void buttonBackSpace_Click(object sender, EventArgs e)
{
textBox1.Focus(); // 确保文本框获得焦点
SendKeys.Send("{BACKSPACE}");//使用SendKeys模拟Backspace键
}
private void buttonClear_Click(object sender, EventArgs e)
{
textBox1.Text = null;//清空当前值
}
private void buttonExit_Click(object sender, EventArgs e)
{
this.Close();//关闭键盘
}
//Ent按下判断值是否正确再传出
private void buttonEnter_Click(object sender, EventArgs e)
{
string inputText = textBox1.Text;
// 检查输入是否为空
if (string.IsNullOrWhiteSpace(inputText))
{
MessageBox.Show("请输入数值!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBox1.Focus();
return;
}
// 尝试将输入转换为double进行比较
if (double.TryParse(inputText, out double inputValue))
{
// 解析最小值和最大值
double minValue, maxValue;
// 尝试解析最小值
if (double.TryParse(min, out minValue))
{
// 尝试解析最大值
if (double.TryParse(max, out maxValue))
{
// 检查是否在范围内
if (inputValue >= minValue && inputValue <= maxValue)
{
// 在范围内:传出值
value = inputText;
this.DialogResult = DialogResult.OK;
this.Close();
}
else
{
// 超出范围
MessageBox.Show($"数值必须在 {minValue} 到 {maxValue} 之间!",
"范围错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox1.Focus();
textBox1.SelectAll();
}
}
else
{
// 无法解析最大值,只检查最小值
if (inputValue >= minValue)
{
value = inputText;
this.DialogResult = DialogResult.OK;
this.Close();
}
else
{
MessageBox.Show($"数值必须大于等于 {minValue}!",
"范围错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox1.Focus();
textBox1.SelectAll();
}
}
}
else
{
// 无法解析最小值,尝试解析最大值
if (double.TryParse(max, out maxValue))
{
// 只检查最大值
if (inputValue <= maxValue)
{
value = inputText;
this.DialogResult = DialogResult.OK;
this.Close();
}
else
{
MessageBox.Show($"数值必须小于等于 {maxValue}!",
"范围错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox1.Focus();
textBox1.SelectAll();
}
}
else
{
// 两个边界都无法解析,直接接受输入
value = inputText;
this.DialogResult = DialogResult.OK;
this.Close();
}
}
}
else
{
// 无法解析为数字
MessageBox.Show("请输入有效的数字!", "格式错误",
MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox1.Focus();
textBox1.SelectAll();
}
}
#endregion
3. 键盘类调用
3.1 在任意界面实例化本窗体类,可以使用输入框的点击事件实例化,并传入当前值, 最小值,最大值。
NumericKeypad numericKeypad = new NumericKeypad("99","-100","1000");
3.2 判断键盘Ent按下时获取键盘类的输入值
if (numericKeypad.ShowDialog() == DialogResult.OK)
{
string result = numericKeypad.value;//获取输入的值
}
3.3 运行效果






承担因您的行为而导致的法律责任,
本站有权保留或删除有争议评论。
参与本评论即表明您已经阅读并接受
上述条款。