Inherited from TextBox and auto-validating Control
Inherited from TextBox and auto-validating Control
Inherited from TextBox and auto-validating Control
Inherited from TextBox and auto-validating Control
Inherited from TextBox and auto-validating Control
Inherited from TextBox and auto-validating Control Inherited from TextBox and auto-validating Control Inherited from TextBox and auto-validating Control Inherited from TextBox and auto-validating Control Inherited from TextBox and auto-validating Control Inherited from TextBox and auto-validating Control Inherited from TextBox and auto-validating Control Inherited from TextBox and auto-validating Control
Inherited from TextBox and auto-validating Control Inherited from TextBox and auto-validating Control
Inherited from TextBox and auto-validating Control
Go Back  Xtreme Visual Basic Talk > > > Inherited from TextBox and auto-validating Control


Reply
 
Thread Tools Display Modes
  #1  
Old 11-16-2011, 10:34 AM
Adel99 Adel99 is offline
Centurion
 
Join Date: Aug 2005
Posts: 160
Default Inherited from TextBox and auto-validating Control


Hello,

In almost all my projects, I frequently use TextBoxes that expect date values. I do a tedious job every time to handle the Validating event. The Validation event ensures that the value is a valid date value according to the current thread's Calendar, and if so, formats the date to "dd-MM-yyyy".

I want a TextBox (e.g. DateTextBox) that automatically runs validation and formats its Text property. All I have to do is dropping it to the form.

I know I should use inheritance; but I do not know where to put the auto-validation code; and I do not know how to save the inherited class as a control in the ToolBox.

I would appreciate any advice.

Adel,
Reply With Quote
  #2  
Old 11-16-2011, 11:15 AM
Adel99 Adel99 is offline
Centurion
 
Join Date: Aug 2005
Posts: 160
Default

OK, I've done the code part successfully and added the Control to the form using the Form's Designer.vb. However, still do not know how to add it to the ToolBox to make it easier to be added by dropping.
Reply With Quote
  #3  
Old 11-16-2011, 03:47 PM
PlausiblyDamp's Avatar
PlausiblyDampInherited from TextBox and auto-validating Control PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Could you not use the DateTimePicker control for date input?

If you are having problems with code it helps if you post the code in question here, it is much easier to debug stuff we can see.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #4  
Old 11-16-2011, 06:08 PM
jwshepherd jwshepherd is offline
Freshman
 
Join Date: May 2009
Location: West Texas
Posts: 45
Default

Quote:
Originally Posted by Adel99 View Post
OK, I've done the code part successfully and added the Control to the form using the Form's Designer.vb. However, still do not know how to add it to the ToolBox to make it easier to be added by dropping.
Did you use the usercontrol to code then textbox and vaidation? if so Once you build the project it will show up in the Toolbox Under {Project}Components While in Design View
Reply With Quote
  #5  
Old 11-17-2011, 01:56 AM
Adel99 Adel99 is offline
Centurion
 
Join Date: Aug 2005
Posts: 160
Default

Thank you so much for both of you.

PlausiblyDamp:
Quote:
Could you not use the DateTimePicker control for date input?
I tried that in the past and failed to make the DateTimePicker support the UmAlQuraCalendar. It seems it uses the Gregorian calendar only, if I'm not mistaken. I tried to change the CurrentThread's Calendar in the InitializeComponent() of the startup form of the project, but nothing changed regarding to DateTimePicker.

Code:
    Private Sub InitializeComponent()
        Threading.Thread.CurrentThread.CurrentCulture = New Globalization.CultureInfo("ar-SA")
        Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.Calendar = New Globalization.UmAlQuraCalendar
        Me.DateTimePicker1 = New System.Windows.Forms.DateTimePicker
'... rest of sub
I also changed the Calendar Windows used in the Control Panel, to see if that had any effect, with no luck.

Quote:
If you are having problems with code it helps if you post the code in question here, it is much easier to debug stuff we can see.
I'm sorry for that. I start a new Project > Add New Class > Name it DateTextBox > Write this code:
Code:
Public Class DateTextBox
    Inherits TextBox
    Private Sub Me_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.Validating
        If Text.Length < 1 Then Exit Sub
        Try
            Dim d As Date = Text
            Me.Text = d.ToString("dd-MM-yyyy")
            e.Cancel = False
        Catch ex As Exception
            MessageBox.Show("Invalid Date", "Invalid Date")
            e.Cancel = True
        End Try
    End Sub
End Class
It works perfectly as for validation and formatting according to the CurrentThread's calendar. (My application has a setting to use either Gregorian or UmAlQuraCalendar Calendars)

jwshepherd:
Quote:
Did you use the usercontrol to code then textbox and vaidation? if so Once you build the project it will show up in the Toolbox Under {Project}Components While in Design View
Thank you so much, jwshepherd. You are right; and the problem is solved. I can find it in the ToolBox > project's name Tab, as you said.

Attached is the VS 2008 project; and thanks to both of you for the second time.
Attached Files
File Type: zip DateTextBox.zip (15.4 KB, 3 views)
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Inherited from TextBox and auto-validating Control
Inherited from TextBox and auto-validating Control
Inherited from TextBox and auto-validating Control Inherited from TextBox and auto-validating Control
Inherited from TextBox and auto-validating Control
Inherited from TextBox and auto-validating Control
Inherited from TextBox and auto-validating Control Inherited from TextBox and auto-validating Control Inherited from TextBox and auto-validating Control Inherited from TextBox and auto-validating Control Inherited from TextBox and auto-validating Control Inherited from TextBox and auto-validating Control Inherited from TextBox and auto-validating Control
Inherited from TextBox and auto-validating Control
Inherited from TextBox and auto-validating Control
 
Inherited from TextBox and auto-validating Control
Inherited from TextBox and auto-validating Control
 
-->