Home All Groups Group Topic Archive Search About

(newbie VB 2005 EE ) composing string from variables

Author
14 Sep 2006 6:33 PM
Opa Vito
Hello, I can't find out how to compose a string from a few variables
that can be used to define the location of a file.

For example:
PictureBox1.Image = System.Drawing.Image.FromFile("pictures\03.jpg")

I cannot find how to compose the string from the fact that the picture
is in folder "pictures" (all pictures will be in that folder) and that
the picture-file is a string-variable that can be randomly set by the user.

All help welcome
Thanks

Vito

Author
14 Sep 2006 6:47 PM
keith.thornhill
Dim FileName as String
FileName = "03.jpg"
PictureBox1.Image = System.Drawing.Image.FromFile("pictures\" &
FileName)

also, dont forget to use File.Exists() first to make sure the file they
specify exists.

-keith


Opa Vito wrote:
Show quoteHide quote
> Hello, I can't find out how to compose a string from a few variables
> that can be used to define the location of a file.
>
> For example:
> PictureBox1.Image = System.Drawing.Image.FromFile("pictures\03.jpg")
>
> I cannot find how to compose the string from the fact that the picture
> is in folder "pictures" (all pictures will be in that folder) and that
> the picture-file is a string-variable that can be randomly set by the user.
>
> All help welcome
> Thanks
>
> Vito
Author
14 Sep 2006 6:52 PM
rowe_newsgroups
Do you mean you want to concatenate two strings? If so try this

Dim PictureFile as string
PictureFile = "03.jpg"

PictureBox1.Image = System.Drawing.Image.FromFile("pictures\" &
PictureFile)

That will open the same picture as your below code. If that's not what
you meant please repost and I'll try again :-)

Thanks,

Seth Rowe

Opa Vito wrote:
Show quoteHide quote
> Hello, I can't find out how to compose a string from a few variables
> that can be used to define the location of a file.
>
> For example:
> PictureBox1.Image = System.Drawing.Image.FromFile("pictures\03.jpg")
>
> I cannot find how to compose the string from the fact that the picture
> is in folder "pictures" (all pictures will be in that folder) and that
> the picture-file is a string-variable that can be randomly set by the user.
>
> All help welcome
> Thanks
>
> Vito
Author
14 Sep 2006 6:54 PM
rowe_newsgroups
Oops, I guess keith types faster than I do!

rowe_newsgroups wrote:
Show quoteHide quote
> Do you mean you want to concatenate two strings? If so try this
>
> Dim PictureFile as string
> PictureFile = "03.jpg"
>
> PictureBox1.Image = System.Drawing.Image.FromFile("pictures\" &
> PictureFile)
>
> That will open the same picture as your below code. If that's not what
> you meant please repost and I'll try again :-)
>
> Thanks,
>
> Seth Rowe
>
> Opa Vito wrote:
> > Hello, I can't find out how to compose a string from a few variables
> > that can be used to define the location of a file.
> >
> > For example:
> > PictureBox1.Image = System.Drawing.Image.FromFile("pictures\03.jpg")
> >
> > I cannot find how to compose the string from the fact that the picture
> > is in folder "pictures" (all pictures will be in that folder) and that
> > the picture-file is a string-variable that can be randomly set by the user.
> >
> > All help welcome
> > Thanks
> >
> > Vito
Author
15 Sep 2006 8:37 AM
Opa Vito
rowe_newsgroups schreef:
> Do you mean you want to concatenate two strings? If so try this
>
> Dim PictureFile as string
> PictureFile = "03.jpg"
>
> PictureBox1.Image = System.Drawing.Image.FromFile("pictures\" &
> PictureFile)

Hi, strange but I tried that too ... and it always failed. I tried it
again by splitting the filestring and checking if it exists:

Dim PictureFile as String
PictureFile="pictures\" & pic  ' pic is the variable name
If File.Exists(PictureFile) Then
PictureBox1.Image = System.Drawing.Image.FromFile(PictureFile)
End If

And it works now :-)

Thanks for your cooperation

Vito
Author
15 Sep 2006 9:13 AM
Andrew Morton
Opa Vito wrote:
> Hello, I can't find out how to compose a string from a few variables
> that can be used to define the location of a file.
>
> For example:
> PictureBox1.Image = System.Drawing.Image.FromFile("pictures\03.jpg")
>
> I cannot find how to compose the string from the fact that the picture
> is in folder "pictures" (all pictures will be in that folder) and that
> the picture-file is a string-variable that can be randomly set by the
> user.

Do make sure that you give it the full path to the file.

Combining parts of a path is perhaps best done using System.IO.Path.Combine.

Andrew