473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Get the list of Controls in the windows application

Hi guys,
I am having a problem in my application..the prob is that i wanna get the list
of controls on the other form...so what i have done is that
1> I have instantiate the form of which i wanna get the Control list
2> Recurse through a foreach loop and get each control and put the details in a XML file..

Now the main prob that is arising is that the controls are comin i the form in any order(ie textbox,button,textbox etc) and i want to recurse in the order of top to bottom and left to right(ie first label then textbox then again a label and then textbox and so on).

the i have implemented is

Expand|Select|Wrap|Line Numbers
  1. Form2 frm = new Form2();
  2.  
  3.             foreach (System.Windows.Forms.Control ctr in frm.Controls)
  4. {
  5. .....//some codes here
  6. }
  7.  
if anyone can help plz reply and for further clarification do reply....
Oct 30 '07 #1
20 1401
Plater
7,872 Expert 4TB
They will come in the order that they were added to the form (say durring design time)

Just because the control is top-most/left-most control, does not mean it was dumped down on the form "first".

You could probably create some sort of sort function (or use array.Sort() with a custom IComparer thing) that sorts based on .Location of the Control
Oct 30 '07 #2
hey Plater,
Can you explain the thing you said with the help of some example in the form of code....please....as i am new into coding so dnt know much abt this....Please help me out,
Oct 30 '07 #3
Plater
7,872 Expert 4TB
Well if you have a Control[] you can use Array.Sort() to sort them, and if you have a custom IComparer that examines the .Location of the object

See:
http://msdn2.microsoft.com/en-us/library/2b9cyk6x.aspx
Oct 30 '07 #4
hi plater,
what i have done now is that i have created a 2-dimensional array and have have stored the X and Y coordinates of all the controls in it...Now what i want is that i want a logic as to write the compareto function in IComparer so that the sorting can be done....what i want is that i have contorls coordinate as

int[,] points = new int[,] {{152, 172}, {38, 172}, {152, 99}, {152, 52}};

its stored in this fashion after the foreach loop completes..
and after sorting i want it in this fashion
1st> 152,52
2nd>152,99
3rd>38,172
4th>152,172

this is just the reverse of the above but sometimes it may be in any order...
ie top-bottom and Left-Right..
Please help me....
Oct 31 '07 #5
Plater
7,872 Expert 4TB
What are you going to do once you have THAT array stored in top-left -> bottom-right order?
How are you going to related that back to your controls?
You would have wanted a Control[] with a custom IComparer to sort on their locations.
Oct 31 '07 #6
What are you going to do once you have THAT array stored in top-left -> bottom-right order?
How are you going to related that back to your controls?
You would have wanted a Control[] with a custom IComparer to sort on their locations.
Hi Plater,

Hey please yaar give an example code and explain me please...i am not gettin anything...please i am new into this coding world so plz help me out
Oct 31 '07 #7
Plater
7,872 Expert 4TB
Hi Plater,

Hey please yaar give an example code and explain me please...i am not gettin anything...please i am new into this coding world so plz help me out

You need to decide how you will determine which control should come first:
Expand|Select|Wrap|Line Numbers
  1. public class ControlComparer : System.Collections.IComparer
  2.     {
  3.         public int Compare(Object a, Object b)
  4.         {
  5.             Control ca = (Control)a;
  6.             Control cb = (Control)b;
  7.  
  8.             //do stuff based on .Location
  9.             //ca.Location.X 
  10.             //ca.Location.Y 
  11.             //cb.Location.X 
  12.             //cb.Location.Y
  13.             //return either a -1, a 0, or a 1
  14.         }
  15.     }
  16.  
You will also need to have you Control[] created, then you can use Array.Sort():
Expand|Select|Wrap|Line Numbers
  1. Control[] mycontrols = new Control[this.Controls.Count];
  2. this.Controls.CopyTo(mycontrols, 0); 
  3. Array.Sort(mycontrols, new ControlComparer()); 
  4.  

Also, why are you so concerned with getting them in "book-reading" order?
Oct 31 '07 #8
Hi Platter,
See platter i m creating an application where the controls have to be created dynamically through XML file and i have made that application and that application put the controls on the form on the basis of first come first serve ie if first node is a textbox node then a textbox will be created and if the first node is a radiobutton node then the radiobutton will be created but now what i want is I want to go in Reverse ie create an Xml file from a windows application based the controls on the form....So if i get the controls in any order then it would spoil the form when i create a form from the XML generated....
Hope you understood...
Thanx for everything
Oct 31 '07 #9
Plater
7,872 Expert 4TB
Not if you saved the X,Y coordinates to the XML file as well.
Then when you rebuild from the XML file, it won't matter what order they're in because the X,Y coordinates will be used to place them in the correct order.
Oct 31 '07 #10
Hi Platter,
Please tell me one thing in that

Public int Compare(Object a,Object b)

wat is Object a and Object b...and why have you made 2 controls element ie ca and cb....please clarify
Oct 31 '07 #11
No i am not saving the X& Y axis as on creation time i may not require few controls so then i will delete that from XML File and then it will be rendered and if i take X& Y axis in XML then there will be a gap created in the form between controls...
Understood

my xml file looks like this
<code>
<?xml version="1.0" encoding="utf-8"?>
<Fields>
<Field id="UserName">
<Name>UserName</Name>
<Type>TextBox</Type>
</Field>
<Field id="Login">
<Name>Login</Name>
<Type>Button</Type>
</Field>
<Field id="Password">
<Name>Password</Name>
<Type>TextBox</Type>
</Field>
</Fields>
</code>
Oct 31 '07 #12
Hi Platter,

hey my top-bottom and left-right prob is not yet solved....so just nw i m keepin aside that prob... my new prob is that when i am traversing in the
foreach loop for Controls.. i want to get the previous controls name property and put in this controls Name property...so can u help me in that...

becoz this code sinept is giving error...
<code>
string name=ctr.GetNextControl(ctr,false).Name.ToString() .Replace("lbl","");
</code>

this is showin null reference error...i dnt knw y..actually there is a control previous to this control
Nov 1 '07 #13
Plater
7,872 Expert 4TB
GetNextControl() would give you the NEXT control and not the previous one right?

Alos, I gave you almost all you needed for the sorting, all you had to do was fill in the logic in that IComparer and you woulda been done.
Nov 1 '07 #14
that logic is most important thing which i m not able to make... If you are able to unserstand my problem then please help me in makin the logic....

and the GetNextControls can get you previous one also if u specify false in the 2nd argument...but in this its gettin me the next control of the form in which i am writin the code and not of the form which i have instantiated and passing the Control object(ctr).....
Nov 1 '07 #15
Plater
7,872 Expert 4TB
Well your logic is to determine "what control comes first based on X,Y coordinates"

I guess decide which one is more important the X or the Y and start there.

Expand|Select|Wrap|Line Numbers
  1. if (ca.Location.Y < cb.Location.Y)
  2. {
  3.     if (ca.Location.X < cb.Location.X)
  4.     {
  5.     //return either -1,0,1
  6.     }
  7.     else if (ca.Location.X > cb.Location.X)
  8.     {
  9.     //return either -1,0,1
  10.     }
  11.     else
  12.     {// ca.Location.X == cb.Location.X
  13.     //return either -1,0,1
  14.     }
  15. }
  16. else if (ca.Location.Y > cb.Location.Y)
  17. {
  18.     if (ca.Location.X < cb.Location.X)
  19.     {
  20.         //return either -1,0,1
  21.     }
  22.     else if (ca.Location.X > cb.Location.X)
  23.     {
  24.         //return either -1,0,1
  25.     }
  26.     else
  27.     {// ca.Location.X == cb.Location.X
  28.         //return either -1,0,1
  29.     }
  30. }
  31. else
  32. {// ca.Location.Y == cb.Location.Y
  33.     if (ca.Location.X < cb.Location.X)
  34.     {
  35.         //return either -1,0,1
  36.     }
  37.     else if (ca.Location.X > cb.Location.X)
  38.     {
  39.         //return either -1,0,1
  40.     }
  41.     else
  42.     {// ca.Location.X == cb.Location.X
  43.         //return either -1,0,1
  44.     }
  45. }
  46.  
Nov 1 '07 #16
for me Y is the most important...and X take a 2nd priority....meaning if i have a contol at X=23 and Y=50 and another control at X=30 and Y=40..

Then the second control should come first...even if in second X is greater then X in first one
Nov 1 '07 #17
Plater
7,872 Expert 4TB
ok so then with Y being most important you can cut that code to this:
Expand|Select|Wrap|Line Numbers
  1. if (ca.Location.Y < cb.Location.Y)
  2. {// ca is closer to the top then cb
  3.     //return either -1,0,1
  4. }
  5. else if (ca.Location.Y > cb.Location.Y)
  6. {// cb is closer to the top then ca
  7.     //return either -1,0,1
  8. }
  9. else
  10. {// ca and cb are at the same "close-ness" to the top
  11.     if (ca.Location.X < cb.Location.X)
  12.     {
  13.         //return either -1,0,1
  14.     }
  15.     else if (ca.Location.X > cb.Location.X)
  16.     {
  17.         //return either -1,0,1
  18.     }
  19.     else
  20.     {// ca.Location.X == cb.Location.X
  21.         //return either -1,0,1
  22.     }
  23. }
  24.  
Nov 1 '07 #18
Hey Platter,
Dude Thanx man...Your code worked and its givin me proper results.....Thanx a ton man....thanx a lot.....i m relieved man....
Nov 2 '07 #19
Hey Platter,
Can you solve my one more problem....

See now when i get the controls collection in that mycontrols[] then i m loopin through each control..and the controls that i gettin are on the other form then the one in which i m writing the codes.... so now i have a requirement where i need to check which is the next control in that loop...

but when i use GetNextControl(ctr,true) this gives me the next control in the frm which i workin i dnt knw why...and when i do this

ctr.GetNextControl(ctr,true) then it gives me a null reference exception...

Can u tell me a way by which i can access the next control in that loop...my codin is this way
Expand|Select|Wrap|Line Numbers
  1. Control[] mycontrols = new Control[frm.Controls.Count];
  2. frm.Controls.CopyTo(mycontrols, 0);
  3. Array.Sort(mycontrols, new ControlComparer());
  4.      foreach (System.Windows.Forms.Control ctr in mycontrols)
  5.      {
  6.         if (ctr.GetNextControl(ctr,true).GetType.FullName.ToString() != "System.Windows.Forms.RadioButton")
  7.                     {
  8.                         SaveRadio(radio, ctr, filename1);
  9.                     }
  10.  
  11.      }
  12.  
Nov 2 '07 #20
Plater
7,872 Expert 4TB
How about using an index'd loop instead?
Expand|Select|Wrap|Line Numbers
  1. Control[] mycontrols = new Control[frm.Controls.Count];
  2. for (int i=0;i<mycontrols.Length;i++)
  3. {
  4.    Control ctr = mycontrols[i];
  5.    Control nextctr=mycontrols[i+1];
  6. }
  7.  
Nov 2 '07 #21

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: RCCNH | last post by:
I am creating a Windows application using C# in .NET. In one of the windows, I have to show a scrollable list of user objects. Those user objects contain various controls themselves (textbox,...
1
by: Norman Fritag | last post by:
Hi there I have avoided to use active x controls because I thought they are causing more problems then they are doing any good. I a new application I would want to use the tree and list view...
6
by: Danny Lesandrini | last post by:
I'm using an Access database to drive a web site and the colors of various table backgrounds are stored in Access. I want users of the Access database to be able to select colors for the site, but...
3
by: Roger | last post by:
Hi In a Windows forms application I have 2 forms A and B; Form B inherits from form A. Form A is never displayed and its only purpose is to be inherited from and therefore contains mostly...
3
by: Nathan Sokalski | last post by:
When I view any page in my application a second time, I recieve the following error: System.Web.TraceContext.AddNewControl(String id, String parentId, String type, Int32 viewStateSize) +313...
0
by: Brian Henry | last post by:
Here is another virtual mode example for the .NET 2.0 framework while working with the list view. Since you can not access the items collection of the list view you need to do sorting another...
4
by: Toze | last post by:
I'm using a assembly to load my apllication (ex: Mobi.exe), and now I need to list all forms in my apllication and list all controls (ex: txtname;btnname) inside of each form.
0
by: morathm | last post by:
I have a windows client database management application written in C# that connects to remote web services to do all the heavy work. The thin-client app uses strong typed datasets, all maintained at...
4
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, I have just started building an application which is windows form based, rather than web based, and I am having troubles with layout. I can't find any control which gives me just a simple text...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.