c# - 使用 numericUpDown 的 ReadOnly false 处理用户编号输入值条件
问题描述
在 C# WinForms 桌面应用程序中,我使用 2 个相互依赖numericUpDown1
min
的numericUpDown2
max
值numericUpDown
控件:
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
if (numericUpDown1.Value <= numericUpDown2.Value)
{
min = (int)numericUpDown1.Value;
}
else
{
numericUpDown1.Value = min - 1;
}
}
private void numericUpDown2_ValueChanged(object sender, EventArgs e)
{
if (numericUpDown2.Value >= numericUpDown1.Value)
{
max = (int)numericUpDown2.Value;
}
else
{
numericUpDown2.Value = max + 1;
}
}
使用 ofReadOnly = true;
来避免手动输入的最大值小于最小值numericUpDown
:
min = 20;
max = 1999;
numericUpDown1.Value = min;
numericUpDown2.Value = max;
numericUpDown1.ReadOnly = true;
numericUpDown2.ReadOnly = true;
numericUpDown1.Increment = 1;
numericUpDown2.Increment = 1;
numericUpDown1.Maximum = 2000;
numericUpDown1.Minimum = 1;
numericUpDown2.Maximum = 2000;
numericUpDown2.Minimum = 1;
但我使用的范围很大,从 1 到 2000,并且希望允许用户numericUpDown
使用ReadOnly = false;
.
我试图弄清楚,如何使用 of 来控制用户输入条件ReadOnly = false;
,numericUpDown
以避免输入的最大数字小于最小或最小大于最大。
解决方案
试试这个解决方案:
首先设置您的 numericUpDown 属性 ReadOnly=false 或在您的 FormLoad_Function 中编写一行代码,
numericUpDown1.ReadOnly = false;
numericUpDown2.ReadOnly = false;
然后
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
if (numericUpDown1.Value > numericUpDown2.Value)
{
numericUpDown1.Value = numericUpDown1.Value - 1;
MessageBox.Show("Min value always less then Max value");
}
}
private void numericUpDown2_ValueChanged(object sender, EventArgs e)
{
if (numericUpDown1.Value > numericUpDown2.Value)
{
numericUpDown2.Value = numericUpDown2.Value + 1;
MessageBox.Show("Max value always greater then Min value");
}
}
推荐阅读
- python - 在 PyCharm 中,f-string 中的变量带有绿色的运行按钮
- embed - yammer 嵌入配置选项
- eclipse - 在 Eclipse 编辑器中更改/删除悬停延迟
- java - JDBCTemplate 可选参数
- rest - MS Outlook 任务 REST API '% Complete' 字段
- c++ - std::strong_ordering 和 std::weak_ordering 的实际意义
- sql-server - SQL 中的板计算
- ffmpeg - 将 Chromium(或任何 Internet 浏览器)捕获到传输流中
- python - 反映 cx_Oracle / sqlalchemy 中的数据库表
- vba - Excel VBA;尝试将数据从工作簿中的一张工作表复制到同一工作簿中的另一张工作表时出现运行时错误“438”