|
-
Jun 28th, 2010, 12:54 AM
#1
Thread Starter
Lively Member
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...
-
Jun 28th, 2010, 12:59 AM
#2
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:
PictureBox1.Image = DirectCast(My.Resources.ResourceManager.GetObject(CStr(Me.ListBox1.SelectedItem)), Image)
-
Jun 28th, 2010, 01:15 AM
#3
Thread Starter
Lively Member
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?
-
Jun 28th, 2010, 01:38 AM
#4
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.
-
Jun 28th, 2010, 07:20 AM
#5
Thread Starter
Lively Member
Re: very simple question about using a resource
thank you very much for your great help! it is very clear now...
-
Jun 28th, 2010, 11:38 AM
#6
Thread Starter
Lively Member
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?
-
Jun 28th, 2010, 11:43 AM
#7
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
-
Jun 28th, 2010, 01:16 PM
#8
Thread Starter
Lively Member
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
-
Jun 29th, 2010, 04:50 AM
#9
Thread Starter
Lively Member
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?
-
Jun 29th, 2010, 09:47 AM
#10
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.
-
Jul 9th, 2010, 11:51 AM
#11
Thread Starter
Lively Member
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?
-
Jul 11th, 2010, 06:26 AM
#12
Thread Starter
Lively Member
Re: very simple question about using a resource
nobody does not have any idea?
-
Jul 11th, 2010, 06:49 AM
#13
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|