Csharp/CSharp Tutorial/GUI Windows Forms/DataBinding TextBox — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:15, 26 мая 2010
Data Binding: StringCollection
using System;
using System.Drawing;
using System.Collections;
using System.Collections.Specialized;
using System.ruponentModel;
using System.Windows.Forms;
public class DataBoundStringCollection : System.Windows.Forms.Form
{
private Button RightButton= new Button();
private Button LeftButton= new Button();
private TextBox textBox2 = new TextBox();
private TextBox textBox1 = new TextBox();
private Button DoneButton= new Button();
private StringCollection sa=new StringCollection();
public DataBoundStringCollection()
{
this.textBox2.Location = new Point(184, 16);
this.RightButton.Location = new Point(192, 64);
this.RightButton.Text = ">>";
this.RightButton.Click += new EventHandler(this.RightButton_Click);
this.textBox1.Location = new Point(8, 16);
this.DoneButton.Location = new Point(104, 64);
this.DoneButton.Text = "Done";
this.DoneButton.Click += new EventHandler(this.DoneButton_Click);
this.LeftButton.Location = new Point(16, 64);
this.LeftButton.Text = "<<";
this.LeftButton.Click += new EventHandler(this.LeftButton_Click);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(294, 111);
this.ControlBox = false;
this.Controls.AddRange(new Control[] {
this.RightButton,
this.LeftButton,
this.textBox2,
this.textBox1,
this.DoneButton});
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.ShowInTaskbar = false;
this.ResumeLayout(false);
sa.Add("A");
sa.Add("B");
sa.Add("C");
sa.Add("D");
this.textBox1.DataBindings.Add("Text",sa,"");
this.textBox2.DataBindings.Add("Text",sa,"");
}
public override void Dispose()
{
base.Dispose();
}
protected void RightButton_Click (object sender, System.EventArgs e)
{
this.BindingContext[sa].Position++;
}
protected void LeftButton_Click (object sender, System.EventArgs e)
{
this.BindingContext[sa].Position--;
}
protected void DoneButton_Click (object sender, System.EventArgs e)
{
Application.Exit();
}
public static void Main()
{
Application.Run(new DataBoundStringCollection());
}
}
Data Entry With Binding
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;
class MyClass : Form
{
XmlSerializer xmlser = new XmlSerializer(typeof(Employee));
BindingSource bindsrc = new BindingSource();
public static void Main()
{
Application.Run(new MyClass());
}
public MyClass()
{
bindsrc.Add(new Employee());
EmployeePanel personpnl = new EmployeePanel(bindsrc);
personpnl.Parent = this;
personpnl.Dock = DockStyle.Fill;
MenuStrip menu = new MenuStrip();
menu.Parent = this;
ToolStripMenuItem item = (ToolStripMenuItem) menu.Items.Add("&File");
item.DropDownItems.Add("&New", null, FileNewOnClick);
item.DropDownItems.Add("&Open...", null, FileOpenOnClick);
item.DropDownItems.Add("Save &As...", null, FileSaveAsOnClick);
}
void FileNewOnClick(object objSrc, EventArgs args)
{
bindsrc[0] = new Employee();
}
void FileOpenOnClick(object objSrc, EventArgs args)
{
OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
StreamReader sr = new StreamReader(dlg.FileName);
bindsrc[0] = xmlser.Deserialize(sr);
sr.Close();
}
}
void FileSaveAsOnClick(object objSrc, EventArgs args)
{
SaveFileDialog dlg = new SaveFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
StreamWriter sw = new StreamWriter(dlg.FileName);
xmlser.Serialize(sw, bindsrc[0]);
sw.Close();
}
}
}
class EmployeePanel : FlowLayoutPanel
{
public EmployeePanel(BindingSource bindsrc)
{
Label lbl = new Label();
lbl.Parent = this;
lbl.Text = "First Name: ";
lbl.AutoSize = true;
lbl.Anchor = AnchorStyles.Left;
TextBox txtboxFirstName = new TextBox();
txtboxFirstName.Parent = this;
txtboxFirstName.AutoSize = true;
txtboxFirstName.DataBindings.Add("Text", bindsrc, "FirstName");
txtboxFirstName.DataBindings[0].DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
this.SetFlowBreak(txtboxFirstName, true);
lbl = new Label();
lbl.Parent = this;
lbl.Text = "Last Name: ";
lbl.AutoSize = true;
lbl.Anchor = AnchorStyles.Left;
TextBox txtboxLastName = new TextBox();
txtboxLastName.Parent = this;
txtboxLastName.AutoSize = true;
txtboxLastName.DataBindings.Add("Text", bindsrc, "LastName");
txtboxLastName.DataBindings[0].DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
this.SetFlowBreak(txtboxLastName, true);
lbl = new Label();
lbl.Parent = this;
lbl.Text = "Birth Date: ";
lbl.AutoSize = true;
lbl.Anchor = AnchorStyles.Left;
DateTimePicker dtPicker = new DateTimePicker();
dtPicker.Parent = this;
dtPicker.AutoSize = true;
dtPicker.DataBindings.Add("Value", bindsrc, "BirthDate");
dtPicker.DataBindings[0].DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
}
}
class Employee
{
public string FirstName;
public string LastName;
public DateTime BirthDate;
}