winnie Posted January 21, 2003 Posted January 21, 2003 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. Quote
*Experts* Nerseus Posted January 21, 2003 *Experts* Posted January 21, 2003 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 Quote "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
UCM Posted January 21, 2003 Posted January 21, 2003 (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 January 21, 2003 by UCM Quote UCM >-)
winnie Posted January 22, 2003 Author Posted January 22, 2003 i have a set list of images that allow user to choose as the background and i am using windows 2000 as the OS. Quote
UCM Posted January 22, 2003 Posted January 22, 2003 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 ;) Quote UCM >-)
winnie Posted January 22, 2003 Author Posted January 22, 2003 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 Quote
Creative2 Posted January 22, 2003 Posted January 22, 2003 I tried me.backgroundimage.fromfile ("C:\images\image1.jpg") but it didnt work, form doesnt show any image, anyone know why? Quote
*Experts* Nerseus Posted January 22, 2003 *Experts* Posted January 22, 2003 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 Quote "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
UCM Posted January 22, 2003 Posted January 22, 2003 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... Quote UCM >-)
Creative2 Posted January 22, 2003 Posted January 22, 2003 yeah me.BackgroundImage=image.fromfile("Location") works but me.BackgroundImage.fromfile("Location") doesnt. Thanx a lot guys. Quote
*Experts* Nerseus Posted January 23, 2003 *Experts* Posted January 23, 2003 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 Quote "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
*Gurus* divil Posted January 23, 2003 *Gurus* Posted January 23, 2003 But you _can_ call a static method on an instance :) Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
*Experts* Nerseus Posted January 23, 2003 *Experts* Posted January 23, 2003 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 Quote "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
*Gurus* divil Posted January 23, 2003 *Gurus* Posted January 23, 2003 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#. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.