[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

RequiredFieldValidator in VB.NET

Experts,
    I have used the RequiredFieldValidator when building .aspx pages, but I am new VS.NET 2003 and I'm trying to find an equivalent control to use on my Windows Forms. If anyone knows how I could validate my textboxes, or if there's already a prebuilt control I could use in VS.NET please let me know what it is and if possible give an example. Thanks in advance!!!

Jason
0
Yeavis
Asked:
Yeavis
  • 5
  • 3
1 Solution
 
Bob LearnedCommented:
With a control, like a TextBox, there is the Validating event, which you can handle, and check to see if the TextBox has a value, and cancel the validation using the event handler argument.

Bob
0
 
YeavisAuthor Commented:
Could you give an example just so I understand fully what you have just explained? Thanks..
0
 
tolgaongCommented:
To make the control easy, I recommend you to inherit the textbox according to your needs.
Let me give an example.
Let's say that, there are only textboxes you want to make mandatory (you can add other controls later by looking the below sample)

1) Firstly, add a new class to your project and name it textboxex. Write the below code in that file
Public Class textboxex
    Inherits TextBox
    Private pisMandatory As Boolean
    Private pWarningText As String

    Public Property isMandatory() As Boolean
        Get
            Return pisMandatory
        End Get
        Set(ByVal value As Boolean)
            pisMandatory = value
        End Set
    End Property

    Public Property WarningText() As String
        Get
            Return pWarningText
        End Get
        Set(ByVal value As String)
            pWarningText = value
        End Set
    End Property
End Class



2) Secondly, in your main form, write the below code to check all textboxes and give a message if there is a blank textbox
    Private Function checkFields(ByVal cont As Control) As Boolean
        Static valid As Boolean = True
        For Each ctl As Control In cont.Controls
            If TypeOf ctl Is textboxex Then
                If CType(ctl, textboxex).isMandatory AndAlso CType(ctl, textboxex).Text = "" Then
                    valid = False
                    MessageBox.Show(CType(ctl, textboxex).WarningText)
                End If
                If ctl.Controls.Count > 0 Then
                    checkFields(ctl)
                End If
            End If
        Next
        Return valid
    End Function

3) Under your save button -or whereever you want- call the function as
checkFields(Me)

4) Before running the program, for your previously added textboxes, from the "Designer generated code" region, change the
system.windows.forms.textbox
to
textboxex
for all occurance.

5) From the Design view, for the textboxes you want to be mandatory, select the control, from properties find the isMandatory property and set to true
And then write a text to the WarninText part such as: You should write something to this control.

You can enhance thw above code. This is just a quick sample.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
YeavisAuthor Commented:
Hey I appreciate it!! I'll give it a try and let you know if I have any problems...
0
 
YeavisAuthor Commented:
Okay, when I use the above code I am having problems. Once I change all occurances of System.Windows.Forms.Textbox to to System.Windows.Forms.textboxex all the textboxes on my form disappear and it tells me that "System.Windows.Forms.textboxex  is not defined"... any help would be appreciated.
0
 
YeavisAuthor Commented:
Alright... nevermind the above problem.. that was my fault I misunderstood.

The problem is that all the textboxes have dissappeared and I cannot complete step 5. Any idea's on how to get around this? They don't even appear in design view and I cannot change any properties because they do not exist.
0
 
tolgaongCommented:
here is the solution
instead of  System.Windows.Forms.Textbox, you wrote System.Windows.Forms.textboxex

you should write only textboxex.

an example is:

dim textbox1 as textboxex

textbox1 = new textboxex


after writing dim textbox1 as

the intelli sense will also show the word "textboxex".
0
 
YeavisAuthor Commented:
Yes I figured that part out, however even after correcting my mistake the textboxes are no longer present on the form. I changed all occurances of System.Windows.Forms.Textbox and I cannot access the properties once I save the changes. Will I have to manually create every textbox again???
0
 
tolgaongCommented:
Hımm, maybe, after wrong compilation, the designer generated code is deleted.
If you have a back-up, please use it.

If you don't have,
be sure that
1) Each textbox has a location, and size property set
2) each textbox is added to the correct containers control => me.controls.add(textbox1), etc
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now