Results 1 to 10 of 10

Thread: [RESOLVED] Dynamically set control property enum

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    137

    Resolved [RESOLVED] Dynamically set control property enum

    Hi, not sure if I'm going to explain this right but here goes

    I have a control, and one of the properties of the control is an enum (Basically lists available images)

    Now I'm trying to dynamically create the control on a form using info stored in a database. I was going to store the value for this image as a string but I cannot figure pout how to set this property using that string.

    Any ideas or how to do this better?

    Thanks for your help

  2. #2
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Music city, U.S.A.
    Posts
    1,253

    Re: Dynamically set control property enum

    How are you storing the images themselves? Are they part of the control's or project's resources, files on disk, in the database?
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Dynamically set control property enum

    Dont' store it as a string... store it as the number that it is...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Dynamically set control property enum

    It sounds like enum is the wrong word, or the wrong usage. If there are a set of available images, and the property can be set to one of those images, then it sounds more like a Dictionary(of String,Image) to me, where the string is the key that references a specific image. You could extend that with an enum by making it a Dictionary(of Integer, Image) and set up an Enum with the values having the integer values of specific dictionary keys, but that's probably not an ideal solution because it would make your set of images static. As far as I know, there is no way to dynamically add members to an Enum, so you'd be restricted to the set of values you put in the Enum at design time.

    Which approach ends up being better probably depends on how you intend to use this. If the purpose of the enum is to set the image at design time, then you will have a fixed set of images anyways, so an enum makes sense. If you want to be able to select images dynamically at runtime, and might want to have different sets of images available, then just using a dictionary would seem like a better option. You can always get the set of keys from the dictionary if you want to display them to the user such that they can select one, and you can make a property that takes a string and sets the image using that string as the key for the dictionary.
    My usual boring signature: Nothing

  5. #5
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,686

    Re: Dynamically set control property enum

    If by chance you are storing images as project resources take a look at the following article.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    137

    Re: Dynamically set control property enum

    Hi

    Just to clarify a little. The control is a bought in control that has these images predefined for the property.

    I'm trying to add this control dynamically to a form and set the relevant properties based on the info from a database. To set the image its

    Control.image = controlimage.imagename

    I did try just setting it to a relevant integer but that didn't seem to work as it only accepted the above format. (I need to recheck that though)

    I was hoping to pull the available options for this property into a list and then match up against the string in the dB but it looks.like that might not be possible.

    Your probably right that its not an enum but I can't think of another way of explaining it.

  7. #7
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Music city, U.S.A.
    Posts
    1,253

    Re: Dynamically set control property enum

    Sounds like you might need just a good ol' fashioned function with a Select Case statement to translate the integer to the appropriate value.
    Code:
    Private Function GetImage(ByVal ImageIndex as Integer) as Object
    
         Select Case 1
              Return ControlImage.ImageName1
         Select Case 2
              ... etc ...
    
    end Function
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    137

    Re: Dynamically set control property enum

    Yeh I was hoping there was a better way but its looking like that might be the answer.

    I'm going to check with the vender to see if they have a way to expose them.

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Dynamically set control property enum

    There is a better way, if ControlImage is the name of an enum. An enum pretty much is an integer, but you can't just use an integer in place of an enum. You can convert it, though:

    Control.Image = Ctype(yourNumberHere, ControlImage)

    That should work as long as the number you supply is actually one of the values for the enum, which it sounds like it will be, since you get the integer from the enum in the first place.
    My usual boring signature: Nothing

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    137

    Resolved Re: Dynamically set control property enum

    Nice one Shaggy

    I have now sorted this.

    I really wanted to avoid creating a giant select case statement so I have created the following sub

    Code:
    ImageList = New List(Of ImageItem)
            For i As Integer = 1 To 500
                Try
                    Dim TImage As ControlImage = CType(i, controlImage)
                    If TImage .ToString <> CStr(TImage ) Then
                        Dim img As New ControlImageitem
                        img .Name = TImage.ToString
                        img .Index = TImage 
                        ImageList .Add(img)
                    End If
                Catch ex As Exception
    
                End Try
            Next
    this basically cycles through all available images and populates a list with its index and name

    which I can then use to lookup against later

    Thanks for all the help

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