Solved

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

Posted on 2004-03-29
4
503 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

630 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