CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    120

    Databinding object to a Windows Form

    Project type is Windows forms application.

    I have the following issue:

    I want to unit test my GUI as far as I can, thus I wrote:

    Code:
    namespace Presentation
    {
    	public class LoginForm : INotifyPropertyChanged
    	{
    		private string _password = string.Empty;
    
    		// Declares an event to fire, should we want to report that a property has changed
    		public event PropertyChangedEventHandler PropertyChanged;
    
    
    		// Case sensitive comparison is performed on the password prior to setting the new value
    		public string Password
    		{ 
    			get { return _password; }
    			set 
    			{
    				if( string.Compare( _password, value, false ) != 0 )
    				{
    					_password = value;
    					if( PropertyChanged != null )
    					{
    						PropertyChanged( this, new PropertyChangedEventArgs( "Password" ) );
    					}
    				}
    			}
    		}
    
    
    		public bool OkButtonEnabled
    		{
    			get
    			{
    				return ! ( string.IsNullOrEmpty( Username ) || string.IsNullOrEmpty( Password ) );
    			}
    		}
    	}
    }
    (Code chopped for legibility)

    Now, I have a corresponding windows form that I want to use this class with, I create the button and text box, and find the databinding controls.

    The first time that I reference it, I am asked to create a data binding source, no problem, I navigate through the namespaces and end up with a loginFormBindingSource object.

    Then I proceed. In properties for the Password, I bind the text property to the loginFormBindingSource's Password property.

    For the button, I go to "advanced" and choose to bind the "Enabled" property to the "OkButtonEnabled" property in the binding source.

    Compiling and running is without error, however, the data binding does not work. I must be missing something important, like a constructor, so I tried
    to initialize the binding source, but to no avail.

    If I remove all GUI bindings and instead opt to bind manually in the form's constructor like this:

    Code:
    public partial class Form1 : Form
    {
    	LoginForm codeBehindLoginForm	 = new LoginForm( );
    
    	public Form1()
    	{
    		InitializeComponent();
    
    					
    		tbPassword.DataBindings.Add	( "Text",	 codeBehindLoginForm, "Password" );
    		btnOk.DataBindings.Add		( "Enabled", codeBehindLoginForm, "OkButtonEnabled" );			
    	}
    }
    ...then everything works as expected.

    Can anyone please enlighten me as to how I can get the GUI binding, using properties to work? I am almost sure that the only thing missing is some form of initalization, I'm just not sure where. A search here, and on google returns only examples of binding to data sources.
    In advance, thanks for reading.

  2. #2
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    120

    Re: Databinding object to a Windows Form

    Hmm, a collegue of mine heard my frustration and offered me the solution:


    Even though the loginFormBindingSource is created and specified in GUI, it is not initialized automatically in codebehind, so the missing thing was this:

    Code:
    		public Form1()
    		{
    			InitializeComponent();
    
    			loginFormBindingSource.Add( new LoginForm( ) );
    		}
    Hoping someone else will find this useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured