8 Replies - 11392 Views - Last Post: 18 March 2011 - 11:52 AM Rate Topic: -----

#1 fabiocrj  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 25
  • Joined: 09-February 11

List of ALL Controls in pROJECT - VB.net

Posted 16 March 2011 - 06:16 AM

Hi all,

I dont know if this is the right area. Please sorry if not.
Well, first of all, thanx for your attention. Ive searched the whole web (at last where google can see) for a solution, but i just cant find.
The problem is the following: I want a list of all controlls in my project, not only on open forms. The reason is that i want to make an access control, and the admin will set which buttons each users can use.

what ive got so far is:

Public Sub getallforms(ByVal sender As Object)
Dim Forms As New List(Of Form)()
Dim formType As Type = Type.GetType("System.Windows.Forms.Form")
For Each t As Type In sender.GetType().Assembly.GetTypes()
If UCase(t.BaseType.ToString) = "SYSTEM.WINDOWS.FORMS.FORM" Then
MsgBox(t.Name)
End If
Next
End Sub


This shows me all forms in project. But i cant access their controls. to you have any ideia of how doing what i want?
Thank you very much!

Is This A Good Question/Topic? 0
  • +

Replies To: List of ALL Controls in pROJECT - VB.net

#2 MrPerq  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 57
  • Joined: 01-March 11

Re: List of ALL Controls in pROJECT - VB.net

Posted 18 March 2011 - 09:00 AM

I am correct you want the classes of the forms (not the actual instances)
so you can make an inventory of all buttons in your app?
(Thats what you are doing with current code)
If that is the case, reflection might help you.

If you wanne enable and disable buttons according to their rights,
why dont you just handle those in the load event?
(You do have the code of the forms dont you?)
Was This Post Helpful? 0
  • +
  • -

#3 fabiocrj  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 25
  • Joined: 09-February 11

Re: List of ALL Controls in pROJECT - VB.net

Posted 18 March 2011 - 09:26 AM

yah, i have just completed the code for listing all buttons.
The reason i want the list of them, is to create users groups with access rights. Example:
FormA: Button1, Button2, Button3
FormB: Button1
FormC: Button1, Button2, Button3, Button4

So ill have the following items to manage accessibility:

FormA.Button1
FormA.Button2
FormA.Button3
FormB.Button1
FormC.Button1
FormC.Button2
FormC.Button3
FormC.Button4


So than i can make a group that can press FormC.Button2 and FormA.Button1, but not the others, and a groups that can press FormC.Button2, FormA.Button2, FormA.Button3, etc

So,. in the load event of the form, i make a query to my database and search for user groups and their accessibility right:

if verifyRight(me.name & Button1.name, UserID) than Button1.enabled=true


function verifyRight(Byval command as String, ByVal UserId as integer) as Boolean
verify in the database and returns
end Function


View PostMrPerq, on 18 March 2011 - 09:00 AM, said:

I am correct you want the classes of the forms (not the actual instances)
so you can make an inventory of all buttons in your app?
(Thats what you are doing with current code)
If that is the case, reflection might help you.

If you wanne enable and disable buttons according to their rights,
why dont you just handle those in the load event?
(You do have the code of the forms dont you?)

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9393
  • View blog
  • Posts: 35,273
  • Joined: 12-June 08

Re: List of ALL Controls in pROJECT - VB.net

Posted 18 March 2011 - 09:37 AM

Have you thought about using the ".controls" method?

Example
Form2.Controls


You can use that to return a user control or form's collection of controls. It's public so it is accessible.
Was This Post Helpful? 0
  • +
  • -

#5 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: List of ALL Controls in pROJECT - VB.net

Posted 18 March 2011 - 09:53 AM

He wants to list all the forms that are in the project and then list all their controls. The code that you use to get the form names is correct however its saved in variable of type Type and you cant use it as object(form). However if your going to make it in load event why dont you just send the current form and then loop all the controls:
    Public Sub Test(ByRef frm As Form)
        For Each cControl As Control In frm.Controls
            MessageBox.Show(cControl.Name)
            'You can put here the validation if the user have rights to access the control and enable/disable it'
        Next
    End Sub


This post has been edited by NoBrain: 18 March 2011 - 09:59 AM

Was This Post Helpful? 0
  • +
  • -

#6 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: List of ALL Controls in pROJECT - VB.net

Posted 18 March 2011 - 10:00 AM

View PostNoBrain, on 18 March 2011 - 09:53 AM, said:

He wants to list all the forms that are in the project and then list all their controls. The code that you use to get the form names is correct however its saved in variable of type Type and you cant use it as object(form). However if your going to make it in load event why dont you just send the current form and then loop all the controls:
        For Each cControl As Control In frm.Controls
            MessageBox.Show(cControl.Name)
        Next




I think the OP wants to get every form, and then list each control on each form - without referencing to each form. The problem is that you can't loop through the array of Types which he had. It's a little weird that VB doesn't let you loop through My.Forms... It's an interesting question.
This would work for one spesific form, but not all of them in the project:
        For Each cControl As Control In frm.Controls
            MessageBox.Show(cControl.Name)
        Next



The closest thing that I've got is:
For each frm In My.Application.OpenForms     
   For Each cControl As Control In frm.Controls
         MessageBox.Show(cControl.Name)
   Next



This would retrieve all control names in your project, but the prerequisite would be to have all your forms open at once. It's a weird problem for which I'm surprised I can't find a better solution.
Was This Post Helpful? 0
  • +
  • -

#7 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: List of ALL Controls in pROJECT - VB.net

Posted 18 March 2011 - 10:07 AM

View PostJack Eagles1, on 18 March 2011 - 07:00 PM, said:

The closest thing that I've got is:
For each frm In My.Application.OpenForms     
   For Each cControl As Control In frm.Controls
         MessageBox.Show(cControl.Name)
   Next



This would retrieve all control names in your project, but the prerequisite would be to have all your forms open at once. It's a weird problem for which I'm surprised I can't find a better solution.

This will loop all the opened forms. However my point was if the OP is going to check if the current form that loads for user rights and enable the controls when the form loads in his method that will check the right to send the current form

Quote

if verifyRight(me.name & Button1.name, UserID) than Button1.enabled=true


My idea is in verifyRight() method to just send the current form (Me) and the user id. If he send it by ref he can enable the buttons while looping all the controls of that form. I dont see the point of looping all the forms if your going to make the check in the load event of a form
Was This Post Helpful? 0
  • +
  • -

#8 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: List of ALL Controls in pROJECT - VB.net

Posted 18 March 2011 - 10:41 AM

I found this while playing around with this topic the other day but never got back to it.
It will give you the name of all the forms. Perhaps someone could use that to then find the controls.

Dim list = AppDomain.CurrentDomain.GetAssemblies().ToList(). _
                    SelectMany(Function(s) s.GetTypes()). _
                    Where(Function(p) (p.BaseType Is [GetType]().BaseType AndAlso _
                                       p.Assembly Is [GetType]().Assembly))
For Each type As Type In list
    Dim typeName As String = type.Name
Next


Was This Post Helpful? 0
  • +
  • -

#9 fabiocrj  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 25
  • Joined: 09-February 11

Re: List of ALL Controls in pROJECT - VB.net

Posted 18 March 2011 - 11:52 AM

I got it:

In the load event:
       For Each formprop In My.Forms.GetType.GetProperties 'percorre todos os forms
            Dim node = Me.tvwListaControles.Nodes.Add(formprop.Name)
            Dim frm As Form = CType(formprop.GetValue(My.Forms, Nothing), Form)
            ControlsTree(node, frm.Controls)
        Next


And a recursive sub

    Private Sub ControlsTree(ByVal node As TreeNode, ByVal controls As Control.ControlCollection)
        For Each ctrl As Control In controls
            If ctrl.Name <> String.Empty And ctrl.GetType.Name <> "Label" Then
                Dim child = node.Nodes.Add(ctrl.Tag, ctrl.GetType.Name & ": " & ctrl.Name)
                ControlsTree(child, ctrl.Controls)
            End If
        Next
    End Sub


It will populate a tree control! Just what i was looking for!
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1