jrram
asked on
ErrorProvider component only sets for last control...why?
Here is my class:
public class superComboBox : ComboBox
{
private bool is_required = false;
private ErrorProvider ctrlErr = new ErrorProvider();
private string err_msg = "";
public bool Required
{
// Retrieves the value of the private variable is_required
get
{
return is_required;
}
// Stores the selected value in the private variable is_required
set
{
is_required = value;
}
}
public string ErrorMessage
{
// Stores the selected value in the private variable is_required
set
{
err_msg = value;
ctrlErr.SetError(this, err_msg);
}
}
}
And here is how I'm using it on my page:
private bool validateComboBox(superComb oBox ctrlToTest)
{
if (ctrlToTest.Required == true)
{
if (ctrlToTest.SelectedIndex == 0)
{
ctrlToTest.ErrorMessage = "The\"Select...\" option is not a valid choice.";
return false;
}
}
return true;
}
Let's say I have 3 drop-down controls on my form. If all 3 controls fail validation, I'd expect to see the red splat besides each control. But, it only appears on the last control and I can't figure out why. I know that you can't dynamically set the name for a error provider control so that's why I thought putting it in a class would help. To no avail though, so help me out people :-)
public class superComboBox : ComboBox
{
private bool is_required = false;
private ErrorProvider ctrlErr = new ErrorProvider();
private string err_msg = "";
public bool Required
{
// Retrieves the value of the private variable is_required
get
{
return is_required;
}
// Stores the selected value in the private variable is_required
set
{
is_required = value;
}
}
public string ErrorMessage
{
// Stores the selected value in the private variable is_required
set
{
err_msg = value;
ctrlErr.SetError(this, err_msg);
}
}
}
And here is how I'm using it on my page:
private bool validateComboBox(superComb
{
if (ctrlToTest.Required == true)
{
if (ctrlToTest.SelectedIndex == 0)
{
ctrlToTest.ErrorMessage = "The\"Select...\" option is not a valid choice.";
return false;
}
}
return true;
}
Let's say I have 3 drop-down controls on my form. If all 3 controls fail validation, I'd expect to see the red splat besides each control. But, it only appears on the last control and I can't figure out why. I know that you can't dynamically set the name for a error provider control so that's why I thought putting it in a class would help. To no avail though, so help me out people :-)
ASKER
That's strange, cause it works if I use it on multiple panel controls.
What do you mean by "it works if I use it on multiple panel controls"?
Does that mean that you have one error provider, and multiple controls with red error conditions?
Bob
Does that mean that you have one error provider, and multiple controls with red error conditions?
Bob
ASKER
I got it to work. All I had to do is declare my ErrorProvider declaration at the beginning of the program and it worked. I think the problem before was I kept re-defining the component.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Xml;
namespace ResizeMe
{
/// <summary>
/// Summary description for part1.
/// </summary>
public class actSurvey : System.Windows.Forms.Form
{
private System.Windows.Forms.Butto n cancel;
private System.Windows.Forms.TabPa ge tabPage1;
private System.Windows.Forms.TabPa ge tabPage2;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Panel processPanel;
private System.Windows.Forms.TabCo ntrol surveyBook;
private System.Windows.Forms.Butto n saveSurvey;
private System.ComponentModel.ICon tainer components;
private ErrorProvider formErrorHandler = new ErrorProvider();
private bool validateTabs()
{
int failedControls = 0;
foreach(TabPage tp in surveyBook.TabPages)
{
foreach(Control ctrl in tp.Controls)
{
if (ctrl.GetType() == typeof(superTextBox))
{
if (!validateTextBox((superTe xtBox) ctrl)) failedControls+=1;
}
else if (ctrl.GetType() == typeof(superComboBox))
{
if (!validateComboBox((superC omboBox) ctrl)) failedControls+=1;
}
else if (ctrl.GetType() == typeof(radioPanel))
{
if (!validateRadioPanel((radi oPanel) ctrl)) failedControls+=1;
}
}
}
return failedControls == 0 ? true : false;
}
private bool validateComboBox(superComb oBox ctrlToTest)
{
formErrorHandler.SetIconAl ignment(ct rlToTest, ErrorIconAlignment.TopRigh t);
formErrorHandler.SetError( ctrlToTest , String.Empty);
if (ctrlToTest.Required == true)
{
if (ctrlToTest.SelectedIndex == 0)
{
formErrorHandler.SetError( ctrlToTest , "The\"Select...\" option is not a valid choice. Please choose another option!"
+ "\n\nMore Info: " + ctrlToTest.MoreInfo);
return false;
}
}
return true;
}
}
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Xml;
namespace ResizeMe
{
/// <summary>
/// Summary description for part1.
/// </summary>
public class actSurvey : System.Windows.Forms.Form
{
private System.Windows.Forms.Butto
private System.Windows.Forms.TabPa
private System.Windows.Forms.TabPa
private System.Windows.Forms.Panel
private System.Windows.Forms.Panel
private System.Windows.Forms.Panel
private System.Windows.Forms.TabCo
private System.Windows.Forms.Butto
private System.ComponentModel.ICon
private ErrorProvider formErrorHandler = new ErrorProvider();
private bool validateTabs()
{
int failedControls = 0;
foreach(TabPage tp in surveyBook.TabPages)
{
foreach(Control ctrl in tp.Controls)
{
if (ctrl.GetType() == typeof(superTextBox))
{
if (!validateTextBox((superTe
}
else if (ctrl.GetType() == typeof(superComboBox))
{
if (!validateComboBox((superC
}
else if (ctrl.GetType() == typeof(radioPanel))
{
if (!validateRadioPanel((radi
}
}
}
return failedControls == 0 ? true : false;
}
private bool validateComboBox(superComb
{
formErrorHandler.SetIconAl
formErrorHandler.SetError(
if (ctrlToTest.Required == true)
{
if (ctrlToTest.SelectedIndex == 0)
{
formErrorHandler.SetError(
+ "\n\nMore Info: " + ctrlToTest.MoreInfo);
return false;
}
}
return true;
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Bob