Kiran Y N
Kiran Y N

Reputation: 29

getting control names programmatically in c#

I am relatively new to c# .net. so please let me know if you want more input to answer my query.

1) what i am trying to do ?

The form that I am using is having nearly 30 data grid view controls in 3 different tabs. And name of datagridview's are below. dgView1, dgView2, dgView3

Along with above datagrid control, i also got few textbox controls, so to be more specific in tab 1 .. i got below control items. txtTabName1, txtStrKey1, dgView1

now I am trying to write a function which will take one input parameter say int v_CtrlNum and using this parameter I need to scan each of the items from one tab and add it to an ArrayList/Collection.

so for example the function will need to read each row from the datagrid view as shown below

for datagrid

foreach (DataGridViewRow in dgView+v_CtrlNum )

for textbox

txtTabName+v_CtrlNum

I'd like to know if I'm taking the right direction doing this.

Upvotes: 2

Views: 2095

Answers (3)

Mason
Mason

Reputation: 721

What I think you might want to do is something like this:

DataGridView[] formDataGrids = this.Controls.OfType<DataGridView>().ToArray();

This will get you an array of all of the DataGridViews in your form. You can probably do it for an individual tab by using the control list of that tab. You could do the same thing for textbox, just replace the array type and the type in the OfType() call with TextBox.

You can't use foreach like in your example, because the right side of the "in" must be a reference to a specific List or Array (something that implements IEnumerable). But if you created a list like above, you could then do something like:

foreach(DataGridView thisGrid in formDataGrids)
    DoSomething(thisGrid);

Or also chain them together like:

foreach(DataGridView thisGrid in this.Controls.OfType<DataGridView>())
    DoSomething(thisGrid);

For the multiple tab handling, you should already have TabPage members created in your designer for each page. You could then do something like:

var formDataGrids = new List<DataGridView>();
if (usingTab1)
    formDataGrids.AddRange(tabPage1.Controls.OfType<DataGridView>());
if (usingTab2)
    formDataGrids.AddRange(tabPage2.Controls.OfType<DataGridView>());
if (usingTab3)
    formDataGrids.AddRange(tabPage3.Controls.OfType<DataGridView>());

foreach(var thisGrid in formDataGrids)
    DoSomething(thisGrid);

Upvotes: 0

Mark Hall
Mark Hall

Reputation: 54562

You can look into the Controls.Find Method be aware it returns an array of controls that match.

Control[] tbp = tabControl1.Controls.Find("txtTabName" + 2,true );
if (tbp.Length > 0)
{
    Control[] dv = tbp[0].Controls.Find("dgView" + 2, true);
}

Upvotes: 2

yu_ominae
yu_ominae

Reputation: 2935

Not entirely sure I am following what you are trying to achieve, but I think you just want to get a control by an ID number, right? You could do something like this:

List<Controls> myTabControls = new List<Controls>();
foreach (Control thisControl in this.Controls)
    if (thisControl.Name.Contains(v_CtrlNum.ToString()))
        foreach (Control thisChildControl in thisControl.Controls)
            myTabControl.Add(thisChildControl)thisChildControl

To get the controls in the tab corresponding to v_CtrlNum, assuming that v_CtrlNum is an identifier that's part of the control name. Then go through the controls from the tab to process each DataGridView, possibly like this:

foreach (Control thisControl in myTabContols)
    if (thisControl.GetType() == typeof(DataGridView))
       // Parse your DataGridView's rows here

Where this.Controls is your form's control collection (this refers to your parent form in this case).

Does this help? But I am not sure whether or not I properly understood what you are asking about in your question...

Upvotes: 0

Related Questions