Mega Code Archive

 
Categories / C# Tutorial / GUI Windows Forms
 

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; }