windows form background image

winnie

Newcomer
Joined
Nov 27, 2002
Messages
12
hi. i am using vb.net.
how can i change the form background image when the user click on a certain link?

for example, i am using image a as the form background. when the user click on change image b link, the form background will change to image b while all the other functions still remain.

any idea anyone?

thank you.
 
Set the Form's BackgroundImage property to a new Bitmap, using something like:

Code:
this.BackgroundImage = new Bitmap(@"c:\temp\newimage.bmp");

I think you can also use the Image.FromFile() method. I like Bitmaps, especially if you want to use transparency.

-Nerseus
 
umm...Nerseus?...I think that's c# or c++, Winnie is using vb.net...

Note that there are several ways of doing it...

Try this one:
Visual Basic:
  Private Sub ChangeImagebLink_LinkClicked(blahh) Handles ChangeImagebLink.LinkClicked
    'If you have Win XP:
    Me.BackgroundImage.FromFile("c:\windows\Coffee Bean.bmp")
    'If you have Win 98 or Win98se:
    Me.BackgroundImage.FromFile("c:\windows\clouds.bmp")
  End Sub

The only thing with this code is that every time the link is clicked, the image will be loaded from the hard drive...It would be best to use an imagelist control as it would load all images into memory first and then you could change the image accordingly with:
Visual Basic:
  Private Sub ChangeImagebLink_LinkClicked(blahhhh) Handles ChangeImagebLink.LinkClicked
    Me.BackgroundImage = Me.ImageList1.Images.Item(0)
  End Sub
where the .Item(0) would be the index of the image you want to set the backgound of the form to from the imagelist control...If you want image number 5 to be the next image them use:
Visual Basic:
  Private Sub ChangeImagebLink_LinkClicked(blahhhh) Handles ChangeImagebLink.LinkClicked
    Me.BackgroundImage = Me.ImageList1.Images.Item(5)
  End Sub
etc etc...

Are you building an application that will allow the user to specify what image to load as the background image or do you have a set list of images that you want to change the background to?

what os r u using? xp or 98?
 
Last edited:
i have a set list of images that allow user to choose as the background and i am using windows 2000 as the OS.
 
Niiiice...

Ok then...

NOTE: don't get upset if you already know half of what I say, I'm making this so that other readers can get it too, thanx...


create an imagelist ( ImageList1 )...

in the properties window for the ImageList1 object, there is a property called ' Images ', this is a collection of images that YOU specify at design time ( you can also add at run time, but lets do design time for now to make it quick and easy )

Click on the 3 dots ( ... ) button in that property window and another window will open that will let you specify the images you want to put in by clicking on the ( Add ) button in the lower left corner...Every image you add will have it's own index value starting with zero ( 0 )....

then when the user clicks on your button to select an image, have you code do:
Visual Basic:
Me.BackgroundImage = Me.ImageList1.Images.Item(5)
where Item(5) is the index of the image that your user wants to display....

If you need, I'll whip up an example project for yas ;)
 
thanks a lot. i can get it now.
i really appreciate u give the details on how to do it.
it helps a lot.
thanks again :p
 
You'll want something like:

Visual Basic:
Me.BackgroundImage = Image.FromFile("C:\images\image1.jpg")

I don't know if VB needs to double up backslashes as well, which might mean use something like:
Visual Basic:
Me.BackgroundImage = Image.FromFile("C:\\images\\image1.jpg")

-nerseus
 
is there a file image1.jpg in the C:\images\ directory?...is the image really a jpg or does it merely have the .jpg exstension?

try using windows explorer and go to the C:\images\ directory then dbl-click the image1.jpg file...see if it opens up or if you get an err...
 
I would think that:
me.BackgroundImage.fromfile("Location")
wouldn't event compiles since FromFile is a static method and can't be called on an instance.

If you don't, make sure you have Option Explicit set (not sure where in VB but I know it's possible). If it is set and you couldn't even get the line to compile, make sure you mention it as well as any errors that are generated when it *does* run. They help more than "the image doesn't show up".

-Nerseus
 
Is that a VB.NET feature? This isn't possible in C#, you'll get:
Static member 'member' cannot be accessed with an instance reference; qualify it with a type name instead

-Nerseus
 
I guess it must just be a VB.NET thing. Nice to know the difference though, I didn't know you couldn't do that in C#.
 
Back
Top