Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello, I'm learning and I want to know the best way of doing this.

 

I have a NxM square of inherited pictureboxes dinamically created:

//in the form
public frmMain(){
  for(int i=0;i<N;i++){
   for(int j=0;j<M;j++){
    this.Controls.Add(new Cuadradito(i,j,number));
   }
  }
}

//the point class
public class Cuadradito:PictureBox{
  public Cuadradito(int xCoord, int yCoord, int num){
     state=num;
  }
  private void Cuadradito_MouseDown(object sender, MouseEventArgs e) {
     state=some_value;
  }

}

The number passed is the state of this point.

In the MouseDown event I change the state of this point and I need to change the states of the adjacent points.

Mi question is how can I do this. I have the N and M values of the points I need to change the state but the MouseDown event occurs only in the instance of "Cuadradito" clicked, so how do I tell the rest of the points to change states. I mean, there is no comunication between instances of this class. I can add an event, but I don't know if this is the best way to do it (an event that fires after the MouseDown, like ChangeState).

 

 

Thanks, hope you can help.

RonQ.

  • Leaders
Posted

With your current solution, you have no way of figuring out which blocks are adjacent to each other. It's like you are numbering the cuadraditos in order, but throwing them blindly into a bin. So, if you find (7,4), you aren't close to finding (7,3). :)

 

What I would do is create a 2-dimensional array of Cuadraditos

 

Cuadradito rejilla[][] = new Cuadradito[N][M] {};

// Declare a new 2D array.

 

then, inside of your loop, you can do something like

 

rejilla[j] = new Cuadradito(i, j, number);

this.Controls.Add(rejilla[j]);

 

And then, in the Cuadradito class, you can get the adjacent items like

state = some_value;

if (rejilla.GetUpperBound(1) > 0) {

rejilla[j - 1].state = 0; //or whatever you want to set the adjacent state to.

else if (rejilla.GetUpperBound(1) < N - 1) {

rejilla[j + 1].state = 0;

}

 

:)

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted (edited)

Thanks you very much for your help, it was easyer than I thought, with the rejilla array I can generate one event for all the cuadraditos and cast the sender to get it state and the adjacent states... thans again..

 

for(int i=0;i<ms.Y;i++){
  for(int j=0;j<ms.X;j++){
     rejilla[i,j]=new Cuadradito(i,j,ms.Tipo(i,j));
     this.Controls.Add(rejilla[i,j]);
     rejilla[i,j].MouseUp +=new MouseEventHandler(rejilla_MouseUp);
  }
}

 

Bye,

RonQ.

Edited by RonQ

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...