Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, December 11, 2018 4:58 PM
Hi,
So I made a settings user control and I have color buttons there to change every forms and user control's buttons and panels colors, but how do I change a user controls button color from another user control? Thanks!
All replies (5)
Tuesday, December 11, 2018 10:33 PM âś…Answered
Okay, here is a thought.
For trying this out.
- Add Extensions.cs (below) to your project which is used in step 2.
- Create a new user control from the one below with one button.
- Build the project.
- Add the user control to the main form.
- Run the project.
Before pressing the button on the user control, yellow is a panel, rose is the user control. There are four buttons but we will target only the ones not in the user control.
Pressing the button
User control, one button, I change the back color. Change the namespace to your namespace. Note Parent.Descendants here is getting only buttons, do the same for other control types. SetForeColor and SetBackColor work on any control with ForeColor and BackColor.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class SettingsControl : UserControl
{
public SettingsControl()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var buttons = Parent.Descendants<Button>()
.Where(item => item.Parent != this).ToArray();
buttons.SetBackColor(Color.Brown);
buttons.SetForeColor(Color.White);
}
}
}
Make sure to change the namespace.
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public static class Extensions
{
public static IEnumerable<T> Descendants<T>(this Control control) where T : class
{
foreach (Control child in control.Controls)
{
T thisControl = child as T;
if (thisControl != null)
{
yield return (T)thisControl;
}
if (child.HasChildren)
{
foreach (T descendant in Descendants<T>(child))
{
yield return descendant;
}
}
}
}
public static void SetForeColor(this IEnumerable<Control> controls, Color color)
{
foreach (var control in controls)
{
control.ForeColor = color;
}
}
public static void SetBackColor(this IEnumerable<Control> controls, Color color)
{
foreach (var control in controls)
{
control.BackColor = color;
}
}
}
}
In closing, the above relies on the parent control, in this case the form.
Second idea, tweaking the UserControl code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class SettingsControl : UserControl
{
public Control FormControl { get; set; }
public SettingsControl()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var buttons = FormControl.Descendants<Button>().ToArray();
buttons.SetBackColor( Color.Brown);
buttons.SetForeColor(Color.White);
}
}
}
Create a new form, Form2, add several buttons and the main form code is below
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var f = new Form2();
settingsControl1.FormControl = f;
f.Show();
}
}
}
Before and after
So we've done just buttons, for other controls follow the same pattern.
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
Tuesday, December 11, 2018 5:28 PM
Hello,
Each control in a user control is private. Select a control (like a button), select properties, select Modifier, select public. Now you can access the control/in this case a button.
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
Tuesday, December 11, 2018 6:23 PM
Thank you for such a quick response. ^-^
Unfortunately, the button still doesn't show up in the settings user control code...
Tuesday, December 11, 2018 7:22 PM
Thank you for such a quick response. ^-^
Unfortunately, the button still doesn't show up in the settings user control code...
Not clear on what you are missing. If you place a button on a user control and set modifier to public the button is accessible outside of the user control. So " the button still doesn't show up in the settings user control code..." is unclear to me.
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
Tuesday, December 11, 2018 8:14 PM
Alright. So basically I have the main form where all my user controls are placed. Then, I have a settings user control where you can change the color of the whole UI etc. The problem is that in the settings user control I can only change the properties of the items inside that user control, not outside of it. So I went to my main form, changed the panel modifier to public and went back to settingsControl.cs which is where the settings user control code is. In my settings user control I have 9 colors. If you click one, it should change the buttons colors in the main form, and in the other user controls. Heres an example.
headerPanelStrip.Forecolor = Color.Red; //mainHeaderStrip is the colored panel in my main form. I set it to public in the properties, but the code right here still doesn't work and neither does the panel full name show up in the auto-complete that appears on top of your code when you write something.
I hope you understand now :)