带有值验证的DialogBox

时间:2015-06-22 11:56:40

标签: c# .net winforms

对于我处理图形的应用程序,我已经设置了一个小的DialogBox:

  • 最大;
  • 敏;
  • 主要步骤(比例尺);
  • 小步骤。

这是一个截屏:

enter image description here

我想在允许用户点击 Ok 之前验证一些内容:

  1. Max> = Min
  2. MaxScale> = MinScale。
  3. 但它没有像我一样工作:如果我设置Min> Max,文本框为红色但 Ok 按钮仍然启用...

    我该如何解决?

    这是我的代码:

    public partial class AxisPropertiesDial : Form
    {
        public AxisPropertiesDial(string title, double max, double min, double majorStep, double minorStep)
        {
            Max = max;
            Min = min;
            MajorStep = majorStep;
            MinorStep = minorStep;
            InitializeComponent();
    
            tbMax.Text = max.ToString();
            tbMin.Text = min.ToString();
            tbMajStep.Text = majorStep.ToString();
            tbMinStep.Text = minorStep.ToString();
            Load += OnLoad;
            Text = "Axis: " + title;
        }
    
        private void OnLoad(object sender, EventArgs e)
        {
            SetDesktopLocation(Cursor.Position.X - Size.Width / 2, Cursor.Position.Y - Size.Height / 2);
        }
    
        public double Max;
        public double Min;
        public double MajorStep;
        public double MinorStep;
    
        private void btnOk_Click(object sender, EventArgs e)
        {
            Max = Convert.ToDouble(tbMax.Text);
            Min = Convert.ToDouble(tbMin.Text);
            MajorStep = Convert.ToDouble(tbMajStep.Text);
            MinorStep = Convert.ToDouble(tbMinStep.Text);
        }
    
        private void ValidateTextBox(TextBox tb, double save, out double valueToAssign, double compareTo, bool greaterThan)
        {
            btnOk.Enabled = false;
            tb.BackColor = Color.Tomato;
            if (!Double.TryParse(tb.Text, out valueToAssign))
            {
                valueToAssign = save;
                return;
            }
            if (greaterThan && (valueToAssign >= compareTo) || !greaterThan && (valueToAssign <= compareTo))
            {
                btnOk.Enabled = true;
                tb.BackColor = Color.White;
            }
            else
            {
                //Rollback
                valueToAssign = save;
            }
        }
    
        private void ValidateAll()
        {
            ValidateTextBox(tbMax, Max, out Max, Min, true);
            ValidateTextBox(tbMin, Min, out Min, Max, false);
            ValidateTextBox(tbMajStep, MajorStep, out MajorStep, MinorStep, true);
            ValidateTextBox(tbMinStep, MinorStep, out MinorStep, MajorStep, false);
        }
    
        private void tb_TextChanged(object sender, EventArgs e)
        {
            ValidateAll();
        }
    }
    

1 个答案:

答案 0 :(得分:2)

问题是你在检查单个文本框时尝试启用或禁用按钮,并且它们彼此冲突,而逻辑需要处于更高级别。更改文本框验证功能以返回bool,并在ValidateAll中使用该功能确定是否启用该按钮:

private bool ValidateTextBox(TextBox tb, double save, out double valueToAssign, double compareTo, bool greaterThan)
{
    tb.BackColor = Color.Tomato;
    if (!Double.TryParse(tb.Text, out valueToAssign))
    {
        valueToAssign = save;
        return false;
    }
    if (greaterThan && (valueToAssign >= compareTo) || !greaterThan && (valueToAssign <= compareTo))
    {
        tb.BackColor = Color.White;
        return true;
    }
    else
    {
        //Rollback
        valueToAssign = save;
        return false;
    }
}

private void ValidateAll()
{
    btnOk.Enabled = 
        ValidateTextBox(tbMax, Max, out Max, Min, true) &
        ValidateTextBox(tbMin, Min, out Min, Max, false) &
        ValidateTextBox(tbMajStep, MajorStep, out MajorStep, MinorStep, true) &
        ValidateTextBox(tbMinStep, MinorStep, out MinorStep, MajorStep, false);
}