【问题标题】:Access several user controls from Child Form?从子窗体访问多个用户控件?
【发布时间】:2016-05-16 21:30:03
【问题描述】:

在我的 Windows 窗体中,我用用户控件 UCDetail 的实例填充了 flowLayoutPanel。像这样的:

public ParentForm()
{
    InitializeComponent();
    dataGridView1.DataSourceChanged += dataGridView1_DataSourceChanged;
    DT = GetAllRooms(); // DT is one column [RoomNum] w/ 5 rows (201-205)
    dataGridView1.DataSource = DT;

}

private void dataGridView1_DataSourceChanged(object sender, EventArgs e)
{
    DataTable dt = (DataTable)dataGridView1.DataSource;
    flowLayoutPanel1.Controls.Clear();
    flowLayoutPanel1.SuspendLayout();
    foreach (DataRow row in dt.Rows)
    {
        UCDetail ucb = new UCDetail(row, this.dataGridView1);
        flowLayoutPanel1.Controls.Add(ucb);
    }
    flowLayoutPanel1.ResumeLayout();
}

UCDetail有一个Click事件,这样当UC被点击时,会显示ChildForm。我传递了单击的 UCDetail 的引用,以便我可以控制 UC 中控件的属性。像这样的:

private void UCDetail_Click(object sender, EventArgs e)
{
    var form = new ChildForm(this);
    form.ShowDialog();
}

最后,ChildForm 有一个带有点击事件的按钮,该事件会在点击的UCDetail 中更改LabelName.Text。像这样的:

UCDetail _cell;
public ChildForm(UCDetail Cell)
{
    InitializeComponent();
    _cell = Cell;
}

private void buttonChangeLabel_Click(object sender, EventArgs e)
{
    _cell.labelEmp.Text = "John Smith";
}

假设用户点击 RoomNum 202,该行的 UCDetail 实例被发送到ChildForm,当我点击ChildForm 中的按钮时,ParentForm 将为 RoomNum 202 显示 John Smith。没有问题。

问题是ChildForm 现在将包含几个文本框,这样我就可以输入几个 labelEmp 会改变的 RoomNumber。

使用前面的例子,我点击了202,它显示ChidForm。在新的文本框中,我将输入 203、205,因为我还想将这些房间更改为“John Smith”。

ChildForm 中,我只能访问 202(它被点击),但不能访问 203、205 或任何其他未被点击的 RoomNum。如何更改这些控件的属性?

谢谢。

【问题讨论】:

  • 我强烈建议您阅读以下内容:codeproject.com/Tips/889332/…
  • 感谢您的链接。我会在找到我遇到的问题的解决方案后阅读它。

标签: c# winforms visual-studio-2010 .net-4.0 windows-forms-designer


【解决方案1】:

我最终传递了flowLayoutPanel1 作为参考。效果很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-25
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-23
    • 1970-01-01
    相关资源
    最近更新 更多