c# - 使用基于文本文件的事件创建 C# 表单字段

标签 c# winforms code-generation

我需要根据我正在解析的文本文件生成 Windows 窗体字段。我还想自动生成验证事件处理程序,它们基本上都是相同的,只是表单字段和用于验证该字段的正则表达式会发生变化。

我很难找到执行此操作的最快和最简单的方法。

任何指向正确方向的指示都会很棒,甚至可能是我遗漏的一些关键字。我只能想象这种类型的问题以前已经涉及过,我只是不知道我到底在寻找什么。

最佳答案

我做过类似的事情,使用 XML 在运行时生成控件

我发现最简单的方法是创建自定义控件以允许轻松验证。

我还收集有关控件的详细信息,以便能够将字段的值返回到网络服务

我是这样实现的

  public bool IsFieldValid()
    {
             Error.SetError(label, "");
        if (_Validate.Length == 0)
        {
         return true;
        }

        else 
        {
            switch (_Validate)
            { 
                case "General":
                    return ValidateText();
                case "Int":
                    return ValidateInt();
                case "Decimal":
                    return ValidateDecimal();
                case "Length":
                    return ValidateTextLength();
                default:
                    return true;
            }


        }


    }


    public bool ValidateText()
    {

            this.shapeContainer1.BackColor = this.BackColor;
            if (_CanBeNull == true & this.textBox.Text.Trim() == "")
            {

                return true;
            }
            else
            {
                if (this.textBox.Text.Trim().Length > 0)
                {

                    return true;
                }
                else
                { 
                    Error.SetError(label, CustomError);
                    return false;
                }

        }
    }

    public bool ValidateDecimal()
    {

        if (_CanBeNull == true & this.textBox.Text.Trim() == "")
        {
            return true;
        }
        else
        {
            try
            {
                this.textBox.Text = this.textBox.Text.Replace("£", "");
                Decimal Val = System.Convert.ToDecimal(this.textBox.Text);
                if (_MaxValue == 0)
                {
                    return true;
                }

                if ((Val >= _MinValue) && (Val <= _MaxValue))
                {
                    return true;
                }
                else
                {

                    this.Error.SetError(this.label, "Value must be between " + _MinValue.ToString() + " and " + _MaxValue);
                    return false;

                }


            }
            catch
            {
                this.Error.SetError(this.label, "Error converting value to a decimal value");
                return false;
            }



        }

    }

    public bool ValidateInt()
    {
        if (_CanBeNull == true & this.textBox.Text.Trim() == "")
        {
            return true;
        }
        else
        {
            try
            {
                int Val = System.Convert.ToInt32(this.textBox.Text);
                if (_MaxValue == 0)
                {
                    return true;
                }

                if ((Val >= _MinValue) && (Val <= _MaxValue))
                {

                    return true;
                }
                else
                {

                    this.Error.SetError(label,"Value must be between " + _MinValue.ToString() + " and " + _MaxValue);
                    return false;

                }


            }
            catch
            {
                this.Error.SetError(label,"Error converting value to a numeric value");
                return false;
            }



        }
    }

    public bool ValidateTextLength()
    {
        if (_CanBeNull == true & this.textBox.Text.Trim() == "")
        {
            return true;
        }
        else
        {

            int Val = this.textBox.Text.Length;
            if ((Val >= _MinValue) && (Val <= _MaxValue))
            {
                return true;
            }
            else
            {

                this.Error.SetError(label,"Length of text must be between " + _MinValue.ToString() + " and " + _MaxValue);
                return false;

            }





        }
    }


    public String TxtReturnXML
    { 
        get {
            string S;
            XmlDocument doc = new XmlDocument();
            XmlNode Xe = doc.CreateNode(XmlNodeType.Element,"Value",null);
            Xe.InnerText = this.textBox.Text.ToString();
            S = Xe.OuterXml;
            doc = null;
            Xe = null;
            return S;


        }



    }

    public bool LoadedXml(XmlNode X)
    {
        try
        {
            _Validate = X.Attributes["Validate"].Value;

            this.textBox.Text = X.Attributes["DefaultValue"].Value.ToString();
            this.label.Text = X.Attributes["LabelText"].Value.ToString();

            if (System.Convert.ToInt32(X.Attributes["Height"].Value) > 0)
            {
                this.Height = System.Convert.ToInt32(X.Attributes["Height"].Value) + 10;
                textBox.Multiline = true;
                this.textBox.Height = System.Convert.ToInt32(X.Attributes["Height"].Value);
            }
            if (System.Convert.ToInt32(X.Attributes["Width"].Value) > 0)
            {
                this.textBox.Width = System.Convert.ToInt32(X.Attributes["Width"].Value);
                this.textBox.Left = (this.Width - 20) - System.Convert.ToInt32(X.Attributes["Width"].Value);
            }
            _CanBeNull = System.Convert.ToBoolean(X.Attributes["CanBeNull"].Value);
            _PassBack = System.Convert.ToBoolean(X.Attributes["PassBack"].Value);

            CustomError = X.Attributes["CustomError"].Value.ToString();
            CustomWarning = X.Attributes["CustomWarning"].Value.ToString();
            if (CustomWarning.Length > 0)
            {
                Error.SetError(label, CustomWarning);

            }


            _MinValue = System.Convert.ToInt32(X.Attributes["MinValue"].Value);
            _MaxValue = System.Convert.ToInt32(X.Attributes["MaxValue"].Value);



        }
        catch {
            return false;
        }

            _LoadedXml = X;
            return true;



}

您还可以添加事件句柄以在特定事件(即模糊)中进行验证

希望对你有帮助

关于c# - 使用基于文本文件的事件创建 C# 表单字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2990262/

相关文章:

orm - 代码生成器还是 ORM?

c# - 如何在 Azure Functions 中设置 HTTP 输出

c# - View 模型 MVC 3

c# - 在 Main() 方法中捕获异常

validation - UML 模型的语义方面如何用于代码生成、有效性检查和复杂性度量?

c++ - 编译时检查函数是否被使用/未使用 c++

c# - 将第二种形式准确地显示在第一种形式的位置

c# - 应用程序设置可靠吗?

c# - 如何重命名 TreeView 中的节点?

c# - 如何将 PictureBox 用作按钮