Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-03-29
4
Medium Priority
?
504 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 1500 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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

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:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

721 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