Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Make a custome member reference?

Status
Not open for further replies.

Babillon

Programmer
Aug 16, 2005
25
CA
I'm trying to create a function that will loop through a ControlCollection and apply a user specified 'mode' to it (such as enabled = false, or clearing a textbox, etc).

Code:
Public Function setMode(ByRef controls As Control.ControlCollection, ByVal mode As String) As Boolean
        Dim i As Control
        Dim command As String

        For Each i In controls
            If TypeOf i Is TextBox Then

            End If
        Next
    End Function

This is what I have so far. I'd like to be able to concatinate a the user supplied mode with CType(i, TextBox) {Eventually I'd like to make even the object type to check be dynamic}.

For example the user calls this:
setMode(grpDisplay.Controls, clear)

Would clear all of the textboxes in the group box.

I've tried
Ctype(i, TextBox). & mode
this give me Identifier Expected.

So, this brings me to my questions:
Is there any way to use a string as an object property? Or am I going to have to admit defeat and use a giant switch?
 
I'm not sure if this will work but

Code:
Public Function setMode(ByRef controls As Control.ControlCollection, ByVal mode As String,byval controltouse as control) As Boolean
        Dim i As gettype(controltouse)
        Dim command As String

        For Each i In controls
            
        Next
    End Function

Christiaan Baes
Belgium

"Time for a new sig." - Me
 
Was it the same we were discussing the other day?

________________________________________________________
Zameer Abdulla
Help to find Missing people
Even a thief takes ten years to learn his trade.
 
more or less.

Christiaan Baes
Belgium

"Time for a new sig." - Me
 
I forgot to paste the thread number thread796-1185266 already copied..

________________________________________________________
Zameer Abdulla
Help to find Missing people
Even a thief takes ten years to learn his trade.
 
Unfortunatly, that's not the problem I'm having. I have the function working, per say.

For example:
Code:
Public Function setMode(ByRef controls As Control.ControlCollection, ByVal mode As String) As Boolean
        Dim i As Control
        Dim command As String

        For Each i In controls
            If TypeOf i Is TextBox Then
                 Ctype(i, TextBox).Enabled = False
            End If
        Next
End Function

This works fine. However, it doesn't work how I want it to. I want to be able to pass to the function a different property instead of Enabled = False . Like say telling all of the form elements to to be red by calling setMode(grpDisplay.Controls, ForeColor, Color.Red)

If there is a function I'm missing that allows you to use a string as an object.property that'd make things easy. If there isn't, is there any way to code it in?
 
It is possible via reflection but I can't come up with the code right now, maybe tommorow. But you can do some googling in the mean time.

Christiaan Baes
Belgium

"Time for a new sig." - Me
 
Welp, I checked out Reflection and that does indeed seem to be what I need to make my dynamic property changing function work. However I've stumbled upon a snag. I've been getting a System.ArgumentException error with "Cannot widen target type to primitive type" as a message.

The following is the full function I've come up with:
Code:
Imports System.Reflection


Module ControlMods
    Public Function setParameters(ByRef controls As Control.ControlCollection, ByVal mode As String, ByVal value As Object) As Boolean
        Dim controlObject As Object
        Dim t As Type
        Dim list() As PropertyInfo
        Dim item As PropertyInfo


        For Each controlObject In controls
            If CType(controlObject, Control).Controls.Count > 0 Then
                setParameters(controlObject, mode, value)
            End If
            t = controlObject.GetType()
            list = t.GetProperties
            For Each item In list
                If item.Name = mode Then
                    item.SetValue(controlObject, value, Nothing)
                End If
            Next
        Next
    End Function
End Module

I get the error on item.SetValue(controlObject, value, Nothing) when I'm calling it with setParameters(Me.Controls, "Enabled", "False")

Any idea why it's giving me this error, and how I can fix it?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top