Data Entry With Binding : DataBinding TextBox « GUI Windows Forms « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » GUI Windows Forms » DataBinding TextBox 
23.82.2.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 = (ToolStripMenuItemmenu.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[0new 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;
}
23.82.DataBinding TextBox
23.82.1.Data Binding: StringCollection
23.82.2.Data Entry With Binding
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.