Search code examples
c#winformserrorprovider

ErrorProvider does not display error message


  1. In the following code I used errorProvider.SetError(control, message) to display the message, but only the Icon is shown, the message is not shown, what is wrong?
  2. Is there a way to adjust the left margin of the error message only? (I know you can SetIconPadding, but I only want left margin to be changed)

    public static DialogResult ShowDialog()
    {
      var inputBox = new Form { ClientSize = new Size(520, 225), FormBorderStyle = FormBorderStyle.FixedDialog };        
      var panel = new TableLayoutPanel { Size = new Size(460, 100), Location = new System.Drawing.Point(45, 15) };        
      var errorProvider = new ErrorProvider { Icon = SystemIcons.Exclamation, BlinkStyle = ErrorBlinkStyle.NeverBlink };        
      errorProvider.SetIconAlignment(panel, ErrorIconAlignment.BottomLeft);
    
      var okButton = new Button
      {
        Size = new System.Drawing.Size(70, 30),
        Location = new Point(330, 180),
        Text = "OK"
      };
    
      okButton.Click += new EventHandler((sender, e) => { errorProvider.SetError(panel, "Test Error"); });
      inputBox.Controls.Add(panel);
      inputBox.Controls.Add(okButton);
    
      return inputBox.ShowDialog();
    }
    

Solution

  • Let me explain about ErrorProvider.

    1. ErrorProvider in Windows Application has following behaviour.

      • It will display error icon as per configuration.
      • It will display error message that you have set once you put your mouse cursor on it.
    2. The behaviour you want is too display error message along with icon.

      • There is one solution build your own control just like ErrorProvider.