Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / February 2007

Tip: Looking for answers? Try searching our database.

How to do  Listboxes with master detail and default selection

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Uwe Lesta - 20 Feb 2007 12:51 GMT
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;
    }
}
----------------------

Kind regards

Uwe
Ciaran O''Donnell - 20 Feb 2007 15:58 GMT
What is the trouble that starts as I cant see off the top of y head what
would go wrong?

Signature

Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com

> Hello NG,
>
[quoted text clipped - 76 lines]
>
> Uwe
Uwe Lesta - 20 Feb 2007 18:10 GMT
> What is the trouble that starts as I cant see off the top of y head what
> would go wrong?

The trouble is:
- select a value in listbox2 and sore it in _data,
- select an other value in listbox1 and the wrong defailt value for Listbox2 is selected.

or from the internal view:
Every time listBox1_SelectedIndexChanged *also* listBox2_SelectedIndexChanged ( databinding )
with index = 0. so if i use this event to store a new selection i override my preselected value.

How to build this simple application ?
- Two listboxs with master detail databinding
- a default value ( _data.SelectedSubItem ) should be selected in listbox2 for each item in listbox1
- a new selection in listbox2 should be the new listbox2 default value.

listbox1    |    listbox2
------------------------------------------------------
first            first_1
            first_2        ( default )
            first_3           
second            second_1
            second_2
            second_3    ( default)   

change the default value for listbox2 by select an other value
change the value in listbox1 and back again and the just selected
value in listbox2 should be selected.

or is it still impossible with master-detail databinding ?

--

Kind regards

Uwe

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.