Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can create a method that takes a <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx" rel="nofollow">Control</a> and a <a href="http://msdn.microsoft.com/en-us/library/system.drawing.color.aspx" rel="nofollow">Color</a> as a parameter, and anything that inherits from <code>Control</code> (i.e. <code>TextBox</code>, <code>DropDownList</code>, <code>Label</code> etc.) will work with this:</p> <pre><code>void SetControlBackgroundColour(Control control, Color colour) { if (control != null) { control.BackColor = colour; } } </code></pre> <p>In your example, you could use it like this:</p> <pre><code>private void TextBox1_Focused(object sender, EventArgs e) { SetControlBackgroundColour(sender as Control, Color.Gold); } </code></pre> <p>In response to the comments, you could then use this method in a recursive method that will set the background colour for each control on the form:</p> <pre><code>void SetControlBackgroundColourRecursive(Control parentControl, Color colour) { if (parentControl != null) { foreach (Control childControl in parentControl.Controls) { SetControlBackgroundColour(childControl, colour); SetControlBackgroundColourRecursive(childControl); } } } </code></pre> <p>And then call this function on your <code>Form</code> object (<code>this</code>) in your <code>Form1_Load</code> method (assuming the form is called <code>Form1</code>):</p> <pre><code>protected void Form1_Load(object sender, EventArgs e) { SetControlBackgroundColourRecursive(this, Color.Gold); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload