Csharp/CSharp Tutorial/GUI Windows Forms/DataBinding Label

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Bind Client Size to Label

<source lang="csharp">using System; using System.Drawing; using System.Windows.Forms; class WhatClientSize: Form {

   [STAThread]
   public static void Main()
   {
       Application.EnableVisualStyles();
       Application.Run(new WhatClientSize());
   }
   public WhatClientSize()
   {
       Text = "What Client Size?";
       Label lbl = new Label();
       lbl.Parent = this;
       lbl.AutoSize = true;
       lbl.DataBindings.Add("Text", this, "ClientSize");
   }

}</source>

Data binding to a programatically created dataset

<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.Data; using System.ruponentModel; using System.Windows.Forms; public class DataBindingDataSet : System.Windows.Forms.Form {

   private Label lbl_first= new Label();
   private Label lbl_name= new Label();
   private Label lbl_title= new Label();
   private Label lbl_company= new Label();
   private Label lbl_phone = new Label();
   private TextBox FirstName = new TextBox();
   private TextBox SurName = new TextBox();
   private TextBox Title = new TextBox();
   private TextBox Company = new TextBox();
   private TextBox Phone = new TextBox();
   private Button btnNext = new Button();
   private Button btnPrev = new Button();
   private Button btnNew = new Button();
   private Button btnEnd = new Button();
   private DataSet dataset = new DataSet("ContactData");
   private void OnPrev(Object sender,EventArgs e)
   {
   this.BindingContext[dataset.Tables["Contacts"]].Position--;
   }
   private void OnNext(Object sender,EventArgs e)
   {
   this.BindingContext[dataset.Tables["Contacts"]].Position++;
   }
   private void OnNew(Object sender, EventArgs e)
   {
       NewEntry();
   }
   private void OnEnd(Object sender, EventArgs e)
   {
       Application.Exit();
   }
   private void MoveToEnd()
   {
   this.BindingContext[dataset.Tables["Contacts"]].Position=dataset.Tables["Contacts"].Rows.Count-1;
   }
   private void NewEntry()
   {
       DataRow row = dataset.Tables["Contacts"].NewRow();
       row["First"]="Blank";
       row["Name"]="default name";
       row["Company"]="default company";
       row["Title"]="no title";
       row["Phone"]="999-999-9999";
       dataset.Tables[0].Rows.Add(row);
       dataset.AcceptChanges();
       MoveToEnd();
   }
   public DataBindingDataSet()
   {
   this.AutoScaleBaseSize = new System.Drawing.Size (5, 13);
   this.ClientSize = new System.Drawing.Size (250, 200);
   this.FormBorderStyle = FormBorderStyle.Fixed3D;
       lbl_first.Text="First name";
       lbl_first.Location = new Point(5,5);
       lbl_first.Size = new Size(120,28);
       lbl_first.Anchor = AnchorStyles.Left | AnchorStyles.Right;
       Controls.Add(lbl_first);
       FirstName.Location = new Point(125,5);
       FirstName.Size = new Size(120,28);
       FirstName.Anchor = AnchorStyles.Left | AnchorStyles.Right;
       Controls.Add(FirstName);
       lbl_name.Text="Surname";
       lbl_name.Location = new Point(5,35);
       lbl_name.Size = new Size(120,28);
       lbl_name.Anchor = AnchorStyles.Left|AnchorStyles.Right;
       Controls.Add(lbl_name);
       SurName.Location = new Point(125,35);
       SurName.Size = new Size(120,28);
       SurName.Anchor = AnchorStyles.Left | AnchorStyles.Right;
       Controls.Add(SurName);
       lbl_company.Text="Company";
       lbl_company.Location = new Point(5,65);
       lbl_company.Size = new Size(120,28);
       Controls.Add(lbl_company);
       Company.Location = new Point(125,65);
       Company.Size = new Size(120,28);
       Controls.Add(Company);
       lbl_title.Text="Title";
       lbl_title.Location = new Point(5,95);
       lbl_title.Size = new Size(120,28);
       Controls.Add(lbl_title);
       Title.Location = new Point(125,95);
       Title.Size = new Size(120,28);
       Controls.Add(Title);
       lbl_phone.Text="Telephone";
       lbl_phone.Location = new Point(5,125);
       lbl_phone.Size = new Size(120,28);
       Controls.Add(lbl_phone);
       Phone.Location = new Point(125,125);
       Phone.Size = new Size(120,28);
       Controls.Add(Phone);
       btnNew.Location = new Point(5,155);
       btnNew.Size = new Size(70,28);
       btnNew.Text="New";
       btnNew.Click+=new EventHandler(OnNew);
       Controls.Add(btnNew);
       btnPrev.Location = new Point(80,155);
       btnPrev.Size = new Size(35,28);
       btnPrev.Text="<<";
       btnPrev.Click += new EventHandler(OnPrev);
       Controls.Add(btnPrev);
       btnEnd.Location = new Point(120,155);
       btnEnd.Size = new Size(70,28);
       btnEnd.Text="End";
       btnEnd.Click += new EventHandler(OnEnd);
       Controls.Add(btnEnd);
       btnNext.Location = new Point(200,155);
       btnNext.Size = new Size(35,28);
       btnNext.Text=">>";
       btnNext.Click += new EventHandler(OnNext);
       Controls.Add(btnNext);
       DataTable t=new DataTable("Contacts");
       t.Columns.Add("First",typeof(System.String));
       t.Columns.Add("Name",typeof(System.String));
       t.Columns.Add("Company",typeof(System.String));
       t.Columns.Add("Title",typeof(System.String));
       t.Columns.Add("Phone",typeof(System.String));
       t.MinimumCapacity=100;
       dataset.Tables.Add(t);
       NewEntry();
       FirstName.DataBindings.Add("Text",dataset.Tables["Contacts"],"First");
       SurName.DataBindings.Add("Text",dataset.Tables["Contacts"],"Name");
       Title.DataBindings.Add("Text",dataset.Tables["Contacts"],"Title");
       Company.DataBindings.Add("Text",dataset.Tables["Contacts"],"Company");
       Phone.DataBindings.Add("Text",dataset.Tables["Contacts"],"Phone");
   }
   static void Main()
   {
       Application.Run(new DataBindingDataSet());
   }

}</source>