Click here to Skip to main content
15,916,091 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have dynamically created a tab control in which i add a tab page and in the tab page i add a picture box

how can i show a image on this picture box
at Click of a button

the error i get is

The name 'picturebox1' does not exist in the current context

i know to show picture on to picturebox but here i m stucked at how can i set image for a dynamically created picture box
Posted
Comments
Neema Derakhshan 14-Sep-13 2:42am    
what is your code ?
Code Mirror 14-Sep-13 3:15am    
PictureBox picturebox1= new PictureBox();
picturebox1.Name = "picturebox1";
picturebox1.Location =new Point(0, 0);
picturebox1.Size = new Size(817, 447);
Controls.Add(picturebox1);
now when i do
picturebox1.Image = //code to show image
i get error : The name 'picturebox1' does not exist in the current context
Neema Derakhshan 14-Sep-13 3:53am    
why don't you set the Image property before adding picturebox1 to controls?
Code Mirror 14-Sep-13 3:59am    
because i want to set images at run time on cliack of a button , but the problem is i can't see this picturebox1 in
private void button1_Click(Object sender, EventArgs e)
{
picturebox1.Image=//here i can't set picturebox1 's image
}
Ganesh KP 14-Sep-13 5:10am    
Where are you declaring your picturebox1? I think the object is not int the scope of your class. try to create as public and do.

You have to make for your form a member first:
private PictureBox picturebox1;


In the form constructor you can set the properties of the picturebox1 object. After this in the button click event you can use your PictureBox object without problem.

By the way you can iterate through the controls with foreach. Maybe you can find your dynamically created control as the follows:
foreach (var i in this.Controls)
{
    if (i.GetType() == typeof(PictureBox))
    {
        Image p = i as Image;

        p.Image = //your code
    }
}


I hope I could help you!
 
Share this answer
 
v2
this is the designer
namespace WindowsFormsApplication6
{
partial class Form1
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code

private void InitializeComponent()
{
this.buttoncreatepicturebox = new System.Windows.Forms.Button();
this.buttonshowimage = new System.Windows.Forms.Button();
this.SuspendLayout();

// buttoncreatepicturebox

// buttonshowimage

// Form1

}
#endregion
private System.Windows.Forms.Button buttoncreatepicturebox;
private System.Windows.Forms.Button buttonshowimage;
private System.Windows.Forms.PictureBox picturebox1; // i wrote this manually
//We have to make for our form a member first
}
}


and

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
picturebox1 = new PictureBox();

Controls.Add(picturebox1);

picturebox1.Name = "picturebox1";
picturebox1.Location = new Point(0, 0);
picturebox1.Size = new Size(100, 100);
picturebox1.SizeMode = PictureBoxSizeMode.CenterImage;
picturebox1.BackColor = Color.AliceBlue;
}
private void button2_Click(object sender, EventArgs e)
{ picturebox1.Image=indowsFormsApplication6.Properties.Resources.monotone_close_exit_delete;
}
}
}
hope this helps some newbie like me!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900