OnValidating Event in a custom control - c#

When does the OnValidate event fire in the life-cycle of a control?
I'm creating a DateBox that will allow the user to enter a date in MM/DD/YYYY format (text) and need to verify that the date is in that format. It'll never be converted to a date (stored as string) but I would like to know the best time to validate that data (and provide feedback).
Note: It may seem a bit like re-inventing the wheel, but the app that I'm writing gets deployed to a tablet-pc and the winforms DateTimePicker is hell to edit with a stylus and my users just want to be able to write in the date.

MaskedTextBox Control might help you.

OnValidate happens after the Loading events (source)
You should validate on the client-side (javascript) AND server side either using the OnValidate or when handling the submission of the form (or both).

Related

Execute event after object value changes

I an using C# and .NET 3.5.
I am trying to do an object Validator that receives any kind of controllers and validates that the user is using it correctly. For example, the Validator receives a Textbox, the user changes the Text of the Textbox, the validator will notice this and will execute a check on it.
Is there anyway to do this? An event that is triggered as soon as one object from a list is changed?
There are several ways to do this. Read about two of them at
User Input Validation in Windows Forms
and
ErrorProvider Component (Windows Forms)

Textbox KeyDown Event in Visual Web Developer

I am trying to implement a KeyDown event for a textbox in Visual Web Developer. I am using C#. I know how to do this in a windows form but the technique isn't portable to VWD. I want to capture the text in the textbox when the user hits Enter.
Any advice is appreciated.
Regards.
Sounds like you may want to read up a bit on Web forms in general. A quick summary:
Since web pages are all client side, you have to explicitly tell it when to talk to the server where all the major lifting takes place.
So you have the html form tags:
<form>
</form>
and all important text boxes and other form controls go between.
Then you need a submit button which under normal circumstances is the only way to submit the form to the server for processing. (The "enter" key activates the submit key also.). Submission always either reloads the page or causes a move to the next page, depending on the actions specified.
ASP.NET does take care of a lot of page events and such for you. as you have probably noticed by now, though, when you right click a text box and look at the available events, you only have a few, such as "textchanged". This is because anytime you do not actually submit a form to the server, you need AJAX to do a call to the server for you while not reloading the page. the "textchanged" event on a textbox is still going to be AJAX driven - it's just the Microsoft has built it in for you. You will want to look at either jQuery or the ASP.Net AJAX libraries.
You say you want to "store" the result - is it to generate new behavior later on the page? that's AJAX. Is it for longevity while the entire application is worked through? That can wait until the submit.
Actually the textChanged method waits for the Enter key to be hit.

Best practice for visual validation of textbox control data

I think I want to show some kind of confirmation tick type thing by a textbox - (it's traditional windows forms stuff, not WPF) - but not sure if it's a bit naff. I would like some kind of slick way of showing that a value is incorrect or valid after some tests have been done i.e. a web service is valid with that name or SMTP server seems to running with that name etc.
Should there be even any visual stuff going on or should a simple message on a status strip at the bottom of the window be enough.....
Any ideas are most welcome.
PS - if the tick thing is a good idea what's the best way to implement this with a textbox control.
Example....
You could make a custom control which contains both a textbox and an imagebox. The custom control could raise a validation event which checks the text and then sets the imagebox graphic based on whether or not the validation passed (or sets it blank if there is not text in the textbox).
The .net centric way would probably be to implement validation providers and some type of custom error provider, like what Henrik is mentioning.
You can use ErrorProvider to show a little exclamation mark when the entered value is incorrect.
you can use the ErrorProviderComponent in order to show notifications. The naming of that component is slightly unfortunate in my mind but you can easily change the icon to show other things than the typical red error "X".

how to validate a textbox programmatically, that is at run time

i am creating textboxes when a button is clicked and again and again and so on
while accepting input i need to check if the boxes are all full,
i know how to do this using design view but how to do this using coding
that is add and validation control to the textbox when it created before initializing/adding it to the page.?
validation should be not null..
So as for validating the user input I would stay away from the ASP.Net Control Validators as hardly anyone in industry uses them. I would use the jQuery validator plugin which is included in a new Visual Studio project by default. You will still want Server side checking but it is much easier to call String.isNullOrEmpty(txtBox.Text) rather than using the Control Validators.

How do I disable some dates on a DateTimePicker control?

I was wondering is it possible to disable selected dates in a DateTimePicker, so that user cannot select them. i know its possible in web forms but in windows forms im unable to do this.how can i achieve this.
The ease with which you can do this will depend on the dates you want to restrict. For instance, if you all you want to do is specify a range of valid dates to pick, then you can use the MinDate and MaxDate properties to set the bounds. If however, you want to cherry pick certain days within a range (for instance, no weekends), there is no built in method for doing this.
You could either find a third party control with this functionality, or you could try to hack it a bit by adding an event handler to the ValueChanged event, and forcing the current date time to the last value (which you'd have to cache) if they user picked something that was illegal according to your business logic... but this is a less than ideal way to do it.
Developer Express controls are usually very flexible and judging from this support article you can achieve what you want to do with their DateEdit control.
The control collection can be obtained from the following location: Over 60 Free Controls from DevExpress. (the free offer is no longer available)
Don't forget to read the EULA.

Categories

Resources