|
You've got a couple of problems. The ListViewItems don't have the full path to the files because you never saved the full path in them. You need both the filename so you can show the user just that, the short filename, and you need the full path to the file so your code can get it easily. You're only doing one of those things.
The ListViewItem class has a Tag property that can store just about any type of object. In your code, it's probably a good idea to store the full path to the file in there so you can retrieve it later when you go to open the file.
Dim dirInfo As New DirectoryInfo(e.Node.FullPath)
If (dirInfo.Exists) Then
ListView1.Items.Clear()
Dim fileInfos As FileInfo() = dirInfo.GetFiles()
Dim info As FileInfo
For Each info In fileInfos
Dim item As New ListViewItem(info.Name)
item.SubItems.Add(info.LastAccessTime.ToString())
item.Tag = info.FullName
ListView1.Items.Add(item)
Next
End If
Now, your second problem. When you go and open the file in your doubleclick handler, you're actually creating a new instance of a ListViewItem, a blank one!, and using that to supply the filename your trying to launch. Obviously, you can't do that. You have to use the selected item in the ListView.
Private Sub ListView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
Try
System.Diagnostics.Process.Start(ListView1.SelectedItem.Tag)
Catch ex As Exception
MsgBox(Err.Description, vbCritical, "Error " & CStr(Err.Number))
End Try
End Sub
* Note: The code in this post has NOT been tested.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Gosh - go and sleep for 8 hours, and next thing you know, someone's taken over !!!!
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Well, now that you're awake.........
I tried Dave's code and I can't seem to get it to work.....
1st. not sure if its due to something else higher up in my code or just a miss-spelling but I can't use ListView1.SelectedItem.Tag, as I have to use ".SelectedItems.Tag" after that change I am unable to then use "Tag" as "Tag is not a memeber of System.Windows.Form.ListView.SelectedListViewItemCollection"
2nd. since I can't use Tag I looked at what .net says I can use and here are my options:
(Clear, Contains, CopyTo, Count, GetEnumerator, GetType, IndexOf, IsReadOnly, Item.)
Plus like I said last night my ultimate goal is to load this txt file to a RichTextBox placed next to the ListView, so I'm not so concerned with the System.Diag.... since that will open the file in its own program. I'm currently thinking of Streamreader as an option.....
Why is common sense such an un-common comodity?
Dim Beautiful As String
Beautiful = "ignorant"
Label1.Text = "The world is full of " & Beautiful & " people."
|
|
|
|
|
wetrivrrat wrote:
Well, now that you're awake.........
ROTFL - barely...
wetrivrrat wrote:
".SelectedItems.Tag" after that change I am unable to then use "Tag" as "Tag is not a memeber of System.Windows.Form.ListView.SelectedListViewItemCollection"
Gosh - you're getting a bit confused here. A SelectedListViewItemCollection contains SelectedListViewItems. I've just mocked up a VB.NET project ( I hate how you can't add events in the form designer, but.... ), here's my code:
Private Sub ListView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
Dim ListViewItem As New ListViewItem
Try
System.Diagnostics.Process.Start(ListView1.SelectedItems(0).Text)
Catch ex As Exception
' This is a BAD idea - catch only the exception that you foresee as being likely, in this case:
' System.IO.FileNotFoundException()
MsgBox(Err.Description, vbCritical, "Error " & CStr(Err.Number))
End Try
End Sub
Also, forget this Err.Number drivel, it's a holdover from VB6, isn't it ? It's sure a waste of time. You have an exception object which will give you a detailed error string and a call stack, so use it. The main reason I hate VB.NET is that they didn't deprecate all this stuff that VB6 users will keep using, although they shouldn't. While we're at it, don't use CStr, use .ToString, that's what it's for.
So if multiselect is turned off, there'll only be one selected item ( there has to be one, there was a double click event ). So you grab it using SelectedItems(0), and then you have the item, you can grab column values if needed, I just used .Text to get the text displayed on screen. Now, the only issue is if you're showing a filename and not a full path, you need to rebuild your path for the execute command to work.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
k...
so I'm glad that this has worked for you on your side... currently I must be looking at only the filename and not the fullpath as I get an error stating the file cant be found....multiselect is off and the only reason for the doubleclick is becuase I expect to have massive txt files that I don't want displaying everytime a user hovers over the filename(and I wanted to simulate the process for doubleclicking to open a filefolder,etc, as the expected audience won't understand the single click ).
Good catch on the SelectedItems(0) I removed that from an earlier sub becauase it was restricting me to only the first listing, and I forgot to put it here(was assumeing the same would happen... )
The confussion re: the "Tag" was because the editor didn't like the use of 'tag' and that was the error it gave in response to Dave's code
Currently the ListView shows the entire filepath as I was just showing filename, had the same probs having now and used fullpath to test....never changed it.
re: rebuilding paths for executing commands....I can't even get the node text to show in a textbox....I'm feeling like I'm missing the biggest part of my day, like I walked out side with no pants on or something.... why don't they have a "Brain Fart" icon smiley face?
Dim Beautiful As String
Beautiful = "ignorant"
Label1.Text = "The world is full of " & Beautiful & " people."
Why is common sense such an un-common comodity?
|
|
|
|
|
wetrivrrat wrote:
The confussion re: the "Tag" was because the editor didn't like the use of 'tag' and that was the error it gave in response to Dave's code
Yeah, I can't see a 'Tag' property, although I didn't look too closely, the (0) was obviously a major part of your problem, inserting it could well make Dave's code work.
wetrivrrat wrote:
re: rebuilding paths for executing commands....I can't even get the node text to show in a textbox....
How do you mean ? Is this a new question ?
If file names are unique and you don't want to show them, you can hide them in the listbox, or you can build a filename->filePath hashtable before you show the list.
wetrivrrat wrote:
I'm feeling like I'm missing the biggest part of my day, like I walked out side with no pants on or something....
LOL - I feel like that all the time....
wetrivrrat wrote:
why don't they have a "Brain Fart" icon smiley face?
Obscenity laws. :P
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
YES!!!! ok, so the problem was because I had mulitselect disable and was not telling the code to look for a path... insert dave's 'tag' and your '(0)' and I feel like I at least have found my shorts, even if they were in the MAILBOX! thanks so much!!!
k.. so, this fixed the 'tag' problem, and the node title in the textbox...(which wasn't a new question, but rather just a statement...an additon to my delima, sorta)...
again thanks
till next time.......
"To all the beautiful people...including myself...."
Dim Beautiful As String
Beautiful = "ignorant"
Label1.Text = "The world is full of " & Beautiful & " people."
Why is common sense such an un-common comodity?
|
|
|
|
|
No worries, glad to help.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
My bad. That's what happens when I try to write code from memory. Christian's right. The doubleclick handler should look like this:
Private Sub ListView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
Try
System.Diagnostics.Process.Start(ListView1.SelectedItems(0).Tag)
Catch ex As Exception
MsgBox(Err.Description, vbCritical, "Error " & CStr(Err.Number))
End Try
End Sub
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Yeah, and from the mistake I posted, it looks like I went to sleep right before I started writing the post!
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
LOL
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Hi I need to create program to connect my VB.NET to MS Outlook and import all messages to database (Access). Does anybody know how to do it. Thanks
|
|
|
|
|
Depends on your version of Outlook. If your using Office XP and above, check out the MS Office XP Primary InterOp Assembelies.
hope this helps.
|
|
|
|
|
I am trying to develop a new usercontrol that has a default image I need to load. I have an embedded bitmap and using the following code to retrieve it.
Dim executing_assembly As System.Reflection.Assembly = Me.GetType.Assembly.GetEntryAssembly()
' Get our namespace.
Dim my_namespace As String = executing_assembly.GetName().Name.ToString()
' Load three pictures.
Dim picture_stream As Stream
Dim bm As Bitmap
picture_stream = executing_assembly.GetManifestResourceStream(my_namespace + ".Checked.bmp")
If Not (picture_stream Is Nothing) Then
bm = New Bitmap(picture_stream)
picturebox.Image = bm
picture_stream.Close()
End If
When i reference my control(dll) and try to use it in another app. it does not work all i get is Object Reference not set to an instance of an object.
But when i use the code by its self in another project it works.
What it boils down to is how can i store/embed an image in a usercontrol dll.
Thanks in advance.
Mark Thibodeaux
|
|
|
|
|
tibmark wrote:
Dim my_namespace As String = executing_assembly.GetName().Name.ToString()
The executing assembly is not the same as the dll, if the code is in a dll. You'll need to get the name that's required for your dll, either by a different method, or by hard coding it.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
I was wrong it comes back null is not a valid vaue for stream. i am fairly new to the creation of usercontrols any examples would be very helpful.
Mark Thibodeaux
|
|
|
|
|
Usercontrols have nothing to do with it. You've obviously copy and pasted this code, do you have any idea what it does ? Resources are stored in your file and retrieved using a fully qualified name. You're building the name by asking what the name of the application that is running is, but that's not the name being used in your dll, which has a different name. Have a look at the string that's being generated, and then replace the application name with the dll name, that should work. I've never pulled resources from a dll before, but I'd imagine that would be the way forward.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Yes that is what i did, i couldnt find the rigght info just code. So let me ask the question the right way i would like to make a usercontrol that hosts a few bitmap images. what is the best way to do this or can you point me in the right direction. I am doing what i can to learn on limited resources and no real experience.
Mark Thibodeaux
|
|
|
|
|
Easiest approach - why not just load the files from disc ?
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
i'm not looking for the easy way out. i want the images to load when i use the control on any app.
Mark Thibodeaux
|
|
|
|
|
|
I think this will help. I understand what you were talking about. Thank you for your patience and help. I need all that I can get.
By the way any tips to gaining knowledge and experience would be very appreciated.
Thanks again.
Mark Thibodeaux
|
|
|
|
|
tibmark wrote:
Thank you for your patience and help.
To be honest, I was just thinking that I've been neither patient, nor helpful Normally, I'd whip up a project to work out the definitive answer, but I'm under a little schedule pressure today, and still trying to keep my batting average up.
tibmark wrote:
By the way any tips to gaining knowledge and experience would be very appreciated.
What's worked for me is to always learn why things do what they do, find a code sample and then use MSDN to nut it out, ask lots of questions in places like this one, and invest in good books, which I read at least twice. I nearly fell over today, I was away sick and some orders come in, I got two books, both over 800 pages, and one > 1000. I had no idea they were so big ( they are both ASP.NET, one on security and one on improving performance ). I've got a lot of reading to do....
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Don't know if 've understoog your application but why don't you use an Imagelist to store and load images. It's easy to access and use.
|
|
|
|
|
i want to make a custom checkbox that uses several predefined bitmaps. how and where do i store the images and call them. i want to be able to use in any project. i have come close but the project i want to use it in has to be add to the usercontrol solution or it will not work.
Mark Thibodeaux
|
|
|
|
|