需要一种更好的方法来迭代表单控件

PJW*_*PJW 2 c# winforms

在我用C#编写的WinForms应用程序中,一个Form上有一个Button,需要稍微改变第二个Form的外观(只需更改Button上的Text).

我已经设法做到这一点,但代码非常长,我相信必须有一个更简洁的方法来实现同样的事情.

这是我的窗体上的按钮frmConflicts的代码,以及它如何更改窗体frmAdmin上的按钮btnAddCase上的文本(工作,但似乎太长) -

private void btnNoConflicts_Click(object sender, EventArgs e)
    {
        try
        {
            foreach (Form f in Application.OpenForms)
            {
                if (f.Name == "frmAdmin")
                {
                    frmAdmin a = (frmAdmin)f;
                    a.conflictsClear = true;
                    foreach (Control ctrl in a.Controls)
                    {
                        if (ctrl.Name == "panAdmin")
                        {
                            foreach (Control ctrl2 in ctrl.Controls)
                            {
                                if (ctrl2.Name == "tabControlAdmin")
                                {
                                    TabControl tab = (TabControl)ctrl2;                                        
                                    foreach(TabPage page in tab.TabPages)
                                    {
                                        if (page.Name == "pageNewCase")
                                        {
                                            foreach (Control ctrl3 in page.Controls)
                                            {
                                                if (ctrl3.Name == "panCaseDetails")
                                                {
                                                    foreach (Control ctrl4 in ctrl3.Controls)
                                                    {
                                                        if (ctrl4.Name == "btnAddCase")
                                                        {
                                                            ctrl4.Text = "Add Case";                                                                
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            this.Close();
        }
        catch (Exception eX)
        {
            MessageBox.Show("frmConflicts: btnNoConflicts()" + Environment.NewLine + eX.Message);
        }
Run Code Online (Sandbox Code Playgroud)

任何有助于显着减少代码量的帮助都会非常受欢迎,因为我需要在我的应用程序的其他地方进行类似的交互.

gza*_*axx 5

如果您的按钮是通过设计师加入,而不是动态创建的解决方案很简单:里面添加你的方法frmAdmin一样

public void ChangeCaption(string caption)
{
     btnAddCase.Text = "Add case";
}
Run Code Online (Sandbox Code Playgroud)

然后

var frmAdmin = Application.OpenForms.OfType<Form>().FirstOrDefault(x => x.GetType() == typeof(frmAdmin));

if (frmAdmin != null)
{
    frmAdmin.ChangeCaption("Add case");
}
Run Code Online (Sandbox Code Playgroud)