简体   繁体   English

在WinForm应用程序中的两个用户控件之间移动数据

[英]Moving data between two user controls in WinForm Application

As a course project i'm building a form in c# which contains two user controls. 作为一个课程项目,我正在用C#构建一个包含两个用户控件的表单。 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. 谢谢。

To do this "correctly," you would want to use something like the MVC architecture. 要“正确”地执行此操作,您可能希望使用MVC架构之类的工具。 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. 最初,要理解和实现肯定要做很多工作,但是,如果您打算进行任何认真的UI应用程序开发,这将非常有用。 Even if you don't go all the way with it, the concepts are useful to help design even "quick and dirty" applications. 即使您不完全使用它,这些概念对于帮助设计“快速而肮脏的”应用程序也很有用。

Define your data model without thinking in terms of the UI, eg: 定义数据模型时无需考虑UI,例如:

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" });

// . . .

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. 对于视图部分,通常将在UI控件上使用数据绑定,以便控件将自动反映对基础数据的更改。 However, this can get difficult especially if you are not using a database-like model (eg System.Data.DataSet ). 但是,这可能会变得很困难,特别是如果您没有使用类似数据库的模型(例如System.Data.DataSet )。 You may opt to "manually" update the data in the controls which might be fine in a small app. 您可以选择“手动”更新控件中的数据,这在小型应用程序中可能很好。

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. 控制器是使用UI事件并对模型进行更改的部分,然后可以将其反映为视图中的更改。

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. 基本上,我建议不要让控件直接对话,而应该通过上述类似控制器的类,该类具有数据模型和视图中其他控件的知识。

Of course it's possible: you need to make the link between the 2 controls in the form. 当然有可能:您需要在表单的2个控件之间建立链接。 Just declare an event 'ButtonClicked' in control #1 Then make a public method 'PerformsClick' on the control #2 只需在控件1中声明一个事件“ ButtonClicked”,然后在控件2中创建一个公共方法“ PerformsClick”

And in the form, in the constructor, after the call to InitializeComponent, link the event from the control #1 to the method to the control #2: 并以构造函数的形式在调用InitializeComponent之后,将事件从控件1链接到方法到控件2:

control1.ButtonClicked += delegate(sender, e) {
  control2.PerformsClick();
};

(I type on the fly to give you an idea, it'll surely not compile) (我会即时输入以提示您,它肯定不会编译)

If you want to pass any data, just add parameters in the PerformsClick method. 如果要传递任何数据,只需在PerformsClick方法中添加参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM