I want a label to display the path of a picture box's picture... How would I do that?
Printable View
I want a label to display the path of a picture box's picture... How would I do that?
I think the picture property returns a handle to the picture's memory address, but if you keep track of it's location you should be able to use something like this:
VB Code:
Public Function GetPath(ByVal FileName As String) As String Dim intFound As Integer intFound = InStrRev(FileName, "\") If intFound > 0 Then GetPath = Mid(FileName, 1, intFound) End Function
I haven't tested it but I think that's right.
But how do I use that?
Hey Rick Bull... the idea is alright. But you forgot to declare InStrRev, I ran a project using it and it asked for a declaration for it.
Besides I also believe you should have given an example of how to use the code. I tried:
Private Sub Form_Load()
Label1.Caption = GetPath(Picture1)
End Sub
All right to use it you could have something like this:
VB Code:
Dim strPictureFileName As String, strPicturePath As String strPictureFileName = "C:\Windows\Desktop\Picture.bmp" strPicturePath = GetPath(strPictureFileName) MsgBox(strPicturePath)
That should display "C:\Windows\Desktop". Obviously you will want to store the proper filename in strPictureFileName, not just "C:\Windows\Desktop\Picture.bmp".
And I didn't forget to declare InstrRev, it sounds like you have VB5 or eariler, you need 6. If you want the function I could probably work it out for you (if I can be bothered ;))
Nah... It is ok. :D I use VB4 indeed and VB5 CCE, I could upgrade to VB6 but I like the 4 better, I guess it is because of a certain something it has. Don't worry about giving the function, it can easily be declared by accessing the kernel32 but still, I think that if our friend did not have VB6 he would have had the same problem I did. Still, I consider your solution a good idea.
Just point out you need to know the location of the picture in order to display the path in the caption of the label, else if you are using the control (Like I did in the code I presented earlier) it will not show the source file you got the picture from.
I did say that in my first post ;)