开发者

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. 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开发者_JAVA百科 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. 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.

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

// . . .

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.

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.


Of course it's possible: you need to make the link between the 2 controls in the form. Just declare an event 'ButtonClicked' in control #1 Then make a public method 'PerformsClick' on the control #2

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:

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜