Solved

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

Posted on 2004-03-29
4
493 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
Comment Utility
(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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now