vb.net:imagelist loses images when the image size property is changed

Discussion in 'ASP .Net' started by Peter Ho, Jul 25, 2003.

  1. Peter Ho

    Peter Ho Guest

    Hello,

    does anyone know why my imagelist loses its entire imagecollection when I
    change the imagesize property of the imagelist during run-time? This does
    not happen when I change the imagesize property during design time. Does
    anyone know a way to work around this problem?

    kind regards,

    Peter
    Peter Ho, Jul 25, 2003
    #1
    1. Advertising

  2. How are you changing it? Can you post some sample code which repros the problem?

    --

    Thanks,
    Carl Prothman
    Microsoft ASP.NET MVP
    http://www.able-consulting.com


    "Peter Ho" <> wrote in message news:bfrhmi$k7d$...
    > Hello,
    >
    > does anyone know why my imagelist loses its entire imagecollection when I
    > change the imagesize property of the imagelist during run-time? This does
    > not happen when I change the imagesize property during design time. Does
    > anyone know a way to work around this problem?
    >
    > kind regards,
    >
    > Peter
    >
    >
    Carl Prothman [MVP], Jul 25, 2003
    #2
    1. Advertising

  3. Peter,
    It appears that if you set the ImageList's ImageSize property and Images collection
    at design time, you won't be able to change the ImageSize without recreating the
    Images collection as well.

    I recommed you create a resource file, then add your images to that resource file,
    then read the images from the resource (which gets embedded in your assembly)
    at runtime.

    Here is are great article on how to do this:
    http://www.jelovic.com/articles/resources_in_visual_studio.htm
    - Note, once you add the images to the resource file (using Resourcer tool)
    make sure to *rename* them since the default name includes the path.

    Here is the VB.NET code that I used to resize the ImageList and add
    two images at runtime.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Me.ImageList1.ImageSize = New Size(16, 16) ' This will reset the Images collection

    ' Create a resource manager to retrieve images embedded in the assembly
    Dim resManager As New System.Resources.ResourceManager("ImageList.Form1", _
    System.Reflection.Assembly.GetExecutingAssembly())

    ' Add image1 to ImageList's Images collection
    Dim myImage As Bitmap = CType(resManager.GetObject("Image1.jpg"), Bitmap)
    Me.ImageList1.Images.Add(myImage)

    ' Add image2 to ImageList's Images collection
    myImage = CType(resManager.GetObject("Image2.jpg"), Bitmap)
    Me.ImageList1.Images.Add(myImage)
    End Sub

    --

    Thanks,
    Carl Prothman
    Microsoft ASP.NET MVP
    http://www.able-consulting.com


    "Peter Ho" <> wrote in message news:bfuc2n$d0u$...
    > here is some code that shows my problem, fill the imagelist with some images
    > during design time and then runt the code
    >
    > ****************************************************************************
    > **************************
    > ****************************************************************************
    > **************************
    > Public Class Form1
    >
    > Inherits System.Windows.Forms.Form
    >
    > #Region " Windows Form Designer generated code "
    >
    > Public Sub New()
    >
    > MyBase.New()
    >
    > 'This call is required by the Windows Form Designer.
    >
    > InitializeComponent()
    >
    > 'Add any initialization after the InitializeComponent() call
    >
    > End Sub
    >
    > 'Form overrides dispose to clean up the component list.
    >
    > Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    >
    > If disposing Then
    >
    > If Not (components Is Nothing) Then
    >
    > components.Dispose()
    >
    > End If
    >
    > End If
    >
    > MyBase.Dispose(disposing)
    >
    > End Sub
    >
    > 'Required by the Windows Form Designer
    >
    > Private components As System.ComponentModel.IContainer
    >
    > 'NOTE: The following procedure is required by the Windows Form Designer
    >
    > 'It can be modified using the Windows Form Designer.
    >
    > 'Do not modify it using the code editor.
    >
    > Friend WithEvents ImageList1 As System.Windows.Forms.ImageList
    >
    > Friend WithEvents Button5 As System.Windows.Forms.Button
    >
    > Friend WithEvents Button1 As System.Windows.Forms.Button
    >
    > <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    >
    > Me.components = New System.ComponentModel.Container
    >
    > Dim resources As System.Resources.ResourceManager = New
    > System.Resources.ResourceManager(GetType(Form1))
    >
    > Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)
    >
    > Me.Button5 = New System.Windows.Forms.Button
    >
    > Me.Button1 = New System.Windows.Forms.Button
    >
    > Me.SuspendLayout()
    >
    > '
    >
    > 'ImageList1
    >
    > '
    >
    > Me.ImageList1.ImageSize = New System.Drawing.Size(16, 16)
    >
    > Me.ImageList1.ImageStream =
    > CType(resources.GetObject("ImageList1.ImageStream"),
    > System.Windows.Forms.ImageListStreamer)
    >
    > Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent
    >
    > '
    >
    > 'Button5
    >
    > '
    >
    > Me.Button5.Location = New System.Drawing.Point(16, 16)
    >
    > Me.Button5.Name = "Button5"
    >
    > Me.Button5.Size = New System.Drawing.Size(192, 88)
    >
    > Me.Button5.TabIndex = 6
    >
    > Me.Button5.Text = "Count images in imagelist1"
    >
    > '
    >
    > 'Button1
    >
    > '
    >
    > Me.Button1.Location = New System.Drawing.Point(16, 120)
    >
    > Me.Button1.Name = "Button1"
    >
    > Me.Button1.Size = New System.Drawing.Size(240, 72)
    >
    > Me.Button1.TabIndex = 7
    >
    > Me.Button1.Text = "Imagelist1.imagesize=new size(20,20)"
    >
    > '
    >
    > 'Form1
    >
    > '
    >
    > Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    >
    > Me.ClientSize = New System.Drawing.Size(304, 251)
    >
    > Me.Controls.Add(Me.Button1)
    >
    > Me.Controls.Add(Me.Button5)
    >
    > Me.Name = "Form1"
    >
    > Me.Text = "Form1"
    >
    > Me.ResumeLayout(False)
    >
    > End Sub
    >
    > #End Region
    >
    >
    >
    > Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles Button5.Click
    >
    > MsgBox(CStr(Me.ImageList1.Images.Count))
    >
    > End Sub
    >
    > Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles Button1.Click
    >
    > Me.ImageList1.ImageSize = New Size(20, 20)
    >
    > End Sub
    >
    > End Class
    >
    > ****************************************************************************
    > ***************
    > ****************************************************************************
    > ***************
    >
    >
    > "Carl Prothman [MVP]" <> wrote in message
    > news:...
    > > How are you changing it? Can you post some sample code which repros the

    > problem?
    > >
    > > --
    > >
    > > Thanks,
    > > Carl Prothman
    > > Microsoft ASP.NET MVP
    > > http://www.able-consulting.com
    > >
    > >
    > > "Peter Ho" <> wrote in message

    > news:bfrhmi$k7d$...
    > > > Hello,
    > > >
    > > > does anyone know why my imagelist loses its entire imagecollection when

    > I
    > > > change the imagesize property of the imagelist during run-time? This

    > does
    > > > not happen when I change the imagesize property during design time. Does
    > > > anyone know a way to work around this problem?
    > > >
    > > > kind regards,
    > > >
    > > > Peter
    > > >
    > > >

    > >
    > >

    >
    >
    Carl Prothman [MVP], Jul 26, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Herr Lucifer
    Replies:
    0
    Views:
    536
    Herr Lucifer
    Jun 21, 2004
  2. Steve R.
    Replies:
    7
    Views:
    1,597
    Whitecrest
    Feb 17, 2004
  3. Martin Drautzburg
    Replies:
    2
    Views:
    374
    Paul McGuire
    Dec 20, 2004
  4. Chaos
    Replies:
    2
    Views:
    482
    Chaos
    Aug 13, 2006
  5. vj
    Replies:
    3
    Views:
    226
Loading...

Share This Page