Simple Problem combobox selected

kurt69

Well-known member
Joined
Jan 17, 2006
Messages
78
Location
Australia
Programming Experience
Beginner
Hey what am I doing wrong here, I have a picturebox with a dropdown list above it, I want it so when an item in the dropdown list is selected it loads the specified image in the picture box.

comboChar is the dropdown listbox and charChoice is the picturebox.

VB.NET:
Private Sub comboChar_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles comboChar.SelectedIndexChanged
        charPortrait = comboChar.SelectedItem
        charChoice.Image.FromFile(Application.StartupPath & "\resources\images\portraits\" & charPortrait & ".bmp")
    End Sub
Also how do I preload external images and sound? Currently I just have the sound file play and instantly stop at the splash screen, didn't know what to do with the images though.
 
do you actually have the bmp files with numeric names like that? and are those files actually in the "Application.StartupPath & "\resources\images\portraits\" dir?
 
Check what value the system return you first from below:
(Application.StartupPath & "\resources\images\portraits\" & charPortrait & ".bmp")

make sure the path and file is correct and availble!
 
JuggaloBrotha, I aint totally sure what you mean (I'm learning as I go with VB.NET) but you mean since it's "SelectedItem" it will be some numeric value not the actual text that I have specified in the collection? If so no.

I have all the files in that path, example names: Bat.bmp, Deep.bmp and for everyone of them. In the collections I have the "Bat" and deep and etc. Case doesn't matter right? IE: If the file is bat.bmp and I'm trying to load Bat.bmp.
 
Application.StartupPath & "\resources\images\portraits\" & charPortrait & ".bmp"

Above code return what to you? Can you post it out?
you can use message box to display or when running debug mode.

MessageBox.Show(
Application.StartupPath & "\resources\images\portraits\" & charPortrait & ".bmp")
 
It only returns "". But that is the problem, I tried charPotrait = comboChar.SelectedText and I also get "". What will return the actual text that is selected in the combo box? By that I mean what is selected in the list. Example: The user might have the 3rd item selected which in the collections is "Bat". I want that to return "Bat"
 
I think this may be a case of 'It hasn't done it yet' What i mean is that i believe selected index changed event occurs almost instantly which means that the selected item property won't be populated yet.
 
I swapped it over too a listbox before you posted and it still doesnt work. That setbounds thing will that increase my form loading quite a bit if I apply it to every control?

Heres the new code btw (listbox):

VB.NET:
[SIZE=2][COLOR=#0000ff]
Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] lstChar_SelectedIndexChanged([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] lstChar.SelectedIndexChanged
charPortrait = lstChar.SelectedItem
charChoice.Image.FromFile(Application.StartupPath & "\resources\images\portraits\" & charPortrait & ".bmp")
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

lstChar = Listbox
charChoice = Picturebox

It still wont change the picturebox's image, if I convert it to a bitmap will that help?



EDIT :: I just added this for testing reasons,
VB.NET:
MsgBox(charPortrait)
and it worked perfectly, popped up saying the text in the selected item of the listbox. So it has to be something to do with the picturebox.
 
Have you set a breakpoint to see if charportrait equals something after that line executes? I take it charprotrait is a string variable so you should really be converting the listbox item to a string when you use it as everything in a listbox is an object...

VB.NET:
charportrait = convert.tostring(lstchar.selecteditem)

Yes it will increase the load time as it will only have to call one method to set the location and size of the control. Another thing you can do is where the designer generated code adds the controls to it's collection, you can take that bit out and just use the parent property instead. So rather than..

VB.NET:
Me.controls.add(control)
it could be ..

VB.NET:
control.parent(me)
Which is also faster than the default windows way. One thing to bear in mind though. Only do this when your app is ready for realease as a subsequent build will change everything back to how it was.
 
Ok it still isn't working, heres what I got out of the breakpoint.
+ charPortrait : Nothing : String
+ lstChar.SelectedItem : "Bat" {String} : Object

charPortrait was nothing but once i clicked something else in the listbox it changed to "Deep".

Thanks for the tips about form load optimization too.
 
Mind you, the FromFile method of Image class is not a sub method, it is a shared function method that returns an image if the load operation succeed, this return you want to assign the Image property of the Picturebox.
VB.NET:
charChoice.Image = Image.FromFile("the.bmp")
 
Thanks john that works *perfectly*, I obviously still have a lot too learn :(

Umm how do I go about preloading images and sound into memory while my splash screen form loads? Once I do this do I need to reference to the memory or still reference to the imagefile location? I've never messed with this stuff.
 
Back
Top