while i want to fill its list from a binding source and choose its selected item from another one. how is it possible?
more description:
------------------
for this purpose i created a test project in which there are three lists, one including list of objects, one including list of shapes and one including list of colors. a color has some properties like red, green and blue components. a shape may be defined with its number of sides. this is a simple sample. for this, i didn't complicated it. now an object may have properties of shape and color as well as some other properties which is defined directly and not selected from a list like its transparency.
now my problem is that when i go to choose the shape or color of an object from a list the selected item of the list box including shapes or colors also changes. while i need to fill the list box of the combo box from the list box and choose the selected item of the combo box from the properties of the object, not to be the selected item of the list including shapes or colors. if i change the selected item of the combo box to change properties of an object, the selected item of the list box also changes. if i change the selected item of the list box, the selected item of the combo box also changes causing properties of the object to change. while they're logically separate things.
for reproducing the problem i've uploaded the whole project
here
output schema
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace BindingSourceTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
class Column<T> where T : Item, new()
{
readonly ListBox _listBox;
readonly Button _buttonNew, _buttonRemove;
public Column(ListBox listBox, Button buttonNew, Button buttonRemove)
{
_listBox = listBox;
_buttonNew = buttonNew;
_buttonRemove = buttonRemove;
_buttonNew.Click += OnButtonNewClick;
_buttonRemove.Click += OnButtonRemoveClick;
}
string CreateNameForNewItem()
{
var items = (IList)_listBox.DataSource;
var newItemName = "New " + typeof(T).Name;
if (items.Cast<Item>().Any(item => item.Name == newItemName))
{
var counter = 2;
string newItemName2;
do newItemName2 = newItemName + " (" + counter++ + ")"; while (items.Cast<Item>().Any(item => item.Name == newItemName2));
newItemName = newItemName2;
}
return newItemName;
}
void OnButtonNewClick(object sender, EventArgs e)
{
var items = (IList)_listBox.DataSource;
items.Add(new T { Name = CreateNameForNewItem() });
_listBox.SelectedIndex = items.Count - 1;
}
void OnButtonRemoveClick(object sender, EventArgs e)
{
var items = (IList)_listBox.DataSource;
items.Remove(_listBox.SelectedItem);
}
}
public class Item
{
public string Name { get; set; }
}
private void InitColumns()
{
new Column<Obj>(listBoxObjs, buttonObjNew, buttonObjRemove);
new Column<Shape>(listBoxShapes, buttonShapeNew, buttonShapeRemove);
new Column<Color>(listBoxColors, buttonColorNew, buttonColorRemove);
}
private void Form1_Load(object sender, EventArgs e)
{
InitColumns();
var black = new Color { Name = "Black", Red = 0, Green = 0, Blue = 0 };
var magenta = new Color { Name = "Magenta", Red = 255, Green = 0, Blue = 255 };
var yellow = new Color { Name = "Yellow", Red = 255, Green = 255, Blue = 0 };
var cyan = new Color { Name = "Cyan", Red = 0, Green = 255, Blue = 255 };
_colors.Add(black);
_colors.Add(magenta);
_colors.Add(yellow);
_colors.Add(cyan);
var triangle = new Shape { Name = "Triangle", NumSides = 3 };
var rect = new Shape { Name = "Rectangle", NumSides = 4 };
_shapes.Add(triangle);
_shapes.Add(rect);
var obj = new Obj {Name = "my mirror", Color = yellow, Transparent = false};
obj.SetShape(rect);
_objs.Add(obj);
obj = new Obj { Name = "my clock", Color = cyan, Transparent = false };
obj.SetShape(triangle);
_objs.Add(obj);
obj = new Obj { Name = "my glasses", Color = black, Transparent = true };
obj.SetShape(rect);
_objs.Add(obj);
bindingSourceObjs.DataSource = _objs;
bindingSourceShapes.DataSource = _shapes;
bindingSourceColors.DataSource = _colors;
comboBox1.DataBindings.Add(new Binding("SelectedValue", bindingSourceObjs, "Shape", true));
comboBox1.DataSource = bindingSourceShapes;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Shape";
comboBox1.SelectedValue = "valueToSelect";
}
public class Shape : Item
{
public int NumSides { get; set; }
}
public class Color : Item
{
public int Red { get; set; }
public int Green { get; set; }
public int Blue { get; set; }
}
public class Obj : Item
{
public Shape Shape { get; private set; }
public Color Color { get; set; }
public bool Transparent { get; set; }
public void SetShape(Shape shape) { Shape = shape; }
}
List <Shape> _shapes = new List<Shape>();
public List<Shape> Shapes { get { return _shapes; } set { _shapes = value; } }
List<Color> _colors = new List<Color>();
public List<Color> Colors { get { return _colors; } set { _colors = value; } }
List<Obj> _objs = new List<Obj>();
public List<Obj> Objs { get { return _objs; } set { _objs = value; } }
}
}