If users are going to be typing dates as well as choosing from the dropdown calendar widget, where is the best event to trap the entered value, whether it was typed or picked, then warn users if the date fails some validation, and finally rollback the edit value to where it was if the user decides not to override the warning?
We need to allow dates in the past, but want to prevent accidental dates in the past, which typically occurs in the first few months of the new year after users have been accustomed to typing, say, 2011 for the entire year and then when the year changes to 2012, they type 2011 out of habit. So this validation would only be in effect for the first few months of the year, not year-round.
I don't see how to rollback the value in EditValueChanged. The args don't have a cancel option there. Is there another better event to do this that works with typed values and picked values?
Use the EditValueChanging event. It does have a Cancel event, along with NewValue and OldValue.
I prefer to use Validating event of Control, where i get CancelEventArgs with which you can set the value as well as the focus on the control. e.Cancel will set the focus on control.
eg:
if (txtName.Text == "")
{
txtName.ErrorText = "Client Name should not be blank.";
e.Cancel = true;
}
Related
I have a datatable (jobs) with a column in this table called StatusId. I have another datatable (jobStatus). The job table has 1 job record and the jobstatus table has all the job status.
On my winforms form I have the job record displayed and the job status is a combobox displaying the contents of the jobstatus table. The List portion (DisplayMember) is bound to the jobstatus table and the data portion is bound to the jobs table. (ValueMember) So all works well and when my jobs are selected and displayed the combobox selects the respective job status...All good.
Now I have another form (JobStatus) where I can added more job status records. So while I am entering / changing job records I discover I need another job status, so I jump over to my job status form and enter my new status. I then jump back to the job form and I now want to be able to select my new job status in the combobox.
My questions is what is the best practice for this senario where new records have been added or edited to a lookup style list which is used to populate a combobox on another form. I have tried putting code in the activate event of the form which works except the form flickers and it looks a ugly.
Any ideas???
I was virtually doing what your said, which made me look a bit closer at the code. The issue was the order I was processing the lines of code, and I found that when
old code
cboCustomer.DataSource = Business.Contact.GetContact( Enums.ContactType.Customer ).Tables[0];
sorted = ( ( DataTable ) cboCustomer.DataSource ).DefaultView;
sorted.Sort = "Name ASC";
new code here
DataView dv = Business.Contact.GetContact( Enums.ContactType.Customer ).Tables[0].DefaultView;
dv.Sort = "Name ASC";
DataTable dt = dv.ToTable();
cboCustomer.DataSource = dt;
works like a charm. Cheers for your help
You should be able to use the Activated event without a problem. That's even what the documentation says it is good for:
You can use this event for tasks such as updating the contents of the form based on changes made to the form's data when the form was not activated.
Of course, you'll get a few false positives when you subscribe to this event on the parent form. Consider the case where the user switches to the form without attempting to use the combo box control. It's not a huge deal, but in that case you will have updated the combo box for no reason. This is easily fixed by switching to handling the Enter event for the combo box control. This one only gets raised when that specific control receives the focus. The only limitation of the Enter event is that you can't change the focused control from inside of this event handler (but you don't want to do that anyway).
So now we need a fix for the flicker problem, which I think you'll find to be pleasantly simple. At the beginning of the event handler method (regardless of which one you use), before you start updating the combo box, call its BeginUpdate method. This prevents the control from flickering while it is being updated (by suppressing painting). At the end, you need to finish with a call to EndUpdate, which reenables painting and performs a single repaint to reflect your changes.
I just tested this myself, and I don't see any flicker. The most you'll get is a single flicker while the control gets repainted to reflect your changes. Obviously, you can't do any better than that.
In EF4, I want to show a confirmation message when user changes a particular column, saying something like "Are you sure you want to change it?", Yes/No. I want to accept this change only if user presses Yes and revert it back to original value if he presses No. I'm trying to do this in the partial method On*Column*Changing (such as OnStatusChanging) generated by EF. The method provides me the new incoming value, but I don't know how to "cancel" it, something equivalent to e.Handled = true, or e.Cancel = true.
BTW, I'm following this approach for validations/confirmations.
When I use the WPF 4.0 DatePicker with blackout date (or min/max), and the user enters a date that is blacked out by typing it in, the DatePicker reverts back to the original value.
This behavior is not the best for entering dates. If you key in the wrong year and tab out, it reverts back to the old value.
I'd like to be able to handle the error in code instead of just reverting back to the old value.
Winforms 2.0. Stick a DateTimePicker on a form. It defaults to TODAY. Click on the dropdown arrow to show the Calendar, and click on TODAY. The ValueChanged event DOES FIRE, even though it is already set to today.
What i would like to do is replicate this same functionality - I would like to reset it (in code) to today, and next time i click on today via the Calendar i want that ValueChanged event to fire (just like it did the first time).
If the above is not possible, i want some event that always fires whenever i pick a date on the Calendar (regardless if there is a change or no change).
Sounds really simple, surely someone has the answer?
What you're seeing there is a side-effect of the fact that DateTimePicker stores the time component of its value as well as the date.
So when your form first initializes, the DateTimePicker is being initialized with DateTime.Now. When you then select "Today" from its drop-down calendar, you're changing the value to DateTime.Today.
If you don't care about the time component and/or you're always reading the value as dateTimePicker1.Value.Date, then you could initialize the control in code with DateTime.Now and the ValueChanged event will fire when the user selects Today from the calendar. Otherwise you might consider catching the CloseUp event, which is fired whenever the calendar control is closed (regardless of whether the value changed).
Im not a win form programmer, but see if there is a Day_Render event.
That should fire for each day...at least there is one for web based development, but I would think it is the same. So look for Render() event.
I am using DateTimePicker with Australian date formatting set (dd/mm/yyyy)
The problem is when user enters a day which is outside of the month (mm), control resets that day to "1".
This obviously is a quite confusing obstacle for user - it's not natural to enter the month first and then enter the day [culture-wise]
Is there a way to disable this validation or postpone it till say,when control loses focus?
Thanks!
I've tried and I do not see anyway we can disable this for on micro-validation via the DateTimePicker's properties.
I know your frustration when the current date is "09/11/2009" (dd/MM/yyyy) and a user wants to enter "31/12/2009" via the keyboard from left to right.
Upon htting "31", it'll revert to "1" as Nov has only 30 days. It will show "2" if "32" is entered.
It forces the user to enter the month first followed by the day. I.e. entering in the direction of right, left, far-right.
My suggestion is use a more generic form of format like yyyy-MM-dd which though does not follow your regional/cultural format, still is easily understood by the user (or Aussie friends).
Another option might be to instantiate an edit control and overlay it on top of the date time picker's edit, and use the value to set the date time picker's value. Of course, you'll need to do some validation before doing the setting; and you'll also want to mimic the way the date time picker's edit field allows you to select the date, month and year individually.