mjohnson3091 Posted June 23, 2005 Posted June 23, 2005 As my dynamic form creation playing is getting further I seem to be getting more problems. I'm creating a form based upon a received XML message, which may or maynot contain a call to create a new PictureBox. If it does, I create the picturebox and all that works fine. The problem I have is, if I assign an event to a button, for example to save the image from the PictureBox, the code won't compile, because it tells me that the reference to the picturebox doesn't exist, which I suppose is true as it hasn't been created yet. How can I get around this? Example of my code... // This is called if we need to add a new picturebox void AddPicture() { PictureBox img1 = new PictureBox(); img1.Width=200; img1.Height=200; img1.Left=50; img1.Top=50; img1.BackColor=Color.White; this.Controls.Add(img1); img1.Visible=true; } // This is called to attempt to save the image void SavePicture() { ((PictureBox) img1).Image.Save("C:\\temp\\pic.bmp"); } I can understand why it fails, because at design time img1 doesn't exist, I just don't know how to solve it or get around it. Any help appreciated. Thanks in advance. Quote
Administrators PlausiblyDamp Posted June 23, 2005 Administrators Posted June 23, 2005 You could declare img outside of the AddPicture method and then the variable would be accessible from the SavePicture method, you would need to check for null before using it though. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mjohnson3091 Posted June 23, 2005 Author Posted June 23, 2005 You could declare img outside of the AddPicture method and then the variable would be accessible from the SavePicture method' date=' you would need to check for null before using it though.[/quote'] Works a treat, thanks again for your help mate. Quote
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.