【问题标题】:Control changing control on other UserControl控制更改其他 UserControl 上的控制
【发布时间】:2016-01-04 23:31:16
【问题描述】:

我有一个包含组合框/下拉列表的用户控件。此用户控件被多次使用并动态添加到面板中。当我更改一个用户控件上的组合框的值时,它会更改其余部分吗?有谁知道如何排序?

所以澄清一下。当我更改顶部用户控件 (7002) 的组合框中的值时,它会将第二个用户控件组合框的值更改为我选择的任何值。

谢谢!

添加控件的代码;

foreach (Common.UserDTO UDTO in BLL.User.GetAllUsers())
{
    Admin_UserControls.UserBar UB = new Admin_UserControls.UserBar(UDTO);
    UB.Location = new Point(0, int.Equals(pnlUserBlock.Controls.Count, 0) ? 0 : pnlUserBlock.Controls[pnlUserBlock.Controls.Count - 1].Bottom);

    pnlUserBlock.Controls.Add(UB);
}

构造函数/加载事件:

private Common.UserDTO UDTO;

public UserBar(Common.UserDTO UDTO)
{ 
    InitializeComponent();

    /* Store the passed in UserDTO */
    this.UDTO = UDTO;
}

private void UserBar_Load(object sender, EventArgs e)
{
    /* Setup the Drop down list */
    cbRanks.DataSource = Common.Helper.GetRanksDT();
    cbRanks.DisplayMember = "Rank";
    cbRanks.ValueMember = "ID";

    /* Setup the users */
    lblUsername.Text = UDTO.Username;
    cbRanks.SelectedValue = UDTO.RankID;
}

【问题讨论】:

标签: c# winforms


【解决方案1】:

如果其他人将来有同样的问题,请将上述评论放入答案中。

创建的每个UserControl 实例都绑定到相同的DataSet,从而为您提供此结果。

由线路引起:

cbRanks.DataSource = Common.Helper.GetRanksDT();

要解决此问题,只需在每次创建 UserControl 时声明一个新实例,请参阅讨论一些方法的 this post

【讨论】:

    猜你喜欢
    • 2011-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多