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