Simple Binding

I l @ ve RuBoard

As previously mentioned, simple binding is the business of taking a simple chunk of data and tying it to a user control. When the data changes, the control reflects that change. When the control's display is edited, the data changes if write access is enabled.

Each Windows Forms control maintains a BindingContext object that, in turn , has a collection of CurrencyManager objects. By the way, the CurrencyManager has nothing to do with financial exchanges. It simply maintains a current position within a particular data object for you. Child controls, such as the GroupBox , can also have their own independent BindingContexts and CurrencyManagers .

The illustration in Figure 3.3.1 shows the relationships between Form, Control, BindingContext and CurrencyManager .

Figure 3.3.1. The relationship between form, BindingManager , and CurrencyManager .

graphics/0303fig01.gif

Let's take a look at using the BindingManager and CurrencyManager with a simple application that binds a text box's Text property to a collection of strings. Listing 3.3.1 shows the DataBound application source code.

Listing 3.3.1 databound.cs : Simple Binding of Controls to Data
 1: namespace Sams   2: {   3:     using System;   4:     using System.Drawing;   5:     using System.Collections;   6:   using System.Collections.Specialized;   7:     using System.ComponentModel;   8:     using System.Windows.Forms;   9:  10:     /// <summary>  11:     ///    Summary description for databound.  12:     /// </summary>  13:     public class databound : System.Windows.Forms.Form  14:     {  15:     private Button RightButton;  16:     private Button LeftButton;  17:     private TextBox textBox2;  18:     private TextBox textBox1;  19:     private Button DoneButton;  20:  21:     private StringCollection sa;  22:  23:         public databound()  24:         {  25:       this.textBox2 = new TextBox();  26:       this.RightButton = new Button();  27:       this.textBox1 = new TextBox();  28:       this.DoneButton = new Button();  29:       this.LeftButton = new Button();  30:       this.SuspendLayout();  31:       //  32:       // textBox2  33:       //  34:       this.textBox2.Location = new Point(184, 16);  35:       this.textBox2.Name = "textBox2";  36:       this.textBox2.TabIndex = 2;  37:       this.textBox2.Text = "textBox2";  38:       //  39:       // RightButton  40:       //  41:       this.RightButton.Location = new Point(192, 64);  42:       this.RightButton.Name = "RightButton";  43:       this.RightButton.TabIndex = 4;  44:       this.RightButton.Text =">>";  45:       this.RightButton.Click += new EventHandler(this.RightButton_Click);  46:       //  47:       // textBox1  48:       //  49:       this.textBox1.Location = new Point(8, 16);  50:       this.textBox1.Name = "textBox1";  51:       this.textBox1.TabIndex = 1;  52:       this.textBox1.Text = "textBox1";  53:       //  54:       // DoneButton  55:       //  56:       this.DoneButton.Location = new Point(104, 64);  57:       this.DoneButton.Name = "DoneButton";  58:       this.DoneButton.TabIndex = 0;  59:       this.DoneButton.Text = "Done";  60:       this.DoneButton.Click += new EventHandler(this.DoneButton_Click);  61:       //  62:       // LeftButton  63:       //  64:       this.LeftButton.Location = new Point(16, 64);  65:       this.LeftButton.Name = "LeftButton";  66:       this.LeftButton.TabIndex = 3;  67:       this.LeftButton.Text = "<<";  68:       this.LeftButton.Click += new EventHandler(this.LeftButton_Click);  69:       //  70:       // databound  71:       //  72:       this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);  73:       this.ClientSize = new System.Drawing.Size(294, 111);  74:       this.ControlBox = false;  75:       this.Controls.AddRange(new Control[] {  76:                     this.RightButton,  77:                     this.LeftButton,  78:                     this.textBox2,  79:                     this.textBox1,  80:                     this.DoneButton} );  81:       this.FormBorderStyle = FormBorderStyle.FixedDialog;  82:       this.Name = "databound";  83:       this.ShowInTaskbar = false;  84:       this.Text = "databound";  85:       this.ResumeLayout(false);  86:  87:       //This is the setup code for the simple binding example  88:       //We have bound two text controls to the same StringCollection  89:  90:       // This is setting up the "database" (an IList thing)  91:       sa=new StringCollection();  92:       sa.Add("Hello databinding");  93:       sa.Add("Sams publishing");  94:       sa.Add("C# example");  95:       sa.Add("By Bob Powell");  96:  97:       //This binds the controls to that database.  98:       this.textBox1.DataBindings.Add("Text",sa,"");  99:       this.textBox2.DataBindings.Add("Text",sa,""); 100:       //See the button handlers below for details 101:       //on how to move through the data. 102:         } 103: 104:         /// <summary> 105:         ///    Clean up any resources being used. 106:         /// </summary> 107:         public override void Dispose() 108:         { 109:             base.Dispose(); 110:         } 111: 112: 113:     //Very simply increments the Position property of the CurrencyManager 114:     protected void RightButton_Click (object sender, System.EventArgs e) 115:     { 116:       //Note how updating one position affects all 117:       //controls bound to this data 118:       this.BindingContext[sa].Position++; 119:     } 120: 121:     //Very simply decrements the Position property of the CurrencyManager 122:     protected void LeftButton_Click (object sender, System.EventArgs e) 123:     { 124:       //Note how updating one position affects all 125:       //controls bound to this data 126:       this.BindingContext[sa].Position--; 127:     } 128: 129:     protected void DoneButton_Click (object sender, System.EventArgs e) 130:     { 131:       Application.Exit(); 132:     } 133: 134:     public static void Main() 135:     { 136:       Application.Run(new databound()); 137:     } 138:     } 139: } 

Compile this program with the command line:

 csc t:/winexe databound.cs 

The setup of the objects on the form will be familiar to you so we don't need to re-iterate those principles. The interesting parts are on lines 91 “95 which creates and populates a simple StringCollection . Remember that the .NET collections all implement the IList interface, making them candidates for databinding. Lines 98 and 99 perform the actual binding of the data to the textboxes. Finally, the click handlers on lines 114 “119 and 122 “127 move forward or backward through the data by moving the position, analogous to the cursor in a database, through the data.

I l @ ve RuBoard


C# and the .NET Framework. The C++ Perspective
C# and the .NET Framework
ISBN: 067232153X
EAN: 2147483647
Year: 2001
Pages: 204

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net