CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
Form2.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="Form2.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>no summary</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Collections.Generic;
10using System.ComponentModel;
11using System.Drawing;
12using System.Text;
13using System.Windows.Forms;
14using NUnit.Framework;
15
16namespace DataBindingApp
17{
18 [TestFixture]
19 public partial class Form2 : Form
20 {
21 private ListBox listBox1;
22 private Button button1;
23 private TextBox textBox1;
24
25 public Form2()
26 {
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);
47 }
48
49 //private ListObject dataSource;
50 private BindingList<MyObject> dataSource;
51
52 public class MyObject
53 {
54 private string _name;
55 private int _age;
56
57 public string Name
58 {
59 get { return _name; }
60 set { _name = value; }
61 }
62
63 public int Age
64 {
65 get { return _age; }
66 set { _age = value; }
67 }
68
69 public MyObject(string name, int age)
70 {
71 _name = name;
72 _age = age;
73 }
74 }
75
76 void Form1_Load(object sender, EventArgs e)
77 {
78 dataSource = new BindingList<MyObject>();
79 for (int i = 0; i < 5; i++)
80 {
81 dataSource.Add(new MyObject("element" + i, i));
82 }
83
84 //dataSource = ListObject.GetList();
85 dataSource.RaiseListChangedEvents = true;
86 dataSource.AllowNew = true;
87 dataSource.AllowRemove = false;
88 dataSource.AllowEdit = false;
89
90 listBox1.DataSource = dataSource;
91 listBox1.DisplayMember = "Name";
92 dataSource.AddingNew += new AddingNewEventHandler(dataSource_AddingNew);
93 dataSource.ListChanged += new ListChangedEventHandler(dataSource_ListChanged);
94 }
95
96 //create a new dataobject
97 public void dataSource_AddingNew(object sender, AddingNewEventArgs e)
98 {
99 //e.NewObject = new ListObject.DataObject(textBox1.Text, 45);
100 e.NewObject = new MyObject(textBox1.Text, 45);
101 MessageBox.Show("AddingNew event raised");
102 }
103
104 //add the new object and test ICancelAddNew interface
105 private void button1_Click(object sender, EventArgs e)
106 {
107 //ListObject.DataObject newObject = dataSource.AddNew();
108
110 //if (newObject.Data.Contains(" "))
111 //{
112 // MessageBox.Show("CancelNew: data property cannot contain spaces");
113 // //discards pending object
114 // dataSource.CancelNew(dataSource.IndexOf(newObject));
115 // Console.WriteLine(dataSource.IndexOf(newObject));
116 //}
117 //else
118 //{
119 // //commits pending object
120 // dataSource.EndNew(dataSource.IndexOf(newObject));
121 //}
122
123 MyObject newObject = dataSource.AddNew();
124
125 if (newObject.Name.Contains(" "))
126 {
127 MessageBox.Show("CancelNew: name property cannot contain spaces");
128 MessageBox.Show("CancelNew: index is " + dataSource.IndexOf(newObject).ToString());
129 //discards pending object
130 dataSource.CancelNew(dataSource.IndexOf(newObject));
131 }
132 }
133
134
135 void dataSource_ListChanged(object sender, ListChangedEventArgs e)
136 {
137 MessageBox.Show("ListChanged event raised: " + e.ListChangedType.ToString());
138 }
139
140 [Test]
142 {
143 RunForm2();
144 }
145
146 [STAThread]
147 public static void RunForm2()
148 {
149 Application.EnableVisualStyles();
150 Form2 newForm2 = new Form2();
151 Application.Run(newForm2);
152 }
153
154 private void InitializeComponent()
155 {
156 this.SuspendLayout();
157 //
158 // Form1
159 //
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);
164 }
165 }
166}
MyObject(string name, int age)
Definition: Form2.cs:69
void RunFormWithRegularListObj()
Definition: Form2.cs:141
void dataSource_AddingNew(object sender, AddingNewEventArgs e)
Definition: Form2.cs:97
static void RunForm2()
Definition: Form2.cs:147