gpt4 book ai didi

c# - 控制更改其他 UserControl 上的控件

转载 作者:行者123 更新时间:2023-11-30 23:31:24 25 4
gpt4 key购买 nike

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

enter image description here

所以澄清一下。当我更改顶部用户控件 (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;
}

最佳答案

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

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

由线路引起:

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

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

关于c# - 控制更改其他 UserControl 上的控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34594173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com