首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在点击按钮时搜索各种表单控件的完整性

如何在点击按钮时搜索各种表单控件的完整性
EN

Stack Overflow用户
提问于 2015-07-30 03:00:48
回答 1查看 29关注 0票数 0

感谢您浏览这篇文章。我只是一个C#和WPF的初学者,我的小项目正在迅速变得一团糟。我有一个WPF表单,我正在制作用于日程安排的表单。目前,我正在尝试清理我拼凑起来的一些代码。它的工作方式有点像我想要的,但还没有完全实现。我有一个组合框,文本框,日期和时间选择器的混合,一些控件是隐藏的,直到选择另有说明。我当前所在的位置是click按钮控件,它最终会将数据推送到我需要的位置。我想让单击按钮最初做的是:

查看所有控件,找到当前可见的控件。在可见控件中,检查它们是否为空/null。如果控件为空/null,则突出显示该字段并显示一条消息,指出“缺少必需的数据”。

我在网上看了看,我觉得循环浏览这些内容是一种更好的方式,然而,我普遍缺乏理解,这让我无法完成这项工作。正如我前面提到的,我有多种控件类型,其中一些来自Xceed WPF工具包。

这是我想出来的:

代码语言:javascript
复制
private void Submit_BT_Click(object sender, RoutedEventArgs e)
    {
        if (string.IsNullOrEmpty(PatientName.Text))
        {
            PatientName.Background = System.Windows.Media.Brushes.LightPink;
            System.Windows.Forms.MessageBox.Show("You are missing required data");
        }
        else
        {
            PatientName.Background = System.Windows.Media.Brushes.White;
        }

        if (string.IsNullOrEmpty(MRN.Text))
        {
            MRN.Background = System.Windows.Media.Brushes.LightPink;
            System.Windows.Forms.MessageBox.Show("You are missing required data");
        }
        else
        {
            MRN.Background = System.Windows.Media.Brushes.White;
        }

        if (string.IsNullOrEmpty(Procedure.Text))
        {
            Procedure.Background = System.Windows.Media.Brushes.LightPink;
            System.Windows.Forms.MessageBox.Show("You are missing required data");
        }
        else
        {
            Procedure.Background = System.Windows.Media.Brushes.White;
        }

        if (string.IsNullOrEmpty(Levels.Text))
        {
            Levels.Background = System.Windows.Media.Brushes.LightPink;
            System.Windows.Forms.MessageBox.Show("You are missing required data");
        }
        else
        {
            Levels.Background = System.Windows.Media.Brushes.White;
        }

        if (string.IsNullOrEmpty(Modality_Select.Text))
        {
            Modality_Select.Background = System.Windows.Media.Brushes.LightPink;
            System.Windows.Forms.MessageBox.Show("You are missing required data");
        }
        else
        {
            Modality_Select.Background = System.Windows.Media.Brushes.White;
        }

        if (string.IsNullOrEmpty(Insurance.Text))
        {
            Insurance.Background = System.Windows.Media.Brushes.LightPink;
            System.Windows.Forms.MessageBox.Show("You are missing required data");
        }
        else
        {
            Insurance.Background = System.Windows.Media.Brushes.White;
        }

        if (string.IsNullOrEmpty(Sched.Text))
        {
            Sched.Background = System.Windows.Media.Brushes.LightPink;
            System.Windows.Forms.MessageBox.Show("You are missing required data");
        }
        else
        {
            Sched.Background = System.Windows.Media.Brushes.White;
        }

        if (string.IsNullOrEmpty(OR_Num.Text))
        {
            OR_Num.Background = System.Windows.Media.Brushes.LightPink;
            System.Windows.Forms.MessageBox.Show("You are missing required data");
        }
        else
        {
            OR_Num.Background = System.Windows.Media.Brushes.White;
        }

        if (string.IsNullOrEmpty(Sched_Date.Text))
        {
            Sched_Date.Background = System.Windows.Media.Brushes.LightPink;
            System.Windows.Forms.MessageBox.Show("You are missing required data");
        }
        else
        {
            Sched_Date.Background = System.Windows.Media.Brushes.White;
        }


        if (Start_Time.Value == null)
        {
            Start_Time.Background = System.Windows.Media.Brushes.LightPink;
            System.Windows.Forms.MessageBox.Show("You are missing required data");
        }
        else
        {
            Start_Time.Background = System.Windows.Media.Brushes.White;
        }

        if (string.IsNullOrEmpty(Combobox1.Text))
        {
            Combobox1.Background = System.Windows.Media.Brushes.LightPink;
            System.Windows.Forms.MessageBox.Show("You are missing required data");
        }
        else
        {
            Combobox1.Background = System.Windows.Media.Brushes.White;
        }

        if (End_Time.Value == null)
        {
            End_Time.Background = System.Windows.Media.Brushes.LightPink;
            System.Windows.Forms.MessageBox.Show("You are missing required data");
        }
        else
        {
            End_Time.Background = System.Windows.Media.Brushes.White;
        }

        if (string.IsNullOrEmpty(Combobox2.Text))
        {
            Combobox2.Background = System.Windows.Media.Brushes.LightPink;
            System.Windows.Forms.MessageBox.Show("You are missing required data");
        }
        else
        {
            Combobox2.Background = System.Windows.Media.Brushes.White;
        }

        if (EMG_Type.IsVisible)
        {
            if (string.IsNullOrEmpty(EMG_Type.Text))
            {
                EMG_Type.Background = System.Windows.Media.Brushes.LightPink;
                System.Windows.Forms.MessageBox.Show("You are missing required data");
            }
            else
            {
                EMG_Type.Background = System.Windows.Media.Brushes.White;
            }

            if (Other_TB.IsVisible)
            {
                if (string.IsNullOrEmpty(Other_TB.Text))
                {
                    Other_TB.Background = System.Windows.Media.Brushes.LightPink;
                    System.Windows.Forms.MessageBox.Show("You are missing required data");
                }
                else
                {
                    Other_TB.Background = System.Windows.Media.Brushes.White;
                }
            }
        }

我如何通过循环遍历控件,只显示一条错误消息,同时高亮显示每个空/空的控件来使其工作?

感谢您对我的帮助。

帕特里克

EN

回答 1

Stack Overflow用户

发布于 2015-07-30 03:23:04

嗨,我个人不支持在WPF中验证这种类型的方法,在WPF中我们可以使用IDataErrorInfo来做这件事。

代码语言:javascript
复制
    public string Error
    {
       get
          {
               return null;
          }
    }


    public string this[string name]
    {
        ValidateProperty(name);
    }

public override void ValidateProperty(string propertyName)
    {
        Tracer.LogValidation("INotifyDataErrorInfo.ValidateProperty called. Validating " + propertyName);
        switch (propertyName)
        {
            case "x":
                {
                    ValidateNonNegative(x, "x");
                    ValidateMandatory(x, "x");
                }
            break;

        case "y":
            {
                ValidateNonNegative(y, "y");
                ValidateMandatory(y, "y");
            }
            break;
    }
    if (String.IsNullOrEmpty(propertyName))
    {
        Tracer.LogValidation("No cross-property validation errors.");
    }
  }

Validation-in-WPF

Validation-in-Windows-Presentation-Foundation

data-validation-in-3-5

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31708969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档