subashbtechmba Posted June 26, 2009 Posted June 26, 2009 Hi, I want to get the controls inside a tab page or group box which is in a windows forms in C#. Ex: foreach(Controls ctrl in tabPage1.controls) { if(ctrl is Label) { my code is here } } Above code is the written by me for accesing Label Controls in my form.But i cant retreive Label Control Because it is inside the tabpage. Is there any solution for this without specifying Groupbox1.Controls or tabpage1.controls Plesae help me with the code Thanks Subash Quote
iMP Posted June 26, 2009 Posted June 26, 2009 Hmm.. Shouldn't it work to check in the loop if the ctrl has any controls in it, and then loop through them to? Perhaps create a recursive sub for it or something :) Just a thought, I might be totally wrong:P hehe Quote
DPrometheus Posted June 29, 2009 Posted June 29, 2009 As iMP said, you should get something like this: vb.NET Private Function CheckControls(ByVal control As Control) As Control For Each c As Control In control.Controls If c.Controls.Count > 0 Then CheckControls(c) If TypeOf c Is Label Then ' Do something with c Return c End If Next Return Nothing End Function [/Code] or C# [Code] private Control CheckControls(Control control) { foreach (Control c in control.Controls) { if (c.Controls.Count > 0) CheckControls(c); if (c is Label) { // Do something with c return c; } } return null; } [/Code] ~DP Quote My Development System Intel Core i7 920 @2.66Ghz 6 GB DDR3 SDRAM Windows 7 Ultimate x64 & Windows Vista Home Premium x64 dual boot GeForce GTX295 1.8 GB 3.5 TB HD
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.