将项目添加到ListBox并将其绑定到另一个Listbox中的项目 [英] Add items to a ListBox and bind them to an item in another Listbox

查看:69
本文介绍了将项目添加到ListBox并将其绑定到另一个Listbox中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

事先致歉,我可能难以阐明自己的意思.我已经进行了搜索,但是对于我来说,讨论的内容远非如此复杂,我只是开始使用C#.

项目:C#WindowsForm/.NET 4.5/Visual Studio 2012

挑战:我想将一个列表框中的项目添加到另一个列表框中(我可以使用Lists和foreach循环轻松地完成此操作),并根据listbox2中所做的选择使结束列表框显示特定的项目.

说明:选定的项目将合并在另一个列表框中创建的组,以便在列表框1中选择少数项目时,会将它们发送到列表框3,但是只有在列表框2中选择特定项目时,它们才会出现.>

想象一下从一个列表中选择游戏,并将其添加到新列表中的"Nintendo"组中,这样当在listbox2中选择Sega时,它们就不会与Sega混合在一起.

我可以在所有列表框中添加值,将所需的值从1复制到3,但是我对如何使选择尊重2上的选择感到困惑.

我已经阅读了有关数据绑定等内容,但是给出的示例太复杂了(也许存在一些语言障碍),是否有资源可以为一个很小的项目提供最简单的解决方案?

请问想用外行的术语开导一个傻瓜吗?

谢谢

你们真好(无论您是谁)都对我的问题投了反对票.很公平.您至少可以告诉我问题出在哪里,或在哪里回答了问题,以便我可以解决我的问题.那不是很好吗?我是C#的入门者,所以第一个问题似乎很荒谬/懒惰是很自然的.

解决方案

数据绑定将是解决之道.要关注的主要组件是 BindingSource .您还可以调查数据集,因为它们是基础数据可以通过过滤给您一些灵活性.但是,如果这是一个小型应用程序,并且您只是在学习,那么以下示例可能是一个不错的开始:

将每个列表框的 BindingSource 拖到表单上.然后,将ListBox的每个 DataSource 属性连接到相应的 BindingSource .

这是后面的示例代码,显示了如何将基础数据绑定到每个 BindingSources 上,而每个 BindingSources 已经绑定到了列表框:

 命名空间WindowsFormsApplication1{公共类游戏{公共字符串名称{get;放;}公共字符串组{get;放;}}公共班组{公共字符串说明{放;}}公共局部类Form1:表单{列表<游戏>_allGames;公共Form1(){InitializeComponent();_allGames =新列表< Game>{新游戏{Name ="Alpha",Group ="},新游戏{Name ="Bravo",Group ="One"},新游戏{Name ="Charlie",Group ="One"},新游戏{名称=三角洲",组=两个"}};bindingSource1.DataSource = _allGames;listBox1.DisplayMember =名称";listBox1.ValueMember =名称";var groups = new List< Group>{新组{Description ="One"},新组{Description ="Two"},新组{描述=三个"}};bindingSource2.DataSource =组;listBox2.DisplayMember =描述";listBox2.ValueMember =描述";}私有无效listBox2_SelectedIndexChanged(对象发送者,EventArgs e){var group = listBox2.SelectedValue.ToString();bindingSource3.DataSource = _allGames.Where(x => x.Group ==组);listBox3.DisplayMember =名称";}}} 

此代码所做的全部工作就是将数据绑定到BindingSource,并告诉每个ListBox要显示基础数据的哪个属性.此示例忽略了将listBox1中的每个项目分配给listBox2中的组的机制,因为我假设您知道如何做到这一点,或者可以弄清楚这一点.

listBox2发生更改时的事件处理程序仅获取做出的选择,并从listBox1创建与该项目匹配的项目列表,然后在listBox3中显示这些项目.

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...

解决方案

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.

这篇关于将项目添加到ListBox并将其绑定到另一个Listbox中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆