C#Winforms TextBox验证检查

时间:2017-02-22 16:52:53

标签: c# winforms

我在Windows窗体中有3对文本框: -

TxtboxSourceFolder1 -> TxtboxDestinationFolder1
TxtboxSourceFolder2 -> TxtboxDestinationFolder2
TxtboxSourceFolder3 -> TxtboxDestinationFolder3

我正在制作List<string> SrcFoldersList<string> DestFolders

现在我必须验证用户输入: -

1)TxtboxSourceFolder1是否有值,如果是,TxtboxDestinationFolder1中是否有相应的值?如果所有答案都是肯定的,并且值是合法的,则将它们添加到相应的列表中。然后重复一遍。

对我来说,似乎要检查文本框是否为空: -

    private int ConstructSourceDestinationFolderList()
    {
        if (Directory.Exists(txtboxSrcFolder1.Text) && Directory.Exists(txtboxDestFolder1.Text))
        {
            trans.szSourceFolderList.Add(txtboxSrcFolder1.Text.ToString());
            trans.szDestinationFolderList.Add(txtboxDestFolder1.Text);
        }
        else
        {
            return 1;
        }

        if (!String.IsNullOrWhiteSpace(txtboxSrcFolder2.Text))
        {
            if (Directory.Exists(txtboxSrcFolder2.Text) && Directory.Exists(txtboxDestFolder2.Text))
            {
                trans.szSourceFolderList.Add(txtboxSrcFolder2.Text);
                trans.szDestinationFolderList.Add(txtboxDestFolder2.Text);
            }
            else
            {
                return 1;
            }
        }


        if (!String.IsNullOrWhiteSpace(txtboxSrcFolder3.Text))
        {
            if (Directory.Exists(txtboxSrcFolder3.Text) && Directory.Exists(txtboxDestFolder3.Text))
            {
                trans.szSourceFolderList.Add(txtboxSrcFolder3.Text);
                trans.szDestinationFolderList.Add(txtboxDestFolder3.Text);
            }
            else
            {
                return 1;
            }
        }

        return 0;
    }

现在我必须为所有文本框做同样的事情。这看起来很麻烦。是否有更紧凑的方法来设计此验证?

0 个答案:

没有答案