CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2007
    Posts
    4

    Exclamation 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


    Code: ( text )
    <code>
    Form2 frm = new Form2();

    foreach (System.Windows.Forms.Control ctr in frm.Controls)
    {
    .....//some codes here
    }
    </code>
    if anyone can help plz reply and for further clarification do reply....

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Get the list of Controls in the windows application

    The problem is that by looping through the controls on the form, like you did, it will loop from the last control added up to the control that was added first. For example, if you added a textbox, radiobutton, checkbox and listbox ( in that order ), the loop will produce : listbox, checkbox, radiobutton, textbox

    What you need to do is to perhaps add a variable which holds the amount of controls on the form, similar to this :
    Code:
    CCount = frm.Controls.Count - 1; //This holds the amount of controls on the form
    Then, in your foreach loop use the reference the index of a certain control, then subtract one from the original control count, something like :
    Code:
    foreach (System.Windows.Forms.Control ctr in frm.Controls)
    {
        MessageBox.Show(frm.Controls(CCount).Name.ToString);
        CCount = CCount - 1;
    }
    This should loop from the first control added, to the last control added to the form. Does this help ¿

    //PS, I wrote this mainly in VB, but I think I translated it correct

  3. #3
    Join Date
    Oct 2007
    Posts
    4

    Re: Get the list of Controls in the windows application

    hi hannes,

    Thanx a lot this has helped a lot...but suppose i have created the controls at the design in any order but now i want to get the control as the way it is rendered on the form...then how should i do.. i thought of cretaing an array of the locations of the controls and then sorting it and then takin the controls based on the location sorted...

    did u understand..

    I mean The Form looks like

    Label2 textbox3
    label1 textbox2
    label3 textbox1

    button

    now in this i have draged textbox1 first then label1 then textbox2 then label2 and so on

    but arranged like the one showed above

    then how to take in that order....can u think on the Location thing..

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Get the list of Controls in the windows application

    OK, what I'd suggest is to create another Control object, set that Control object equal to "the previous Control". Then, compare the Left and Top properties of these objects. Something like this :
    Code:
     Control ctrl; 
        Control ctrl2; 
        
        int CCount; 
        int ActCount; 
        
        CCount = this.Controls.Count - 1; 
        string[] ActControl = new string[CCount + 1]; 
        
        foreach ( ctrl in this.Controls) { 
            if (!(CCount == 0)) 
                ctrl2 = this.Controls(CCount - 1); 
            
            if (ctrl2.Top < ctrl.Top) { 
                if (ctrl2.Left < ctrl.Left) { 
                    ActControl(ActCount) = ctrl2.Name.ToString; 
                    MessageBox.Show(ActControl(ActCount)); 
                    CCount = CCount - 1; 
                } 
                
            } 
            
        }
    It is way from perfect, but it should give you more or less an idea of how to go about with this. Just for clarity, I added an Array, in which the Control Object's names get stored.

  5. #5
    Join Date
    Mar 2004
    Posts
    223

    Re: Get the list of Controls in the windows application

    If comparing the TabIndex property helps, then that would oranize your code too.

    [If you find my answers useful, please leave your comments and rating for it.]
    Happy Coding!
    My Dev.: MS VS 2005 Version 8.0.50727.42 .NET 2.0.50727
    Mark your answers Tools>>RESOLVED once its answered.

  6. #6
    Join Date
    Oct 2007
    Posts
    4

    Re: Get the list of Controls in the windows application

    hi,
    Thanx to you guys my that prob is solved..bt i have one more prob that is..

    See i am gettin the list of controls in another form in 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

    Code: ( text )
    Code:
    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).ToString() != "System.Windows.Forms.RadioButton")
                        {
                            SaveRadio(radio, ctr, filename1);
                        }
    
         }
    for further clarification please reply

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured