在 WinForm 应用程序中的两个用户控件之间移动数据 [英] Moving data between two user controls in WinForm Application

查看:28
本文介绍了在 WinForm 应用程序中的两个用户控件之间移动数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个课程项目,我正在用 C# 构建一个包含两个用户控件的表单.第一个用户控件有一个checkedlistbox,第二个控件也有一个checkedlistbox,当第一个控件checkedlistbox 将包含人员列表(男性/女性),第二个用户控件checkedlistbox 将有两个选项:男性、女性和当我点击第一个控件上的按钮说:更新朋友"它假设转到第二个控件并检查我们是否选择了男性或女性,并根据选择的性别类型与朋友更新第一个用户控件中的选中列表框在第二个控件上.

As a course project i'm building a form in c# which contains two user controls. The first user control has a checkedlistbox and the second control has also a checkedlistbox when the first control checkedlistbox will contain list of people (male/female) and the second user control the checkedlistbox will have two options: male, female and when I click a button on the first control which says: "update friends" it's suppose to go to the second control and check if we selected male or female and according to that to update the checkedlistbox in the first user control with friends by gender type by what was selected on the second control.

基本上我想在每次选择第一个控件上的按钮时引发一个事件,然后将数据从第二个控件获取到第一个控件.是否可以在表单内且是不同控件的两个控件之间执行此操作?

Basically I want to raise an event every time the button on the first control selected then to get the data from the second control to the first control. Is it possible to do so between two controls who are inside a form and are different controls?

任何帮助都会得到帮助.谢谢.

Any help will be appriciated. Thanks.

推荐答案

要正确"执行此操作,您需要使用类似 MVC 架构.一开始要理解和实施肯定需要做更多的工作,但是如果您计划进行任何认真的 UI 应用程序开发,这非常有用.即使您没有坚持下去,这些概念对于帮助设计甚至快速而肮脏"的应用程序也很有用.

To do this "correctly," you would want to use something like the MVC architecture. It's definitely a lot more work initially to understand and implement but is very useful to know if you plan on doing any serious UI application development. Even if you don't go all the way with it, the concepts are useful to help design even "quick and dirty" applications.

在不考虑 UI 的情况下定义您的数据模型,例如:

Define your data model without thinking in terms of the UI, e.g.:

internal enum Gender
{
    Male,
    Female
}

internal class Person
{
    public Gender Gender { get; set; }

    public string Name { get; set; }
}

// . . .

// Populate the list of people
List<Person> allPeople = new List<Person>();
allPeople.Add(new Person() { Gender = Gender.Male, Name = "Xxx Yyy" });
allPeople.Add(new Person() { Gender = Gender.Female, Name = "Www Zzz" });

// . . .

对于视图部分,您通常会在 UI 控件上使用数据绑定,以便控件自动反映对基础数据的更改.但是,这会变得很困难,尤其是如果您不使用类似数据库的模型(例如 System.Data.DataSet).您可以选择手动"更新控件中的数据,这在小型应用中可能没问题.

For the view portion, you would typically use data binding on the UI controls so that the controls will automically reflect changes to the underlying data. However, this can get difficult especially if you are not using a database-like model (e.g. System.Data.DataSet). You may opt to "manually" update the data in the controls which might be fine in a small app.

控制器是使用 UI 事件并对模型进行更改的部分,然后这些更改可能会反映为视图中的更改.

The controller is the portion that uses the UI events and makes changes to the model, which may then be reflected as changes in the view.

internal class Controller
{
    private Gender selectedGender;
    private List<Person> allPeople;
    private List<Person> friends;

    public Controller(IEnumerable<Person> allPeople)
    {
        this.allPeople = new List<Person>(allPeople);
        this.friends = new List<Person>();
    }

    public void BindData(/* control here */)
    {
        // Code would go here to set up the data binding between
        // the friends list and the list box control
    }

    // Event subscriber for CheckedListBox.SelectedIndexChanged
    public void OnGenderSelected(object sender, EventArgs e)
    {
        CheckedListBox listBox = (CheckedListBox)sender;
        this.selectedGender = /* get selected gender from list box here */;
    }

    // Event subscriber for Button.Click
    public void OnUpdateFriends(object sender, EventArgs e)
    {
        this.friends.AddRange(
            from p in this.allPeople
            where p.Gender == this.selectedGender
            select p);
        // If you use data binding, you would need to ensure a
        // data update event is raised to inform the control
        // that it needs to update its view.
    }
}

// . . .

// On initialization, you'll need to set up the event handlers, etc.
updateFriendsButton.Click += controller.OnUpdateFriends;
genderCheckedListBox.SelectedIndexChanged += controller.OnGenderSelected;
controller.BindData(friendsListBox);

// . . .

基本上,我建议不要让控件直接对话,而是通过上述类似控制器的类,该类了解数据模型和视图中的其他控件.

Basically, I recommend not having controls talk directly, but rather through a controller-like class as above which has knowledge of the data model and the other controls in the view.

这篇关于在 WinForm 应用程序中的两个用户控件之间移动数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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