If (null = boundField || 0 = boundField.Length) continue (control is ComboBox & (control as ComboBox).SelectedIndex = -1)) If ((control is TextBox & control.Text = "") || if the current control contains additional controls, run recursivelyĬheckNewRecordRequiredControls(control, provider) Public virtual void CheckNewRecordRequiredControls(Control ctl, ErrorProvider provider) If all you want to do is make sure a value has been entered, then you could use a procedure similar to ' CheckNewRecordRequiredControls', and if the control has a value then clear the WarningProvider like this: I check to see if the control is valid and then I clear the WarningProvider. I have event handlers for the ' Leave' event of my controls. Once the WarningProvider has been set, you do need to manage clearing it when it's appropriate. WarningProvider.SetError(control, " Required Field") not set up to handle bindings to anything // other than DataView or DataTable continue If ( is DataView)īoundTable = (Įlse if ( is DataTable) ![]() get the bound table - if we're bound to a dataview, // we need to get the table from that // otherwise just get to the DataTable string boundTable = string.Empty get bound field name string boundField =Ĭ if the current control contains additional controls, run recursively if ( > 0) / /// parent control that contains databound controls private void CheckNewRecordRequiredControls(Control ctl)įoreach (Control control in ctl.Controls) / within to see if they required a value / /// Recursively check all controls contained In your form/control, in the declarations section, add the following lines of code: Setting up the 'Warning Provider'įor the sake of this article, let's assume you have a strongly typed DataSet called AppDataSet, and that it contains a DataTable called Customer, with two required fields: Customer_Name and Customer_Type. Thirdly, if you don't want the default error icon for your 'warning', then add an ImageList control to your control/form, and add the 16x16 icon that you wish to use.įourthly, create a method called ' GetDataSet()' which will give you your DataSet object. If you're not using Strongly-Typed DataSets, the 'Warning Provider' will still work, as long as you have set the appropriate constraints. ![]() If you are not, please give yourself a little slap on the wrist, and then go read some articles about Strongly-Typed DataSets. Hopefully anyone reading this article is making full use of Strongly-Typed DataSets. Secondly, we will be checking constraints on our DataSet. So, you need to be using bound controls for this to work. Prerequisitesįirst of all, I need to point out that we will be accessing the DataBinding information on our controls. Since the icon and message associated with the ErrorProvider can easily be changed, I figured that would be a good way to create a 'Warning Provider'. I have always liked using the ErrorProvider as an easy way to alert the user that something is wrong. ![]() One way to do that is to 'point out' required fields, when they're adding new records. I like providing a friendly user interface for users of the applications I work on.
0 Comments
Leave a Reply. |