Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

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

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