Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 13-Aug-10 2:34am
Edited 13-Aug-10 2:42am
v2
Comments
E.F. Nijboer at 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.
samqty at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try something like:
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:
 
            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 
  Permalink  
v2
Comments
sivakumarmr10 at 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)



Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 13 Aug 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100