1
votes

I have a problem using C# forms. My main form is called Form1, from which i can open a child form called x as a dialog.

When I call a function in my child form x , i want all the other forms and main form to be affected by it.

public void Change_Layout_Red(System.Windows.Forms.Control Container)
{
    if (rb_EmberRed.Checked == true)
    {
        try
        {


            foreach (Control ctrl in Container.Controls)
            {
                if (ctrl.GetType() == typeof(TextBox))
                    ((TextBox)ctrl).BackColor = Color.Red;
                if (ctrl.GetType() == typeof(ComboBox))
                    ((ComboBox)ctrl).BackColor = Color.Red;
                if (ctrl.GetType() == typeof(DataGridView))
                    ((DataGridView)ctrl).BackColor = Color.Red;
                if (ctrl.GetType() == typeof(Label))
                    ((Label)ctrl).ForeColor = Color.White;
                if (ctrl.GetType() == typeof(TabPage))
                    ((TabPage)ctrl).BackColor = Color.Black;
                if (ctrl.GetType() == typeof(Panel))
                    ((Panel)ctrl).BackColor = Color.Red;
                if (ctrl.GetType() == typeof(RadioButton))
                    ((RadioButton)ctrl).ForeColor = Color.White;
                if (ctrl.Controls.Count > 0)
                    Change_Layout_Red(ctrl);

            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }


}

When I click a button in the child form , i want this function to run and apply to all the forms available, it will become like a default value.

private void btn_ChangeLayout_Click(object sender, EventArgs e)
{
    Change_Layout_Red(Form1.ActiveForm);
}

This code is only changing the colors in the active child form.

First is there a way to make this function change the default values for all new opened sub forms? Secondly, how can i access the control in the main form Form1? I tried to add Form1.((TextBoX)ctrl).Backcolor = Color.Red in the first function, but it is not working.

Thanks

1
Your Form1.ActiveForm is the child form, therefore it contains just the controls of the child form, to access the controls of your parent form you might want to use Form1.Parent instead of Form1.ActiveForm.Paul Weiland
@MeAndSomeRandoms you are right but this option is not availableNassif Bousaba
foreach (Form frm in Application.OpenForms) Change_Layout_Red(frm);Hans Passant
@HansPassant thank youNassif Bousaba

1 Answers

2
votes

You can create a base form and add in it a property like

public virtual Layout Layout { get; set; }   //Se below

Then, in the child form, you can access to its parent:

(Form1.Parent as MyCustomForm).Layout = new Layout(Color.Red);

In the Parent form you can iterate the child forms and change their layouts:

public virtual Layout Layout { 
  get {return _layout; }
  set {
    if (IsMdiContainer) {
      foreach (MyCustomForm item in MdiChildren.Cast<MyCustomForm>()) {
        item.Layout = value;
      }
    }
    foreach (Control ctrl in Container.Controls) {
      //Apply layout settings to controls
    }
  }

The layout class can be something like this:

class Layout {
  public Layout(Color color) {
    //Initialize the properties values based on the selected color
  }
  public Color TextBoxForeColor {get; set;}
  public Color TextBoxBackColor {get; set;}
  public Color LabelForeColor {get; set;}
  public Color LabelBackColor {get; set;}
}