Solved

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

Posted on 2004-03-29
4
500 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

Containers and Docker for Everyone

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

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

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 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…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

729 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