Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

  • *Experts*
Posted

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

 

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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted (edited)

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:

  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:

  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:

  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?

Edited by UCM

UCM

>-)

Posted

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:

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 ;)

UCM

>-)

  • *Experts*
Posted

You'll want something like:

 

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:

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

 

-nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

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...

UCM

>-)

  • *Experts*
Posted

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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
  • *Experts*
Posted

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 want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...