Click here to Skip to main content
15,909,645 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!

I made a picturebox, but in another class, not in the main, so I can't use Controls.Add, therefore I don't see it on my form.

Any ideas?
Posted

If you have a reference to your "other class", and the PictureBox object is public (or otherwise publicl retrievable), you can use Controls.Add.

 
Share this answer
 
Comments
wizardzz 7-Jan-11 10:15am    
Oh we got in a race there. Both posted 33 seconds ago.
Is the protection level of the PictureBox public? Other classes can only see objects that you allow them to.

[ADDED]

How about this.

public class players
{
    public PictureBox pb;

    public players()
    {
        this.pb = new PictureBox();
        this.pb.Image = Image.FromFile(@"D:\asd.jpg");
    }
}
public partial class Form1 : Form
{
    public Form1()
    {
            InitializeComponent();
            players p1 = new players();
            //Add control here. Controls.Add(p1.pb)
    }
}
 
Share this answer
 
v2
modify your code as given below

public class players  
{
public PictureBox pb;
    public players()
    {
         pb = new PictureBox();
        pb.Image = Image.FromFile(@"D:\asd.jpg");
        //There I would like to add the control
    }
}
public partial class Form1 : Form
{
players  p1  // creating object of class because of this you can access any where inside this class(Form1)
    public Form1()
    {
            InitializeComponent();
            p1 = new players();
    }
    private void Form1_Load(object sender, EventArgs e)
       {
       p1.pb.Controls.Add(yourcontrol);  // Control.Add is accessible
       
      }


it will enable you to access what do you want.
 
Share this answer
 
v3
Comments
velvet7 7-Jan-11 11:57am    
Worked. Thanks!
I don't really understand why you would want to add a PictureBox from another class into your Form. If the PictureBox is to be shown on the Form it should belong to the Form and not any other class.

In the example code, the Players class should raise an event passing the path to the image, and the Form1 class should subscribe to the event and create the PictureBox from the path in the arguments.

Something like this:
C#
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
public partial class Form1 : Form
{
    private Players players;
    public Form1()
    {
        InitializeComponent();
        players = new Players();
        players.NewPlayerImageRequest += new EventHandler<PathToImageEventArgs>(
            players_NewPlayerImageRequest);
        players.Initialize();
    }
    void players_NewPlayerImageRequest(object sender, PathToImageEventArgs e)
    {
        try
        {
            Image image = Image.FromFile(e.Path);
            PictureBox pictureBox = new PictureBox();
            pictureBox.Image = image;
            pictureBox.Size = image.Size;
            // Set other properties here
            Controls.Add(pictureBox);
        }
        catch (FileNotFoundException ex)
        {
            MessageBox.Show("The file " + ex.Message + " could not be found");
        }
        // catch other exceptions here
    }
}
public class Players
{
    public event EventHandler<PathToImageEventArgs> NewPlayerImageRequest;
    public const string DefaultImagePath = @"D:\asd.jpg";
    public void AddPlayerImage(string path)
    {
        OnNewPlayerImageRequest(new PathToImageEventArgs(path));
    }
    public void Initialize()
    {
        AddPlayerImage(DefaultImagePath);
    }
    protected virtual void OnNewPlayerImageRequest(PathToImageEventArgs e)
    {
        EventHandler<PathToImageEventArgs> eh = NewPlayerImageRequest;
        if (eh != null)
            eh(this, e);
    }
}
public class PathToImageEventArgs : EventArgs
{
    private string path;
    public PathToImageEventArgs(string path)
    {
        this.path = path;
    }
    public string Path
    {
        get { return path; }
    }
}
 
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