Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / November 2006

Tip: Looking for answers? Try searching our database.

Validating event in databound controls

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Martin Stettner - 13 Nov 2006 14:55 GMT
Hi,

I've created a custom control derived from TextBox, which internally parses
the Text to a decimal value (in the OnValidated method) and exposes this
value as a Value property.
The control handles invalid input in an overwritten OnValidating method by
setting the Cancel member of the CancelEventArgs parameter to true (and by
changing the background color of the control).

I now want to bind the Value property of my control to some class property.
When I do so, i get two issues:

First, if I set the update mode to DataSourceUpdateMode.OnValidation and
enter a value in my text box, I have to leave the textbox twice using TAB key
(so I must press TAB, go back to the text box and press TAB again) before the
value is accepted. When the update mode is
DataSourceUpdateMode.OnPropertyChanged, overything goes fine.

Second, If I enter a wrong value into the text box, the focus still moves on
to the next control instead of staying in my text box, although the form's
AutoValidate property is set to EnablePreventFocusChange.

I would like my databound control to behave like the other (non-bound)
controls. Do you have any ideas what's happening? Does the databinding
mechanism influence/bypass the normal validation cycle in any way? Should I
use another method to parse the Text property (and set my new Value property)?

Thank you in advance
Bart Mermuys - 13 Nov 2006 19:46 GMT
Hi,

> Hi,
>
[quoted text clipped - 29 lines]
> use another method to parse the Text property (and set my new Value
> property)?

I'm not sure how to reproduce your exact problem.  But DataBinding does
attach an eventhandler to the Validating event inwhere it will push control
values to datasource values but only if they are changed (if the property
has a similar changed event).

1) If you are overriding OnValidating, then you should call
Base.OnValiditing at the end since this will trigger the Validating event
and a data push from control to source.  Something like this seems to work:

public class Class1 : TextBox
{
 private Decimal val;

 public event EventHandler ValueChanged;

 public Decimal Value
 {
   get { return val; }
   set
   {
      if (val != value)
      {
         val = value;
         Text = val.ToString();
         if (ValueChanged != null)
           ValueChanged(this, EventArgs.Empty);
      }
    }
 }

 protected override void OnValidating(System.ComponentModel.CancelEventArgs
e)
 {
    decimal result;
    if (decimal.TryParse(Text, out result))
    {
       Value = result;
       BackColor = System.Drawing.Color.White;
    }
    else
   {
       BackColor = System.Drawing.Color.Red;
       e.Cancel = true;
   }
   base.OnValidating(e);
 }
}

2) But if you would bind to a normal TextBox Text property,  then
DataBinding will try to parse the Text into a Decimal also and if it fails
you can do something inside Binding.BindingComplete eventhandler:

textBox1.DataBindings["Text"].BindingComplete += new
BindingCompleteEventHandler(Form3_BindingComplete);

void Form3_BindingComplete(object sender, BindingCompleteEventArgs e)
{
 if (e.BindingCompleteContext == BindingCompleteContext.DataSourceUpdate)
 {
  if (e.BindingCompleteState != BindingCompleteState.Success)
     e.Binding.Control.BackColor = System.Drawing.Color.Red;
  else
     e.Binding.Control.BackColor = System.Drawing.Color.White;
 }
}

For both cases to work properly you need to make sure formatting is enabled
(code or desinger) (Binding.FormattingEnabled).

HTH,
Greetings

> Thank you in advance

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.