Announcement

Collapse
No announcement yet.

ErrorProvider

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • ErrorProvider

    An die Expertenrunde:

    In der letzten Ausgabe des Entwicklers war der Einsatz des ErrorProviders beschrieben.
    Gibt es eine Möglichkeit, aus dem Textfeld, welches der ErrorProvider gerade mit dem rotem Blinker versehen hat, mit einem Button "Abbrechen" wieder herauszukommen?

  • #2
    Du kannst bei Button Objekten ausschalten das sie eine Validierung durchführen soll mit dem Property "CausesValidation" oder so ähnlich (habs aus dem Kopf -> Gedächtnis geschrieben

    Comment


    • #3
      So einfach geht das nicht. Der Cursor steht eisern im Textfeld. Selbst das Programm zu beenden ist nicht möglich.

      Otto K

      Comment


      • #4
        Hallo,

        >So einfach geht das nicht.

        es geht so einfach! Das folgende Beispiel zeigt, dass der über <b>ShowDialog</b> modal angezeigte Dialog trotz Fehlermeldung des ErrorProviders über den Abbrechen-Button geschlossen werden kann:

        A) Hauptformular Form1:
        <pre>
        using System;
        using System.Drawing;
        using System.Collections;
        using System.ComponentModel;
        using System.Windows.Forms;
        using System.Data;

        namespace ErrorProviderDemo
        {
        /// <summary>
        /// Summary description for Form1.
        /// </summary>
        public class Form1 : System.Windows.Forms.Form
        {
        private System.Windows.Forms.Button button1;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form1()
        {
        //
        // Required for Windows Form Designer support
        //
        InitializeComponent();

        //
        // TODO: Add any constructor code after InitializeComponent call
        //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
        if( disposing )
        {
        if (components != null)
        {
        components.Dispose();
        }
        }
        base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
        this.button1 = new System.Windows.Forms.Button();
        this.SuspendLayout();
        //
        // button1
        //
        this.button1.Location = new System.Drawing.Point(16, 24);
        this.button1.Name = "button1";
        this.button1.TabIndex = 0;
        this.button1.Text = "button1";
        this.button1.Click += new System.EventHandler(this.button1_Click);
        //
        // Form1
        //
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(292, 266);
        this.Controls.Add(this.button1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
        Application.Run(new Form1());
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
        Form2 aFrm2 = new Form2();
        aFrm2.ShowDialog();
        }
        }
        }
        </pre>

        B) Dialog-Formular Form 2:

        <pre>
        using System;
        using System.Drawing;
        using System.Collections;
        using System.ComponentModel;
        using System.Windows.Forms;

        namespace ErrorProviderDemo
        {
        /// <summary>
        /// Summary description for Form2.
        /// </summary>
        public class Form2 : System.Windows.Forms.Form
        {
        private System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.ErrorProvider errorProvider1;
        private System.Windows.Forms.Button buttonCancel;
        private System.Windows.Forms.Button buttonOK;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form2()
        {
        //
        // Required for Windows Form Designer support
        //
        InitializeComponent();

        //
        // TODO: Add any constructor code after InitializeComponent call
        //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
        if( disposing )
        {
        if(components != null)
        {
        components.Dispose();
        }
        }
        base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
        this.buttonCancel = new System.Windows.Forms.Button();
        this.buttonOK = new System.Windows.Forms.Button();
        this.textBox2 = new System.Windows.Forms.TextBox();
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.errorProvider1 = new System.Windows.Forms.ErrorProvider();
        this.SuspendLayout();
        //
        // buttonCancel
        //
        <b>this.buttonCancel.CausesValidation = false;
        this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;</b>
        this.buttonCancel.Location = new System.Drawing.Point(144, 112);
        this.buttonCancel.Name = "buttonCancel";
        this.buttonCancel.TabIndex = 7;
        this.buttonCancel.Text = "Abbrechen";
        //
        // buttonOK
        //
        this.buttonOK.DialogResult = System.Windows.Forms.DialogResult.OK;
        this.buttonOK.Location = new System.Drawing.Point(48, 112);
        this.buttonOK.Name = "buttonOK";
        this.buttonOK.TabIndex = 6;
        this.buttonOK.Text = "buttonOK";
        //
        // textBox2
        //
        this.textBox2.Location = new System.Drawing.Point(24, 56);
        this.textBox2.Name = "textBox2";
        this.textBox2.TabIndex = 5;
        this.textBox2.Text = "textBox2";
        //
        // textBox1
        //
        this.textBox1.Location = new System.Drawing.Point(24, 24);
        this.textBox1.Name = "textBox1";
        this.textBox1.TabIndex = 4;
        this.textBox1.Text = "textBox1";
        this.textBox1.Validating += new System.ComponentModel.CancelEventHandler(this.text Box1_Validating);
        //
        // errorProvider1
        //
        this.errorProvider1.ContainerControl = this;
        //
        // Form2
        //
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(292, 174);
        this.Controls.Add(this.buttonCancel);
        this.Controls.Add(this.buttonOK);
        this.Controls.Add(this.textBox2);
        this.Controls.Add(this.textBox1);
        this.Name = "Form2";
        this.Text = "Form2";
        this.ResumeLayout(false);

        }
        #endregion

        private void textBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
        if (textBox1.Text.Length != 5 )
        {
        this.errorProvider1.SetError(textBox1, "Bitte 5stellig!");
        e.Cancel = true;
        }
        else
        {
        this.errorProvider1.SetError(textBox1, "");
        }

        }
        }
        }

        </pre&gt

        Comment


        • #5
          Vielen Dank, habe es gerade ausprobiert. Es war "relativ" einfach.
          Otto K

          Comment

          Working...
          X