|
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET). |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
July 9th, 2006, 10:41 PM
|
Registered User
|
|
Join Date: Jul 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Help with listbox
hi,this is my first time in the forums and i need help with a simple project i'm doin. below is a code snippet from my program, and that is to open the specific item in Listbox1 when i double click on it, but looks like my code is only opening the last added file, even when i double click on the first item in Listbox1.
In addition, is there anyway i can just display the filename in Listbox1 instead of the whole filepath?
i am a beginner in vb6 so, any help would be gr8ly appreciated.
(oh,n by the way, how do we post attachments here?)
[here's my code for Listbox1]
Private Sub List1_DblClick()
selectedfile = File1.Path & "\" & File1.FileName
ShellExecute 0, "open", selectedfile, "", "", SW_SHOWMAXIMIZED
End Sub
|
July 10th, 2006, 10:10 AM
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
It looks to me that you need to check where you assign the values into File1.Path and File1.Filename properties.
You are not setting those values in the code you show for the double-click of the listbox, so I am guessing that code exists in some other event of the listbox... or perhaps you are setting these properties in some other way not related to the events of the listbox.
Woody Z http://www.learntoprogramnow.com
|
July 10th, 2006, 03:47 PM
|
Registered User
|
|
Join Date: Jul 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
by the way, here's all my bits of code.in my form, i just have a DriveListbox(Drive1), a DirListbox(Dir1), a FileListbox(File1), and a Listbox(List1).whatever filename i choose from File1 gets displayed in the listbox where i should be able to open when i double click on that particular file!
* * * * * * * *
Option Explicit
Dim selectedfile As String
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
'This is needed to open any file, it declares what is going to get executed later on
Private Const SW_SHOW = 5
Private Const SW_SHOWMAXIMIZED = 3
'This sets the windowsize for what is getting opened
Private Sub cmdRemove_Click()
Dim i As Integer
i = 0
Do While i < List1.ListCount
If List1.Selected(i) Then
List1.RemoveItem i
Else
i = i + 1
End If
Loop
End Sub
Private Sub cmdClose_Click()
End
End Sub
Private Sub Dir1_Change()
File1.Path = Dir1.Path
'This will update the file path when you click on the directory
End Sub
Private Sub Drive1_Change()
Dir1.Path = Drive1
'This will update the drive path when you click on the drive letter
End Sub
Private Sub File1_Click()
selectedfile = File1.Path & "\" & File1.FileName
'This will update the variable selectedfile based on the drive letter, and file path
'ShellExecute 0, "open", selectedfile, "", "", SW_SHOWMAXIMIZED
'This will finally open the selected file with the appropriate program
List1.AddItem selectedfile
End Sub
Private Sub Form_Load()
Dir1.Path = "c:\"
'This sets the initial directory and drive paths, you can change this to whatever you want
End Sub
Private Sub Form_Unload(Cancel As Integer)
Unload Form1
'This will guarantee to close out anything that was opened during the program execution
End Sub
Private Sub List1_DblClick()
selectedfile = File1.Path & "\" & File1.FileName
'This will update the variable selectedfile based on the drive letter, and file path
ShellExecute 0, "open", selectedfile, "", "", SW_SHOWMAXIMIZED
'This will finally open the selected file with the appropriate program
End Sub
* * * * * * * *
|
July 11th, 2006, 07:19 PM
|
Registered User
|
|
Join Date: Jul 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
:)no worries, program working fine, thanks!
|
July 11th, 2006, 10:51 PM
|
Authorized User
|
|
Join Date: Sep 2003
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
You can not upload Attachments here; but certainly now a days there are many Free File Hosting services started around the Internet where you can upload your any file and they give you a download link to that file. Then come back to this forum and post that download link for us to get your attachment downloaded. I guess this is the only way-out, if you don't have your Website hosted on a Server of course!
|
July 11th, 2006, 10:56 PM
|
Authorized User
|
|
Join Date: Sep 2003
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Regarding FileList and getting currently selected file; you don't need to write so much of code on so many different events with variables! You can get the path with FileListbox.Path and get the selected file name by simply FileListbox.List(FileListbox.ListIndex) code line. So, write your Open File code on Double_Click event of FileListbox and get the complete file name with path as ...
Dim sFileToOpen as String
sFileToOpen = FileListbox.Path & "\" & FileListbox.List(FileListbox.ListIndex)
Then Shell or ShellExecute this file. Yeh, I know you mentioned that the program is working fine; I don't know by what method you have done your code ... see if you have implemented the method I suggested because optimized code is the only way to get your application work better!
|
|
|