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 - Form2 frm = new Form2();
-
-
foreach (System.Windows.Forms.Control ctr in frm.Controls)
-
{
-
.....//some codes here
-
}
-
if anyone can help plz reply and for further clarification do reply....
20 1401
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
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,
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....
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.
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
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: -
public class ControlComparer : System.Collections.IComparer
-
{
-
public int Compare(Object a, Object b)
-
{
-
Control ca = (Control)a;
-
Control cb = (Control)b;
-
-
//do stuff based on .Location
-
//ca.Location.X
-
//ca.Location.Y
-
//cb.Location.X
-
//cb.Location.Y
-
//return either a -1, a 0, or a 1
-
}
-
}
-
You will also need to have you Control[] created, then you can use Array.Sort(): -
Control[] mycontrols = new Control[this.Controls.Count];
-
this.Controls.CopyTo(mycontrols, 0);
-
Array.Sort(mycontrols, new ControlComparer());
-
Also, why are you so concerned with getting them in "book-reading" order?
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
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.
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
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>
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
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.
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).....
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. -
if (ca.Location.Y < cb.Location.Y)
-
{
-
if (ca.Location.X < cb.Location.X)
-
{
-
//return either -1,0,1
-
}
-
else if (ca.Location.X > cb.Location.X)
-
{
-
//return either -1,0,1
-
}
-
else
-
{// ca.Location.X == cb.Location.X
-
//return either -1,0,1
-
}
-
}
-
else if (ca.Location.Y > cb.Location.Y)
-
{
-
if (ca.Location.X < cb.Location.X)
-
{
-
//return either -1,0,1
-
}
-
else if (ca.Location.X > cb.Location.X)
-
{
-
//return either -1,0,1
-
}
-
else
-
{// ca.Location.X == cb.Location.X
-
//return either -1,0,1
-
}
-
}
-
else
-
{// ca.Location.Y == cb.Location.Y
-
if (ca.Location.X < cb.Location.X)
-
{
-
//return either -1,0,1
-
}
-
else if (ca.Location.X > cb.Location.X)
-
{
-
//return either -1,0,1
-
}
-
else
-
{// ca.Location.X == cb.Location.X
-
//return either -1,0,1
-
}
-
}
-
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
ok so then with Y being most important you can cut that code to this: -
if (ca.Location.Y < cb.Location.Y)
-
{// ca is closer to the top then cb
-
//return either -1,0,1
-
}
-
else if (ca.Location.Y > cb.Location.Y)
-
{// cb is closer to the top then ca
-
//return either -1,0,1
-
}
-
else
-
{// ca and cb are at the same "close-ness" to the top
-
if (ca.Location.X < cb.Location.X)
-
{
-
//return either -1,0,1
-
}
-
else if (ca.Location.X > cb.Location.X)
-
{
-
//return either -1,0,1
-
}
-
else
-
{// ca.Location.X == cb.Location.X
-
//return either -1,0,1
-
}
-
}
-
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....
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 -
Control[] mycontrols = new Control[frm.Controls.Count];
-
frm.Controls.CopyTo(mycontrols, 0);
-
Array.Sort(mycontrols, new ControlComparer());
-
foreach (System.Windows.Forms.Control ctr in mycontrols)
-
{
-
if (ctr.GetNextControl(ctr,true).GetType.FullName.ToString() != "System.Windows.Forms.RadioButton")
-
{
-
SaveRadio(radio, ctr, filename1);
-
}
-
-
}
-
How about using an index'd loop instead? -
Control[] mycontrols = new Control[frm.Controls.Count];
-
for (int i=0;i<mycontrols.Length;i++)
-
{
-
Control ctr = mycontrols[i];
-
Control nextctr=mycontrols[i+1];
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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.
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
| |