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 Data Binding / December 2007

Tip: Looking for answers? Try searching our database.

DataBinding & ErrorProvider

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Marco Schwarz - 11 Dec 2007 08:38 GMT
Hi,

I add a ErrorProvider on a form

if (string.IsNullOrEmpty(txtPersonName.Text)) {             
    m_ErrorProvider.SetError(txtPersonName, "Name is incorrect");
} else {
        m_ErrorProvider.SetError(txtPersonName, null);
}

add a BindingList<Person> personList object to the class and set the
bindingobject.

txtPersonName.DataBindings.Add("Text", personList, "Name");

So when I leave the control the error provider show me the error, but
the person object have the wrong value.

How can I stop the assign of the wrong value?

Thanks
Marco
Morten Wennevik [C# MVP] - 12 Dec 2007 07:05 GMT
Hi Marco,

A couple of unclear things.  How do you run your if else statement?  What do
you mean by "person object have the wrong value".  An errorprovider does not
prevent you setting an incorrect value.  What you may be seeking is the
Control.Validating event where you can prevent the control from losing focus
if it does not contain a valid entry.  In my opinion it is better to be
notified of an incorrect value somewhere on a form and be allowed to continue
to enter values in other controls before correcting the error.

Signature

Happy Coding!
Morten Wennevik [C# MVP]

> Hi,
>
[quoted text clipped - 18 lines]
> Thanks
> Marco
Marco Schwarz - 15 Dec 2007 09:28 GMT
Morten Wennevik [C# MVP] schrieb:
> Hi Marco,
>
[quoted text clipped - 5 lines]
> notified of an incorrect value somewhere on a form and be allowed to continue
> to enter values in other controls before correcting the error.

That's right. I can check, before Action how SAVE or UPDATE was fired,
if the UI hase invalidate controls and so I can stop the user. So I have
2 validate-methode!

1. UI (ErrorProvider)
2. BussinesObject (Methode Save with NHibernate).

The UI + BussinesObject is for the user and only BussinesObject is for a
batch insert.

Thanks
Marco
Morten Wennevik [C# MVP] - 17 Dec 2007 06:57 GMT
Hi Marco,

You didn't really answer my questions so I'm still not sure what kind of
setup you got.  In any case, I have included a demonstration sample with
databinding against a business object (Person) and an ErrorProvider.  I have
not used the validating event, but person.ValidateName can be used for
validation checks before saving/closing.  Create a blank form called Form1
and replace the code with the following:

   public partial class Form1 : Form
   {
       private ErrorProvider errorProvider = new ErrorProvider();
       private BindingList<Person> list = new BindingList<Person>();
       private TextBox textBox1 = new TextBox();
       private ListBox listBox1 = new ListBox();

       public Form1()
       {
           InitializeComponent();
       }

       protected override void OnLoad(EventArgs e)
       {
           // Create some dummy data to display
           list.Add(new Person("Alfa"));
           list.Add(new Person("Beta"));
           list.Add(new Person("Gamma"));
           list.Add(new Person("All"));

           // Databindings
           listBox1.DataSource = list;
           listBox1.DisplayMember = "Name";
           textBox1.DataBindings.Add("Text", list, "Name");

           // We'll update the errorprovider when the text is changed
           textBox1.TextChanged += new EventHandler(textBox1_TextChanged);

           // Add the controls
           Controls.Add(textBox1);
           listBox1.Location = new Point(0, 30);
           Controls.Add(listBox1);
       }

       void textBox1_TextChanged(object sender, EventArgs e)
       {
           // Get the selected person and display any error text it contains
           Person person = listBox1.SelectedItem as Person;
           if(person != null)
               errorProvider.SetError(sender as Control, person.NameError);
       }

       // Our business object
       class Person
       {
           private string _name;
           private string _nameError;

           public string Name
           {
               get { return _name; }
               set
               {
                   if (_name != value)
                   {
                       _name = value;
                       // Any changes to the Name property
                       // will call the ValidateName method
                       ValidateName();
                   }
               }
           }

           // This property contains any error text for this business object
           public string NameError
           {
               get { return _nameError; }
               set
               {
                   if (_nameError != value)
                   {
                       _nameError = value;
                   }
               }
           }

           public Person(string name)
           {
               this.Name = name;
           }

           // This method has a double function
           // When called it will update NameError with the proper error text
           // but it will also let the caller know if the business object
is valid
           // 'if(person.ValidateName()){ SavePerson(person); }'
           public bool ValidateName()
           {
               if (!Name.StartsWith("A"))
               {
                   NameError = "Name must start with A";
                   return false;
               }
               else
               {
                   NameError = "";
                   return true;
               }
           }
       }
   }

Signature

Happy Coding!
Morten Wennevik [C# MVP]

> Morten Wennevik [C# MVP] schrieb:
> > Hi Marco,
[quoted text clipped - 19 lines]
> Thanks
> Marco
Marco Schwarz - 18 Dec 2007 13:10 GMT
Sorry now I unterstand answer, thanks

Morten Wennevik [C# MVP] schrieb:
> Hi Marco,
>
[quoted text clipped - 106 lines]
>         }
>     }

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.