Results 1 to 13 of 13

Thread: very simple question about using a resource

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    93

    Exclamation very simple question about using a resource

    hi guys, I am sure that it is very easy but I could not manage it. my aim is to choose some items from a listbox and a picturebox will display the photo in resources with the same name of the item in list box. here is the code I wrote but I cannot make it work. anyone has an idea?

    PictureBox1.Image = My.Resources.Resource1.ListBox1.SelectedItem

    it gives a mistake telling that listbox1 is not a member of Resource1. I know this; but the result of Listbox1.selected item is a member of resource1...

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: very simple question about using a resource

    My.Resources.Resource1 is presumably an Image. It doesn't have a member named ListBox1. ListBox1 is a member of the form. If you want to use strings to get resources then you must use a ResourceManager, which you get from My.Resources. You use it to get an object by name, then cast it as the appropriate type:
    vb.net Code:
    1. PictureBox1.Image = DirectCast(My.Resources.ResourceManager.GetObject(CStr(Me.ListBox1.SelectedItem)), Image)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    93

    Re: very simple question about using a resource

    Thank you very much; it works now. But could you please tell me why you use "me" inside the code? What does it mean?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: very simple question about using a resource

    'Me' always refers to the current instance. It's not required but I like to be explicit.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    93

    Re: very simple question about using a resource

    thank you very much for your great help! it is very clear now...

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    93

    Re: very simple question about using a resource

    I thought that it worked but now I see that it is not. Now the code is not giving an error but picture1 is not showing the picture either... what could it be the reason?

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: very simple question about using a resource

    sorry... but my crystal ball is busted (it was one heck of a weekend)... do you mind posting your code as you currently have it?

    Thaaaaaanks.
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    93

    Re: very simple question about using a resource

    ah sure, please see it below. my listbox gets its content from a database table. My aim is when in the list box I choose "SMCOP 2120 BAL", picturebox1 should show the file with name "SMCOP 2120 BAL.jpg" that is available in my resources.

    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    Dim dtMontaj_Tipi As New AmetailorDataSet.Montaj_TipiDataTable
    Dim adapterMontaj_Tipi As New AmetailorDataSetTableAdapters.Montaj_TipiTableAdapter
    adapterMontaj_Tipi.Fill(dtMontaj_Tipi)


    ComboBox1.DataSource = dtMontaj_Tipi
    ComboBox1.DisplayMember = "Montaj Tipi"


    Dim dtBoy As New AmetailorDataSet.BoyDataTable
    Dim adapterBoy As New AmetailorDataSetTableAdapters.BoyTableAdapter
    adapterBoy.Fill(dtBoy)


    ComboBox2.DataSource = dtBoy
    ComboBox2.DisplayMember = "Boy"

    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    FindCombinations()
    End Sub

    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
    FindCombinations()
    End Sub

    Sub FindCombinations()
    Try
    ListBox1.Items.Clear()

    Dim selectedMontaj_Tipi As DataRowView = (ComboBox1.SelectedItem)
    Dim selectedBoy As DataRowView = (ComboBox2.SelectedItem)

    Dim ad As New AmetailorDataSetTableAdapters.Kabin_ButonyerleriTableAdapter
    Dim dtCombination As New AmetailorDataSet.Kabin_ButonyerleriDataTable
    ad.Fill(dtCombination)

    For Each dr As AmetailorDataSet.Kabin_ButonyerleriRow In dtCombination
    If dr.Boy = selectedBoy("Boy") And dr.Montaj_Tipi = selectedMontaj_Tipi("Montaj Tipi") Then
    ListBox1.Items.Add(dr.Model)
    End If
    Next
    Catch ex As Exception

    End Try


    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    PictureBox1.Image = My.Resources.ResourceManager.GetObject(CStr(ListBox1.SelectedItem))
    End Sub
    End Class

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    93

    Re: very simple question about using a resource

    is there a problem with the code or do you need any further information so that you can give me a hand?

  10. #10
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: very simple question about using a resource

    The problem I see is that you did not use all of the code given .

    Code:
    PictureBox1.Image = My.Resources.ResourceManager.GetObject(CStr(ListBox1.SelectedItem))
    Should be@
    Code:
    PictureBox1.Image = DirectCast(My.Resources.ResourceManager.GetObject(CStr(Me.ListBox1.SelectedItem)), Image)
    You are missing the directcast into an image. You should not be able to compile your application and it should tell you why if you have Option Explicit/Option Strict turned on, which no sane code should ever turned off for a project. If you do not have these turned on or have no idea what I am talking about just search for Option Explicit on the forums.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    93

    Re: very simple question about using a resource

    I made your correction but it still do not display the picture. It does not give an error either. Any comments?

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    93

    Re: very simple question about using a resource

    nobody does not have any idea?

  13. #13
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,158

    Re: very simple question about using a resource

    you must type the exact file name, its case sensetive

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width