9using System.Collections.Generic;
10using System.ComponentModel;
13using System.Windows.Forms;
19 public partial class Form2 : Form
21 private ListBox listBox1;
22 private Button button1;
23 private TextBox textBox1;
27 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
28 this.textBox1 =
new System.Windows.Forms.TextBox();
29 this.listBox1 =
new System.Windows.Forms.ListBox();
30 this.button1 =
new System.Windows.Forms.Button();
31 this.textBox1.Location =
new System.Drawing.Point(169, 26);
32 this.textBox1.Size =
new System.Drawing.Size(100, 20);
33 this.textBox1.Text =
"new element";
34 this.listBox1.FormattingEnabled =
true;
35 this.listBox1.Location =
new System.Drawing.Point(12, 12);
36 this.listBox1.Size =
new System.Drawing.Size(120, 95);
37 this.button1.Location =
new System.Drawing.Point(180, 83);
38 this.button1.Size =
new System.Drawing.Size(95, 23);
39 this.button1.Text =
"Add New Object";
40 this.button1.Click +=
new System.EventHandler(this.button1_Click);
41 this.ClientSize =
new System.Drawing.Size(292, 266);
42 this.Controls.Add(this.button1);
43 this.Controls.Add(this.listBox1);
44 this.Controls.Add(this.textBox1);
45 this.Text =
"Databinding Tests";
46 this.Load +=
new EventHandler(Form1_Load);
50 private BindingList<MyObject> dataSource;
60 set { _name = value; }
76 void Form1_Load(
object sender, EventArgs e)
78 dataSource =
new BindingList<MyObject>();
79 for (
int i = 0; i < 5; i++)
81 dataSource.Add(
new MyObject(
"element" + i, i));
85 dataSource.RaiseListChangedEvents =
true;
86 dataSource.AllowNew =
true;
87 dataSource.AllowRemove =
false;
88 dataSource.AllowEdit =
false;
90 listBox1.DataSource = dataSource;
91 listBox1.DisplayMember =
"Name";
93 dataSource.ListChanged +=
new ListChangedEventHandler(dataSource_ListChanged);
100 e.NewObject =
new MyObject(textBox1.Text, 45);
101 MessageBox.Show(
"AddingNew event raised");
105 private void button1_Click(
object sender, EventArgs e)
123 MyObject newObject = dataSource.AddNew();
125 if (newObject.Name.Contains(
" "))
127 MessageBox.Show(
"CancelNew: name property cannot contain spaces");
128 MessageBox.Show(
"CancelNew: index is " + dataSource.IndexOf(newObject).ToString());
130 dataSource.CancelNew(dataSource.IndexOf(newObject));
135 void dataSource_ListChanged(
object sender, ListChangedEventArgs e)
137 MessageBox.Show(
"ListChanged event raised: " + e.ListChangedType.ToString());
149 Application.EnableVisualStyles();
151 Application.Run(newForm2);
154 private void InitializeComponent()
156 this.SuspendLayout();
160 this.ClientSize =
new System.Drawing.Size(292, 266);
161 this.Name =
"Databinding tests";
162 this.Load +=
new System.EventHandler(this.Form1_Load);
163 this.ResumeLayout(
false);