U
Uwe Lesta
Hello NG,
I am despairing, please help me.
With VS2005 i drag 2 Listboxes on my form and a bindingsource.
Listbox1 is bind to a bindingSource1.DataSource = data
Listbox2 is bind to a Listbox2.DataSource = subItemsBindingSource
all work as expected. When i select a item in listBox1, listbox2 display the details.
In the next step i implement
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedIndex >= 0)
listBox2.SelectedValue = _data[listBox1.SelectedIndex].SelectedSubItem.Name;
}
to select a default value for listbox 2. Fine.
Trouble starts when i try to record the changes of listbox2 with
private void listBox2_SelectedValueChanged(object sender, EventArgs e)
{
if (listBox2.SelectedIndex > 0)
_data[listBox1.SelectedIndex].SubItemName = (string)listBox2.SelectedValue;
}
What is the right / advised way to implement such a behaviour ?
Please Help.
----------------------
public class data
{
private string _name;
private string _subItemName;
private subData _selectedSubItem;
private List<subData> _subItems;
public string Name { get { return _name; } set { _name = value; } }
public string SubItemName { get { return _subItemName; } set { _subItemName = value; } }
public List<subData> SubItems { get { return _subItems; } set { _subItems = value; } }
public subData SelectedSubItem { get { return _selectedSubItem; } set { _selectedSubItem = value; } }
}
public class subData
{
private string _name;
public string Name { get { return _name; } set { _name = value; } }
public subData(string n) { _name = n; }
}
public partial class Form1 : Form
{
public data[] _data;
public Form1()
{
InitializeComponent();
_data = new data[] {new data(), new data(), new data() };
_data[0].Name = "first";
_data[0].SubItemName = "first_2";
_data[0].SubItems = new List<subData>(new subData[] { new subData("first_1"), new subData("first_2"), new
subData("first_3") });
_data[0].SelectedSubItem = _data[0].SubItems[1];
_data[1].Name = "second";
_data[1].SubItemName = "second_3";
_data[1].SubItems = new List<subData>(new subData[] { new subData("second_1"), new subData("second_2"), new
subData("second_3") });
_data[1].SelectedSubItem = _data[1].SubItems[2];
_data[2].Name = "thhird";
_data[2].SubItemName = "thhird_2";
_data[2].SubItems = new List<subData>(new subData[] { new subData("thhird_1"), new subData("thhird_2"), new
subData("thhird_3") });
_data[2].SelectedSubItem = _data[2].SubItems[1];
bindingSource1.DataSource = _data;
}
}
I am despairing, please help me.
With VS2005 i drag 2 Listboxes on my form and a bindingsource.
Listbox1 is bind to a bindingSource1.DataSource = data
Listbox2 is bind to a Listbox2.DataSource = subItemsBindingSource
all work as expected. When i select a item in listBox1, listbox2 display the details.
In the next step i implement
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedIndex >= 0)
listBox2.SelectedValue = _data[listBox1.SelectedIndex].SelectedSubItem.Name;
}
to select a default value for listbox 2. Fine.
Trouble starts when i try to record the changes of listbox2 with
private void listBox2_SelectedValueChanged(object sender, EventArgs e)
{
if (listBox2.SelectedIndex > 0)
_data[listBox1.SelectedIndex].SubItemName = (string)listBox2.SelectedValue;
}
What is the right / advised way to implement such a behaviour ?
Please Help.
----------------------
public class data
{
private string _name;
private string _subItemName;
private subData _selectedSubItem;
private List<subData> _subItems;
public string Name { get { return _name; } set { _name = value; } }
public string SubItemName { get { return _subItemName; } set { _subItemName = value; } }
public List<subData> SubItems { get { return _subItems; } set { _subItems = value; } }
public subData SelectedSubItem { get { return _selectedSubItem; } set { _selectedSubItem = value; } }
}
public class subData
{
private string _name;
public string Name { get { return _name; } set { _name = value; } }
public subData(string n) { _name = n; }
}
public partial class Form1 : Form
{
public data[] _data;
public Form1()
{
InitializeComponent();
_data = new data[] {new data(), new data(), new data() };
_data[0].Name = "first";
_data[0].SubItemName = "first_2";
_data[0].SubItems = new List<subData>(new subData[] { new subData("first_1"), new subData("first_2"), new
subData("first_3") });
_data[0].SelectedSubItem = _data[0].SubItems[1];
_data[1].Name = "second";
_data[1].SubItemName = "second_3";
_data[1].SubItems = new List<subData>(new subData[] { new subData("second_1"), new subData("second_2"), new
subData("second_3") });
_data[1].SelectedSubItem = _data[1].SubItems[2];
_data[2].Name = "thhird";
_data[2].SubItemName = "thhird_2";
_data[2].SubItems = new List<subData>(new subData[] { new subData("thhird_1"), new subData("thhird_2"), new
subData("thhird_3") });
_data[2].SelectedSubItem = _data[2].SubItems[1];
bindingSource1.DataSource = _data;
}
}