Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi all, newbie coder needs help please,
I have a combobox set up displaying images and text (works great) but for the life of me I can't change the class to pass in image paths, below is the class code, I would be eternally greatful if someone can adjust code to be useful rather than use hard coded paths.
Thanks in advance.

'-------------------------------------------------------------
VB
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Text

    Public Class ImageFactory
        
        Public Shared count As Integer = 0
    Private Shared _myactualdata As New Images()
        Public Shared ReadOnly Property MyImages() As Images
            Get
                Return _myactualdata
            End Get
        End Property
        Public Class Images
            Inherits ObservableCollection(Of ImageData)
        Public Sub New()
            ' this works but not much use to me as it is.
            Me.Add(New ImageData("Resources\Images\0.jpg"))
            Me.Add(New ImageData("Resources\Images\1.jpg"))
            Me.Add(New ImageData("Resources\Images\2.jpg"))
            Me.Add(New ImageData("Resources\Images\3.jpg"))
            Me.Add(New ImageData("Resources\Images\4.jpg"))
            Me.Add(New ImageData("Resources\Images\5.jpg"))
            Me.Add(New ImageData("Resources\Images\6.jpg"))
            Me.Add(New ImageData("Resources\Images\7.jpg"))
            Me.Add(New ImageData("c:\test\100_DSC_1727-s.jpg"))
        End Sub
       
    End Class


        Public Class ImageData
            Implements INotifyPropertyChanged
            '
            Private _sImageName As [String] = ""

            Public Sub New(ByVal sImageName As String)
                ImageFactory.count += 1
                ImageName = sImageName
            End Sub

            Public Property ImageName() As [String]
                Get
                    Return _sImageName
                End Get
                Set(ByVal value As [String])
                    _sImageName = value
                    NotifyPropertyChanged(ImageName)
                End Set
            End Property

            Protected Sub NotifyPropertyChanged(ByVal sProp As [String])
                RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(sProp))
            End Sub

            Public Event PropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
        End Class

    End Class
Posted
Comments
[no name] 30-Jul-12 21:17pm    
You might want to consider passing a Collection to your class for your strings.

1 solution

There are a number of minor mistakes in you code. One is that you created a custom notifycation when changing the properties and it wants a string, so do that:
VB
NotifyPropertyChanged("ImageName")


The only reason you need this class is the you want to know if a memeber gets an updated source, like a change in the Url etc. The ObservableCollection was made to fire an event when the collection gets updated, like adding an item and removing an item. It does however not fire an event when an item gets changed/updated.

And why did you make this class shared readonly?

VB
Public Shared ReadOnly Property MyImages() As Images


A shared list is usually not needed, as the same would be seen by all new instances of the new class.

To make make the code work according to your specs, you only need the ImageData class, An ObservableCollection and not the other code. You could simply bind the observable collection to any list, grid or whatever.
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900