Windows Develop Bookmark and Share   
 index > Windows Forms General > RequiredFieldValidator in WinForms
 

RequiredFieldValidator in WinForms

Hi all,
I am an ASP.NET app developer so don't mind this silly Q ;) 

I have many TextBox controls on my WinForm that is required (cannot be empty) and I want to validate this when the user clicks the OK button. In WebForms apps I can use the RequiredFieldValidator control that will do this for me using JavaScript on the client side. How can I simulate this in WinForms apps?

Thanks.
MigrationUser 1  Wednesday, February 05, 2003 3:28 AM
Use the ErrorProvider component.  It's about 2/3 of the way down the on Windows Forms toolbox tab.

 - mike
MigrationUser 1  Wednesday, February 05, 2003 3:51 AM
WOW. Thanks Mike.
MigrationUser 1  Wednesday, February 05, 2003 3:55 AM
Ok Mike. Another question.

Suppose I have 4 textbox controls that all should be filled before I hit my OK button. I want to be able on btnOK_Click event to investigate if any errorProvider was raised or not. In the case it was raised I want to show a MessageBox that mention that. How can I achieve this in WinForms app ?

TIA,
MigrationUser 1  Wednesday, February 05, 2003 10:58 AM
Private Function IsValid() As Boolean
''Checks ErrorProvider for all controls to determine if any are in error, returns results
''Assumes valid, exits if any field is in error
Dim blnIsValid As Boolean = True
Dim ctl As Control

For Each ctl In Controls
If errProvider.GetError(ctl) <> "" Then
Return False
End If
Next
''Is valid
Return True
End Function

This is assuming your ErrorProvider is name errProvider
MigrationUser 1  Wednesday, February 05, 2003 4:16 PM
I think this will do the job. I will try it.

Josh, thanks :) 
MigrationUser 1  Thursday, February 06, 2003 2:48 AM
Here's another approach (perhaps too brute force, but...). For some courseware I was writing for AppDev (www.appdev.com) , I created a custom control that emulates the behavior of the RequiredFieldValidator, for a TextBox control. Perhaps this will meet your needs? It is currently limited to a text box, but you could easily inherit from the base Control class and handle things a bit differently to expand it. THe point of this particular example was to be as simple as possible, but it might give you some ideas:

' From TextBoxRequired.vb:
Imports System.ComponentModel

Public Class TextBoxRequired
  Inherits System.Windows.Forms.TextBox

  Private mstrInitialValue As String

  ' If the control's value equals its initial
  ' value, it's not valid. Normally, the 
  ' initial value is an empty string--therefore,
  ' when validating the control, if the current
  ' value is the same as the initial value
  ' (that is, it's still empty) then the 
  ' control isn't valid.
  <Category("Behavior"), _
  Description("Specifies the value the control " & _
  "must differ from in order to validate.")> _
    Public Property InitialValue() As String
    Get
      Return mstrInitialValue
    End Get
    Set(ByVal Value As String)
      mstrInitialValue = Value
    End Set
  End Property

  ' Provide a way to determine if the control's
  ' text is valid, even if the client has turned
  ' off the CausesValidation property for the control.
  <Browsable(False)> _
  Public ReadOnly Property Valid() As Boolean
    Get
      Return IsValid()
    End Get
  End Property

  Private Function IsValid() As Boolean
    Return Me.Text <> mstrInitialValue
  End Function

  Protected Overrides Sub OnValidating(ByVal e As CancelEventArgs)
    e.Cancel = Not IsValid()
    MyBase.OnValidating(e)
  End Sub
End Class

MigrationUser 1  Monday, February 17, 2003 7:01 PM

You can use google to search for other answers

Custom Search

More Threads

• Panel scrolling issue
• .Net DateTime picker inaccurate timestamp
• Developing an install application in .Net
• WebBrowser Empty Document
• Newbie - ToolStripMenu not displayed fully .NET 2.0
• LockScreen, get and change Keys.
• DataGridView, troubles with DataGridViewComboBoxColumn
• (NEWB) Textbox to Listbox VBe'05
• Help please, will only take you a minute
• How to generate automatic last modified date and updated by when a record changes or a record is added