// © Andrey Bushman, 2014
// Windows Forms: the binding sample.
// Written for the topic: http://www.theswamp.org/index.php?topic=46873.0
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinFormsBindingSample {
class Program {
static List<Person> data;
[STAThread]
static void Main(string[] args) {
data
= new List
<Person
>(); data
.Add(new Person
("Vasya",
"Vasilyev")); data
.Add(new Person
("Petya",
"Petrov")); data
.Add(new Person
("Kolya",
"Nikolaev")); data
.Add(new Person
("Ksenia",
"Sidorova"));
form.Text = "WinForms: the binding sample.";
form.Width = 500;
form.Height = 300;
form
.MinimumSize = new Size
(300,
200); form.StartPosition = FormStartPosition.CenterScreen;
FlowLayoutPanel panel
= new FlowLayoutPanel
(); panel.FlowDirection = FlowDirection.LeftToRight;
panel.WrapContents = true;
panel.Parent = form; // Also you can use it: form.Controls.Add(panel);
panel.Dock = DockStyle.Fill;
panel.BorderStyle = BorderStyle.FixedSingle;
Label label
= new Label
(); label.Name = "label";
label.Text = "User names:";
label.TextAlign = ContentAlignment.MiddleCenter;
label.AutoSize = true;
label.Parent = panel;
ComboBox cbx
= new ComboBox
(); cbx.Name = "cbx";
cbx.DataSource = data;
cbx.ValueMember = ".";
cbx.DisplayMember = "FullName";
cbx.Parent = panel;
cbx.SelectedIndexChanged += cbx_SelectedIndexChanged;
Button btn
= new Button
(); btn.Name = "btn";
btn.Text = "Change data";
btn.AutoSize = true;
btn.Parent = panel;
btn.Click += btn_Click;
Application.EnableVisualStyles();
Application.Run(form);
}
static void cbx_SelectedIndexChanged(object sender, EventArgs e) {
ComboBox cbx = sender as ComboBox;
if (cbx.SelectedIndex < 0)
return;
Person person = cbx.SelectedItem as Person;
MessageBox.Show(person.FullName, "Selection changed",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
static void btn_Click(object sender, EventArgs e) {
Button btn = sender as Button;
FlowLayoutPanel panel = btn.Parent as FlowLayoutPanel;
Form form = panel.Parent as Form;
// Examples: how to find a control by its name.
Boolean x1 = form.Controls.ContainsKey("cbx"); // false
Boolean x2 = panel.Controls.ContainsKey("cbx"); // true
Control[] result = form.Controls.Find("cbx", true); // items count: 1.
Control[] result2 = form.Controls.Find("cbx2", true); // items count: 0.
Person ivan
= new Person
("Ivan",
"Ivanov");
if (!data.Any(n => n.FullName == ivan.FullName)) {
ComboBox cbx = result[0] as ComboBox;
Person oldValue = cbx.SelectedValue as Person;
// Change data source:
data
.Add(new Person
("Ivan",
"Ivanov")); // add new item data.Remove(data[1]); // remove any item
Person newValue = cbx.SelectedValue as Person;
// Refresh items list
cbx.SelectedIndexChanged -= cbx_SelectedIndexChanged;
cbx.DataSource = null;
cbx.DataSource = data;
cbx.ValueMember = ".";
cbx.DisplayMember = "FullName";
if (newValue == null || newValue != oldValue)
cbx.SelectedIndex = 0;
cbx.SelectedIndexChanged += cbx_SelectedIndexChanged;
}
else {
MessageBox.Show(ivan.FullName, "Already exists",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
class Person {
public Person(String name, String surname) {
Name = name;
Surname = surname;
}
public String Name { get; set; }
public String Surname { get; set; }
public String FullName { get { return ToString(); } }
public override string ToString() {
return String.Format("{0} {1}", Name, Surname);
}
}
}