【问题标题】:How can i create a list of my resources so i can refer to an element using index? - VB.NET如何创建资源列表,以便可以使用索引引用元素? - VB.NET
【发布时间】:2014-10-02 17:19:58
【问题描述】:

这就是我所拥有的:一个带有 PictureBox 和 TrackBar 的简单 Windows 窗体应用程序。 所以这就是我想要的:我希望能够将任何 jpeg 或 png 放入资源中,并通过 TrackBar1.Value 引用它们,而不必在字符串数组中一一指定它们的名称。

我看到的唯一方法是以某种方式引用包含资源中内容的数组。

看,我想出了这段代码,效果很好:

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

但是那样,我必须手动在字符串数组中键入名称...

非常感谢。

【问题讨论】:

    标签: vb.net image list resources resourcemanager


    【解决方案1】:

    这样的东西应该可以工作

    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
    

    【讨论】:

    • 我在您的第 2 行收到 Declaration expected 错误消息
    • 抱歉,我的vb有点生锈了,试试看
    • 我还是明白了...我不明白为什么
    • 第237次是我一直说的魅力
    • 所以我用picbox.Image = CType(My.Resources.ResourceManager.GetObject(ResourceNames(TrackBar1.Value)), Image) 尝试了你的代码,但它崩溃了,因为 ResourceNames 不包含任何东西。好吧,它包含一些东西,但它的长度是 2,它应该是 20
    猜你喜欢
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-08
    • 1970-01-01
    相关资源
    最近更新 更多