Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Beginner
Oct 1, 1996



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Selecting File from File List Box

I put a drive box, a directory list box, and a file list box on the form, with the intention of being able to select an *.ico file from the file list, and have it display the relevant icon in a picture box which I also had on the form. Please help before I go bald !!!

Here's the sample code that demonstrates this. On your form put a DriveBox, DirBox, FileListBox and Picture control. Make sure to set the AutoSize property on the Picture control to True and the Pattern property on your FileListBox to *.ico.

'the first two subs just  keep the DriveBox, DirBox and FileList box synced.
Private Sub Dir1_Change()
File1.Path = Dir1.Path
End Sub

Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub

'this is the sub that will display the icon in the picture control when double-clicked
Private Sub File1_DblClick()
Dim pic As String

pic = Dir1.Path       'this puts the path where the icon file is into the pic variable
pic = FixPath$(pic) 'FixPath$ code is below.  It simply adds a trailing backslash if needed.
pic = pic & File1.filename    'this adds the icon's filename to the end of the pic variable
Picture1.Picture = LoadPicture(pic)  'and this loads it into your picture control

End Sub

'copy and paste this into your form's General/Declarations section
Function FixPath$(Test$)
'sticks a backslash on the end of test$ if there's not one there already
If Right$(Test$, 1) <> "\" Then Test$ = Test$ + "\"
FixPath$ = Test$
End Function
This code should work equally well with VB3.0 and VB4.0.
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date