1

how to set back color for all text boxes in win form globally? can we set it somewhere like in a global variable and use when needed? I need to set the back color of control in form_load from a global variable instead of writing

mytextbox1.BackColor = Color.Red;
mytextbox2.BackColor = Color.Red; 
Zigma
  • 529
  • 6
  • 37
Prathap
  • 49
  • 10
  • Have you tried below answer? – Mohammad Arshad Alam Nov 26 '14 at 05:56
  • yes it works fine. i want to use this method in all forms, how to access this method globally? – Prathap Nov 26 '14 at 06:22
  • Updated the answer. Please check – Mohammad Arshad Alam Nov 26 '14 at 06:24
  • public class Helper { public void SetRedColorToTextBoxes(Form frm) --> Form could not be found(are you .... { Action func = null; --> control could not be found(are you .... func = (controls) => { foreach (Control control in controls) if (control is TextBox) (control as TextBox).BackColor = Color.Red; else func(control.Controls); }; func(frm.Controls); } } – Prathap Nov 26 '14 at 06:30
  • add two namespace `using System.Drawing; using System.Windows.Forms;` – Mohammad Arshad Alam Nov 26 '14 at 06:32
  • it works well. thanks a lot Arshad – Prathap Nov 26 '14 at 06:37
  • we can assign the backcolor of controls without using this. what is the advantage of using action delegate? – Prathap Nov 26 '14 at 06:41
  • if i want to access this 'SetRedColorToTextBoxes' method from Helper class, i can create the object of Helper class and i can acess it. Here you've written '(new Helper()).SetRedColorToTextBoxes(this);' does it means same as creating object and accessing the method? – Prathap Nov 26 '14 at 06:44

3 Answers3

5
 private void SetRedColorToTextBoxes()
 {
     Action<Control.ControlCollection> func = null;

     func = (controls) =>
         {
             foreach (Control control in controls)
                 if (control is TextBox)
                     (control as TextBox).BackColor = Color.Red;
                 else
                     func(control.Controls);
         };

     func(Controls);
 }

and call SetRedColorToTextBoxes() function in form load.

    private void YourForm_Load(object sender, EventArgs e)
    {
        SetRedColorToTextBoxes();
    }

Edit Add a .cs file and put the code there.

  class Helper
   {
    public void SetRedColorToTextBoxes(Form frm)
    {
        Action<Control.ControlCollection> func = null;

        func = (controls) =>
        {
            foreach (Control control in controls)
                if (control is TextBox)
                    (control as TextBox).BackColor = Color.Red;
                else
                    func(control.Controls);
        };

        func(frm.Controls);
    }
}

and call it your form load as :

 private void YourForm_Load(object sender, EventArgs e)
    {
        // this means instance of  currentform.
       (new Helper()).SetRedColorToTextBoxes(this);
    }
Mohammad Arshad Alam
  • 9,694
  • 6
  • 38
  • 61
  • thanks for your reply, it works fine. but to call this method in another form i need to write this method again.could you please tell me a solution for this.I need to access this same method globally. – Prathap Nov 26 '14 at 06:04
  • @Prathapsv: Simply make this method `static` within some (maybe) static helper class and provide all parameters within the method call. – Oliver Nov 26 '14 at 08:25
  • @Arshad: If you have to test an object to be of a specific type and afterwards need to use it as this type you should not double type check it. Either you write `if(control is TextBox) ((TextBox)control).BackColor = Color.Red;` or (what i would prefer in this case) `var textBox = control as TextBox; if(textBox != null) textBox.BackColor = Color.Red;`. Also take a look at [this SO question](http://stackoverflow.com/questions/496096/casting-vs-using-the-as-keyword-in-the-clr). – Oliver Nov 26 '14 at 08:28
0

Can i make sure you are not looking for themes. Some third parties like devexpress gives option to design your own themes

Binesh Nambiar C
  • 152
  • 1
  • 2
  • 9
0

As a slight change to Arshad's solution above, I would also make the method to take colour on the fly:

public void SetRedColorToTextBoxes(Form frm, Color myColor)
silverspoon
  • 1,085
  • 1
  • 11
  • 22