?
Solved

Pass Imagelist to UserControl

Posted on 2005-03-09
10
Medium Priority
?
989 Views
Last Modified: 2012-08-14
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
Comment
Question by:broadbent
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13494541
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
 
LVL 4

Author Comment

by:broadbent
ID: 13498365
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
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13515212
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 4

Author Comment

by:broadbent
ID: 13515649
Thanks. I'll try it and get back to you.
0
 
LVL 4

Author Comment

by:broadbent
ID: 13521887
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
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 375 total points
ID: 13532953
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
 
LVL 4

Author Comment

by:broadbent
ID: 13545638
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
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13553257
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
 
LVL 4

Author Comment

by:broadbent
ID: 13554105
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
 
LVL 4

Author Comment

by:broadbent
ID: 13554119
How stupid, you're both the same person.
0

Featured Post

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question