0

Here's what i have : a simple windows forms application with a PictureBox and a TrackBar. So here's what i want : i want to be able to put any jpeg or png in the resources and refer to them by the TrackBar1.Value without having to specify their names 1 by 1 in a string array.

The only way i see is to refer somehow to an array containing what's in the Resources.

See, i've came up with this code wich works fine :

Dim list() As String = {"2013-04-03 22.53.41", "2013-04-10 12.43.47", 
       "2013-05-24 01.44.00", "2013-05-25 11.49.51", "2013-05-25 16.37.10", 
       "2013-06-06 23.22.46", "2013-07-04 19.59.29", "2013-09-14 12.31.09", 
       "2013-11-20 20.28.07", "2014-01-03 15.30.21", "2014-01-24 20.12.16", 
       "2014-03-18 19.03.21", "2014-05-27 20.40.07", "2014-07-21 19.46.37", 
       "2014-08-05 14.05.09", "2014-09-01 17.41.46", "2014-09-01 22.13.08", 
       "2014-09-14 17.49.31", "2014-09-15 17.27.55", "2015-05-30 12.45.58"} 
      ' These are 19 iPhone pictures

Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll
    picbox.Image = CType(My.Resources.ResourceManager.GetObject(list(TrackBar1.Value)), Image)
End Sub

But that way, i have to manually type the names in a string array...

Thanks a lot.

1 Answers1

0

Something like this should work

Private _resourceNames As String()
Public ReadOnly Property ResourceNames As String()
    Get
        If _resourceNames Is Nothing Then
            Dim thisExe As System.Reflection.Assembly
            thisExe = Me.GetType.Assembly

            System.Reflection.Assembly.GetExecutingAssembly()
            _resourceNames = thisExe.GetManifestResourceNames()
        End If
        Return _resourceNames
    End Get

End Property
Miniver Cheevy
  • 1,667
  • 2
  • 14
  • 20
  • I get `Declaration expected` error msg at your line 2 – 1LandSurveyor Oct 02 '14 at 17:45
  • sorry, my vb is a little rusty, try that – Miniver Cheevy Oct 02 '14 at 17:55
  • i still get it... i dont understand why – 1LandSurveyor Oct 02 '14 at 17:57
  • 237th time is the charm I always say – Miniver Cheevy Oct 02 '14 at 18:03
  • so i tryed your code with `picbox.Image = CType(My.Resources.ResourceManager.GetObject(ResourceNames(TrackBar1.Value)), Image)` but it crashes as ResourceNames is not containing anything. well it contains something but its length is 2 and its supposed to be 20 – 1LandSurveyor Oct 02 '14 at 18:28
  • Are the resources in the same Assembly? Can you put a break point in the ResourceNames property and step throught it? – Miniver Cheevy Oct 02 '14 at 19:35
  • sorry, im not quite sure what an Assembly is... maybe you can be more specific on the test you want me to do and tell me what to verify. thank you – 1LandSurveyor Oct 02 '14 at 19:45
  • each project in your solution is going will be compiled into an assembly - so the question is are the resources in the same project that runs when you press play? Place a break point in on the line "If _resourceNames Is Nothing Then" and then run the app and step through the code that loads the resources. – Miniver Cheevy Oct 02 '14 at 19:48
  • ok well yes its all in the same project. i placed a break point on the line if.. and in the if statement but running the application, the code wasnt breaked. So i feel like it's not even reading this part of the code... I've copied your code directly in the Public Class, thats right? – 1LandSurveyor Oct 02 '14 at 20:15
  • that is unexpected, try putting a breakpoint in your form load and then right click on ResourceNames and choose quick watch, then drill down into it and see if there is anything there. – Miniver Cheevy Oct 02 '14 at 20:24
  • at the beginning and at the end of the if boucle, _resourceNames is Nothing and ResourceNames is length = 2 containing ResourceNames(0)=ProjectName.Resources.resources and ResourceNames(1)=ProjectName.Form1.resources – 1LandSurveyor Oct 02 '14 at 20:46
  • Using breakpoints, im really sure by now that the debugger doesnt go through the if condition. it breaks at the first line : `Private _resourceNames As String()` then i did put breakpoints to every other lines but the hit count stays 0, it doesnt break. – 1LandSurveyor Oct 02 '14 at 21:33