Click here to Skip to main content
15,936,482 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Panel pan = (Panel) this.tabPage1.Controls.Find("a" + i.ToString(),true);
            CheckBox ck = (CheckBox)this.tabPage1.Controls.Find("check" + i.ToString(), true);




in above code that i am creating number of panels dynamically in C#.net(windows application)and in that each panel that am addng the checkboxs dynamically


here my requirement is that accessing the particular checked checkbox and writintg the value to the corresponding database

in above code that ai means (a1,a2,a3.........)panels created dynamically
checki means (check1,check2,.........)checkboxs created dynamically
what is the error in it
and how should i access it

more over i am getting the error as
1).cannot converttype system.windows.forms.controls[] to system.windows.forms.panel
2).cannot converttype system.windows.forms.controls[] to system.windows.forms.checkbox
Posted
Updated 13-Aug-10 2:42am
v2
Comments
E.F. Nijboer 13-Aug-10 8:49am    
If you set a breakpoint and go look at the names of the dynamically created controls, are the names as you expected (and when you break in the loop, does it equals "a" + i.ToString())? Are you sure their parent is the tabpage you are looking on? Step through it with the debugger and sure to find it quick and easy.
Samuel Cherinet 13-Aug-10 16:10pm    
am confused here ... why are you looking for the panel in the first place ... I mean you are looking for the checkbox in the tabpage??? if you added the checkboxes in the panels then you should be looking for the checkbox inside the panel.

1 solution

Try something like:
C#
Panel pan = (Panel)this.tabPage1.Controls.Find("a" + i.ToString(), true)[0];
CheckBox ck = (CheckBox)this.tabPage1.Controls.Find("check" + i.ToString(), true)[0];

Of course you should also check if the control you're looking for exists at all and is indeed a checkbox or panel:

C#
Control[] panels = this.tabPage1.Controls.Find("a" + i.ToString(), true);
Panel pan = null;
CheckBox ck = null;
if (panels.Length > 0 && panels[0] is Panel)
{
    pan = (Panel)panels[0];
}
else
{
    //The panel does not exist - do whatever you think is appropriate
}

Control[] checkBoxes = this.tabPage1.Controls.Find("check" + i.ToString(), true);
if (checkBoxes.Length > 0 && checkBoxes[0] is CheckBox)
{
    ck = (CheckBox)checkBoxes[0];
}
else
    //Checkbox doesn't exist
 
Share this answer
 
v2
Comments
[no name] 14-Aug-10 4:37am    
Reason for my vote of 5
perfect one matching answer

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