Solved

Problem setting an Image in a control inhereted from PictureBox (C# & Windows Forms)

Posted on 2004-03-29
4
498 Views
Last Modified: 2010-05-18
Hi there.... Please give me a hand with this code, because I'm sure I'm missing something.

I've made a control that inherits from PictureBox. This is the code

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace Quattropoli.UI
{
      public class Casilla : System.Windows.Forms.PictureBox
      {
            private System.ComponentModel.Container components = null;
            private short _fila;
            private short _columna;
            public short Fila
            {
                  get
                  {
                        return _fila;
                  }
                  set
                  {
                        _fila = value;
                  }
            }
            public short Columna
            {
                  get
                  {
                        return _columna;
                  }
                  set
                  {
                        _columna = value;
                  }
            }
            public Casilla()
            {
                  InitializeComponent();
            }
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if(components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            #region Component Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  components = new System.ComponentModel.Container();
            }
            #endregion
            public void ColocarFicha(Juego.Ficha ficha)
            {
                  System.Reflection.Assembly myAssembly = this.GetType().Assembly;
                  System.Resources.ResourceManager myManager = new System.Resources.ResourceManager("Quattropoli.imagenes",myAssembly);
                  switch(ficha.Tipo)
                  {
                        case Juego.Ficha.TipoFicha.Azul:
                              this.Image = (Image)myManager.GetObject("Azul");
                              break;
                        case Juego.Ficha.TipoFicha.Roja:
                              this.Image = (Image)myManager.GetObject("Rojo");
                              break;
                  }
            }
      }
}

Then I have a user control that should build a matrix of Casilla Controls. here's the code:

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Diagnostics;

namespace Quattropoli.UI
{
      public class Tablero : System.Windows.Forms.UserControl
      {
            private System.ComponentModel.Container components = null;
            private Casilla[,] _casillas = new Casilla[6,7];
            public Tablero()
            {
                  InitializeComponent();
                  for(short i=0; i<6; i++)
                  {
                        for(short j=0; j < 7; j++)
                        {
                              ConfigurarCasilla(i,j);
                              Controls.Add(_casillas[i,j]);
                              Controls[Controls.Count-1].Click+=new EventHandler(Tablero_Click);
                        }
                  }            
            }
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if(components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            #region Component Designer generated code
            private void InitializeComponent()
            {
                  //
                  // Tablero
                  //
                  this.Name = "Tablero";
                  this.Size = new System.Drawing.Size(280, 240);
                  this.Load += new System.EventHandler(this.Tablero_Load);

            }
            #endregion

            private void Tablero_Load(object sender, System.EventArgs e)
            {

            }
            private void ConfigurarCasilla(short fila, short columna)
            {
                  _casillas[fila,columna] = new Casilla();
                  _casillas[fila,columna].Fila = fila;
                  _casillas[fila,columna].Columna = columna;
                  _casillas[fila,columna].BackColor = Color.White;
                  _casillas[fila,columna].BorderStyle = BorderStyle.FixedSingle;
                  _casillas[fila,columna].Top = fila * 40;
                  _casillas[fila,columna].Left = columna * 40;
                  _casillas[fila,columna].Width=40;
                  _casillas[fila,columna].Height=40;
            }

            private void Tablero_Click(object sender, EventArgs e)
            {
                  ((Casilla)sender).ColocarFicha(new Juego.Ficha(Juego.Ficha.TipoFicha.Azul));
            }
      }
}

At this point everything works fine. If i click over some "Casilla" inside the "Tablero" control, I get my image loaded and displayed. Now i want to programaticly change the image of some "Casilla", so i added this method to "Tablero" class

            public void jugar(short i, short j, Juego.Ficha ficha)
            {
                  _casillas[i,j].ColocarFicha(ficha);
            }

now, this method doesn't work. I mean, I call it from the main form and get nothing!. The weird stuff is that if I debug, after i call _casillas[i,j].ColocarFicha(ficha); the image is actually set in the array and the Controls Collection, but it doesn't get displayed. Any ideas?.. thanks
0
Comment
Question by:esteban_felipe
4 Comments
 
LVL 12

Accepted Solution

by:
farsight earned 500 total points
ID: 10712224
(1) I'm guessing:  Add this
               _casillas[i,j].Refresh()
at the end of jugar() or
                this.Refresh()
at the end of Casillas.ColocarFicha()

(2) [off-topic]
I also wonder why you did this:
public class Casilla : System.Windows.Forms.PictureBox
     {
          private System.ComponentModel.Container components = null;
          // ...
     }
Why aren't you just using:
     this.container
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question