Click here to Skip to main content
15,884,917 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Experts,

My winform has various texboxes inside panels and outside the panels.
I want to set their properties like backcolor, enabled etc in a separate class.
but we are unable to do it.

I have also tried like this: "foreach(Control c in Controls)", but unable to access that control exits in panel.

Kindly suggest me the best.

--- Anil Kumar.

Thanks for prompt replies. In that scenario i have already implemented successful code for datagridview as below: -

code for the form having DataGridView:
======================================
...
....
...
ConnetDataBase conDB = new ConnetDataBase();
...
...
conDB.FillDataGrid(Sql, dgvUserList);

code for class: -
================
C#
class ConnetDataBase
    {

public void FillDataGrid( string Sql,DataGridView dgvName)
{
    try
    {
        connection.Open();
        adapter = new SqlDataAdapter(Sql, connection);
        DataSet ds = new DataSet();
        adapter.Fill(ds);
         dgvName.DataSource = ds.Tables[0];
        connection.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

}

---------------------------
This works fine for any datagridview calling from any forms.
But for textbox or other controls how can it may be implemented ?

--- Anil kumar
Posted
Updated 29-Apr-19 13:27pm
v4

If you want to access the controls inside a panel you have to address the panel's controls collection.

So to get this working you'll have to check the controls type and if it's a panel you'll have to go over all the panels controls again.
 
Share this answer
 
This snippet might help you :

C#
foreach (Control cntol in this.Controls)
{
    Type type = cntol.GetType();
    //if (type.Name == "Panel")
    if(cntol is Panel)
    {
        foreach (Control panelControls in cntol.Controls)
        {
            //Access the panel's controls here
        }
    }
    else
    {
        // Access the controls outside of the panels here
    }
}
 
Share this answer
 
v2
Comments
Rojeh 18-Apr-11 8:53am    
My 5
Tarun.K.S 18-Apr-11 9:01am    
Thanks!
Kim Togo 18-Apr-11 9:01am    
My 5. But instead of
if (type.Name == "Panel")

Use

if (cntrol is Panel)

type.Name is by default empty.
Tarun.K.S 18-Apr-11 9:34am    
Thanks and sure I shall use this now. :)
BobJanova 18-Apr-11 10:05am    
Controls other than panels can have sub-controls, though they usually don't. Why not just remove the if test entirely and traverse all subtrees? Most of them will have 0 element Controls arrays anyway.
The background colour of many controls is inherited, if you don't set it explicitly.

In the general case, assuming you are wanting to set these control properties based on something in your business logic, it is better to use data binding than to write explicit property manipulation code, which can get very complex and hard to keep in sync surprisingly quickly. Create a presenter (or controller or model-view) class for the form, or perhaps more if it is pulling data from logically different locations, and data bind the properties of the controls to properties of that class.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Apr-11 14:38pm    
These are useful notes, my 5.
The setup function can be anything, not just color all in the same color (which would not require any traversing of the controls).
You did not fully answer the question though, but I did, please see.
--SA
BobJanova 18-Apr-11 16:19pm    
You're absolutely correct, I did not fully answer the question as written, because that way lies madness ;) but I believe your answer is correct as it stands.
Sergey Alexandrovich Kryukov 18-Apr-11 16:45pm    
Well, thank you, Bob.
I understand your approach; you did not have to answer in full; again, your information is useful.
--SA
You need to use recursion in this case:

static void SetupTextBox(TextBox textBox) {
   //text.Box.BackColor = ...
   //whatever setup you need
}
static void SetupTextBoxes(Control parent) {
   TextBox textBox = parent as TextBox;
   if (textBox != null)
      SetupTextBox(textBox);
   foreach(Control control in parent.Controls)
      SetupTextBoxes(control);
}

//...

SetupTextBoxes(myFormInstance);
or, in the method of your form:
SetupTextBoxes(this);


—SA
 
Share this answer
 
Comments
Sander Rossel 18-Apr-11 16:52pm    
Very nice. Never looked at it like that. 5 from me.
Sergey Alexandrovich Kryukov 18-Apr-11 16:55pm    
Thank you.
This is simple and very basic.
--SA
Sander Rossel 18-Apr-11 17:01pm    
Simple thinking can be very hard sometimes! :laugh:
Sergey Alexandrovich Kryukov 18-Apr-11 17:25pm    
Who would argue against that!
Thank you.
--SA
Espen Harlinn 18-Apr-11 17:42pm    
A 5 :)
I see some good answers here. So this answer is simply adding to what has already been said, which also works. Hoewever, with the previous code example if you will use a TabControl or GroupBox instead of a Panel your code will not work anymore. The following snippet, which makes use of a recursive method, should fix this. C# is not my main language, bear with me please ;)

C#
private void button1_Click(object sender, EventArgs e)
        {
            ColorControls(this); // A Form is also a Control.
        }

        private void ColorControls(Control ctl)
        {
        // Loop through the controls in a Control.
        foreach (Control c in ctl.Controls)
           if (c.Controls.Count > 0)
            {
                // If the control contains multiple Controls (Container Controls))
                // then loop through those controls.
                ColorControls(c);
            }
            else
            // If there are no more controls then check if
            // this control is a TextBox (or any type you want).
           {
                if (c is TextBox)
                {
                    // Set background color.
                    c.BackColor = Color.Red;
                }
            }
        }

Hope it helps.
 
Share this answer
 
Comments
ricmil42 18-Apr-11 14:16pm    
Yup, works for any level of nested controls.
Sergey Alexandrovich Kryukov 18-Apr-11 14:41pm    
A bit of redundant code here, my 4.
No need to check up Count, "is" is redundant: you really need to use "as" because it provides the check and the cast at the same time. You did not have cast at all, because you assume that the property is universal. But what to do is some property to be set up is TextBox-specific? So, your code is less universal without any gain.
Please see my Answer.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900