Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1007
  • Last Modified:

Pass Imagelist to UserControl

I am writing a usercontrol to show an image, and I would like to store the image in an imagelist on its parent. I can do this programmatically at run time, but how do I do this at design time?

Ideally I would like 3 properties on my control:-
ImageList
Up Image
Down Image

The result would be to be able to see the Up Image at design time, and to store the control(s) on my parent form as well as the images.
0
broadbent
Asked:
broadbent
  • 6
  • 4
1 Solution
 
RonaldBiemansCommented:
Hi Broadbent

just add property to your usercontrol like

 private _myimagelist As ImageList

    Public Property myimage() As ImageList
        Get
            Return _myimagelist
        End Get
        Set(ByVal Value As ImageList)
            _myimagelist = Value
        End Set
    End Property

if you add a imagelist to your parent form, and you click on the usercontrol  there should be an myimage property, if you click on that it should give you the imagelist of the parent.

could you explain what you want to do with the up and downimage properties, should it just loop trough the imagelist and return an image ?
0
 
broadbentAuthor Commented:
I've done that, and you do see any imagelist on the parerntform but it doesn't perservere.

Even so, you haven't told me how to pass the image to the form at design time, justv as you might with a toolbar.
0
 
RonaldBiemansCommented:
you mean like this ?

private TheImagelist As ImageList
private _imageindex As Integer = -1

    Public Property myimage() As ImageList
        Get
            Return TheImageList
        End Get
        Set(ByVal Value As ImageList)
            TheImageList = Value
            ShowImage()
        End Set
    End Property
   
    Public Property ImageToUse() As Integer
        Get
            Return _imageindex
        End Get
        Set(ByVal Value As Integer)
            _imageindex = Value
            ShowImage()
        End Set
    End Property

    Public Sub ShowImage()
        If _imageindex <> -1 And Not IsNothing(TheImageList) AndAlso (_imageindex < TheImageList.Images.Count) Then
            Me.PictureBox1.Image = TheImageList.Images(_imageindex)
        End If
    End Sub
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
broadbentAuthor Commented:
Thanks. I'll try it and get back to you.
0
 
broadbentAuthor Commented:
I have a usercontrol which draws an image in either an up state or down state. Both images are read from an imagelist on its parent form. m_UpIndex and m_DownIndex represent the index +1 of the image in the imagelist If the value is 0, then there is no image, and the Up image is drawn instead of the down image. The following code does the work on the usercontrol.


  Dim m_ImageList As ImageList
 
  Public Property ImageList() As ImageList
    Get
      Return m_ImageList
    End Get
    Set(ByVal Value As ImageList)
      m_ImageList = Value
      Refresh()
    End Set
  End Property

  Public Property DownIndex() As Integer
    Get
      Return m_DownIndex
    End Get
    Set(ByVal Value As Integer)
      If m_ImageList Is Nothing Then Exit Property
      If Value <= m_ImageList.Images.Count And Value >= 0 Then m_DownIndex = Value
      Refresh()
    End Set
  End Property

  Public Property UpIndex() As Integer
    Get
      Return m_UpIndex
    End Get
    Set(ByVal Value As Integer)
      If m_ImageList Is Nothing Then Exit Property
      If Value <= m_ImageList.Images.Count And Value >= 0 Then m_UpIndex = Value
      Refresh()
    End Set
  End Property
 
  Private Sub ImageButton_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    If m_ImageList Is Nothing Or m_UpIndex = 0 Then Exit Sub
    If m_ImageList.Images(m_UpIndex - 1) Is Nothing Then Exit Sub
    Dim r As Rectangle
    Dim img As Image
    Dim g As Graphics = CreateGraphics()
    With g
      If m_Down Then  ' ie mouse is over
        If m_DownIndex > 0 Then
        img = m_ImageList.Images(m_DownIndex - 1)
      Else
          img = m_ImageList.Images(m_UpIndex - 1)
      End If
      Else
        img = m_ImageList.Images(m_UpIndex - 1)
      End If

m_Down is set by the MouseEnter or MouseLeave events, but can also be set as a property.

I can set these properties in the parent form, and setting the Down property does change the image. Clicking on another control does not create a problem. Going back to this control still shows the correct downindex.

However if I close the form in the VS editor and then reload it, the downindex resets to 0 although the upindex remains. Executing the form also does not work, unless I set the image control downindex property at run time ie in the form load event.

in the InitializeComponent() event however the downindex is still correct.

What is going on?
0
 
RonaldBiemansCommented:
Hi broadbent, This is something studip in .net, when you set the properties in design time they are added in alphabatical order, which means downindex is set before imagelist ,hence the property resets to 0, if you rename Imagelist to Aimagelist you will see that it works.
0
 
broadbentAuthor Commented:
Thanks for the last contribution.

My original question was to try to get the up and sown indexes to show a small bitmap of the image just as you do in VB on its standard controls.
0
 
RonaldBiemansCommented:
You can do this, instead of using an imagelist and indexes, just use an upimage and a downimage property

like

Dim m_upimage As Image
    Public Property upimage() As Image
        Get
            Return m_upimage
        End Get
        Set(ByVal Value As Image)
            m_upimage = Value
        End Set
    End Property
0
 
broadbentAuthor Commented:
The problem with this is that I haven't yet worked out how to do the equivalent of VB6 resource files and store the images in it. In any case the imagelist seems to be better in vb.net and it is a good way of storing them instead of file references.

Thanks

Neither of you answered the original question, but I found that Ronald's answer had a lot of use for me, and the first answer I knew any way.
0
 
broadbentAuthor Commented:
How stupid, you're both the same person.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now