Reputation: 37
My apologies in advance, I might have difficulty in making myself clear. I have searched, but the discussions were far to complicated for me to follow, and I just started dwelling in C#.
Project: C# WindowsForm / .NET 4.5 / Visual Studio 2012
Challenge: I want to add items from a listbox to another listbox (I can do it easily with Lists and foreach loops), and make the end listbox show specific item depending on selections made in listbox2.
Explanation: The selected items are to incorporate a group that I create in yet another listbox, so that if I select a handful of items in listbox1, I send them to listbox3, but they only should appear when I select a specific item in listbox2.
Imagine selecting games from a list, adding it to the "Nintendo" group in a new list, so they don't get mixed with the Sega ones when Sega is selected in listbox2.
I can add values in all listboxes, copy the ones I want from 1 to 3, but I am at a loss on how to make the selection respect the selection on 2.
I've read about databinding and etc, but the examples gives were too complicated (and maybe a bit of language barrier was present), is there a resource that can provide the simplest solution to a really small project?
Care to enlighten a fool using layman terms, please?
Thanks
EDIT: Nice of you (whoever you were) to downvote my question. Fair enough. You could at least tell me what the problem was, or where the question was answered so I could resolve my problem. Wouldn't that be nice? I am a starter in C#, so it's only natural that the first question may seem ridiculous/lazy...
Upvotes: 0
Views: 2661
Reputation: 6026
DataBinding would be the way to go. The main component to focus on is BindingSource. You could also investigate DataSets for your underlying data because they would give you some flexibility with filtering. But, if this is a small application, and if you're just learning, the following example might be a good start:
Drag a BindingSource
for each of your listboxes onto your form. Then, connect each of the ListBox's DataSource
properties to a corresponding BindingSource
.
Here is an example code behind that shows how to bind your underlying data to each of the BindingSources
which are in turn already bound to the listboxes:
namespace WindowsFormsApplication1
{
public class Game
{
public string Name { get; set; }
public string Group { get; set; }
}
public class Group
{
public string Description { get; set; }
}
public partial class Form1 : Form
{
List<Game> _allGames;
public Form1()
{
InitializeComponent();
_allGames = new List<Game>
{
new Game { Name = "Alpha", Group = "" },
new Game { Name = "Bravo", Group = "One" },
new Game { Name = "Charlie" , Group = "One"},
new Game { Name = "Delta", Group = "Two" }
};
bindingSource1.DataSource = _allGames;
listBox1.DisplayMember = "Name";
listBox1.ValueMember = "Name";
var groups = new List<Group>
{
new Group { Description = "One" },
new Group { Description = "Two" },
new Group { Description = "Three" }
};
bindingSource2.DataSource = groups;
listBox2.DisplayMember = "Description";
listBox2.ValueMember = "Description";
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
var group = listBox2.SelectedValue.ToString();
bindingSource3.DataSource = _allGames.Where(x => x.Group == group);
listBox3.DisplayMember = "Name";
}
}
}
All this code is doing is binding data to the BindingSource and telling each ListBox which property of the underlying data to display. This example ignores the mechanism that assign each item from listBox1 to the group in listBox2 because I assume you know how to do that or can figure that out.
The event handler for when listBox2 changes just gets which selection was made and creates a list of items from listBox1 that match that item, and then displays those items in listBox3.
Upvotes: 1
Reputation: 5373
For the sake of simplicity, lets assume the items bound to your listboxes are strings. Then you can use a dictionary to hold your group assignment (you have to hold it somewhere):
Dictionary<string, List<string>> listOfGroups = new Dictionary<string, List<string>>();
Where key is a name of group, and list of strings holds items in group. Then, somewhere, you create a group to which you will be assigning values
void CreateGroup(string groupName)
{
listBox2.Items.Add(groupName);
if (!listOfGroups.ContainsKey(groupName)
listOfGroups.Add(groupName, new List<string>());
}
Then, you manage items in groups and add/remove them when, for example, selection on listBox2 changes:
string item = (string)listBox1.SelectedItem;
if(!listOfGroups.ContainsKey((string)listBox2.SelectedItem))
listOfGroups.Add((string)listBox2.SelectedItem,new List<string>());
((List<string>)listOfGroups[(string)listBox2.SelectedItem]).Add(item);
listBox3.Items.Add(item);
listBox1.Items.Remove(item);
And finaly:
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
listBox3.Items.Clear();
if (!listOfGroups.ContainsKey((string)listBox2.SelectedItem))
listOfGroups.Add((string)listBox2.SelectedItem, new List<string>());
listBox3.Items.AddRange(listOfGroups[(string)listBox2.SelectedItem].ToArray());
List<string> list = listOfAllItems.Where(a => !listBox3.Items.Contains(a)).ToList();
listBox1.Items.Clear();
listBox1.Items.AddRange(list.ToArray());
}
That's only an idea, though. If you're using DataSet, maybe you can just use linq (instead of a dictionary) to select items you want to display after selection in listbox2 changes.
Upvotes: 1
Reputation: 128
You can save some kind of Dictionary, key is element in first ListBox first, and value is the item of the second ListBox.
Upvotes: 0