[VS.NET] Bataille Navale en C# > Pb de tableau

Designotik_51 Messages postés 16 Date d'inscription vendredi 17 décembre 2004 Statut Membre Dernière intervention 7 janvier 2005 - 3 janv. 2005 à 16:36
cs_gulix Messages postés 25 Date d'inscription lundi 31 mars 2003 Statut Membre Dernière intervention 14 mars 2005 - 4 janv. 2005 à 17:31
Salut tout le monde

J'essaie de faire une bataille navale en graphique après l'avoir faite en programmation objet (mode console) mais je ne sais pas comment m'y prendre pour visualiser un tableau de picture box correspondant au tableau qui est dans mon code.

Si quelqu'un peut m'aider...

Merci

| Designotik |

5 réponses

Designotik_51 Messages postés 16 Date d'inscription vendredi 17 décembre 2004 Statut Membre Dernière intervention 7 janvier 2005
3 janv. 2005 à 16:44
Important : J'ai l'intention de créer un evenement :
> qd l'utilisateur cliquera sur une des cases, celle-ci restera enfonce ou changera de couleur jusqu'a ce que l'utilisateur choisisse a l'aide de boutons radio le type de bateau et la direction (horizontale ou verticale) puis appuie sur OK.

| Designotik |
0
cs_gulix Messages postés 25 Date d'inscription lundi 31 mars 2003 Statut Membre Dernière intervention 14 mars 2005
3 janv. 2005 à 20:48
J'ai fait un truc de ce genre pour un projet, mais avec des Button.

Voici ce que ça donnait ...



public class Principale : Form

{

const int TAILLE_CASE = 25;

public const int NB_LIGNE = 30;

public const int NB_COL = 30;

public Button[] cases = new Button[NB_LIGNE*NB_COL];



public Principale()
{
InitializeComponent(); }



private void InitializeComponent()

{

// Principale

this.ClientSize =
new
System.Drawing.Size(TAILLE_CASE*NB_COL+100,TAILLE_CASE*NB_LIGNE);

// Dessin des Cases

for (int j=0; j<NB_LIGNE; j++)

{

for (int i=0; i<NB_COL; i++)

{

int position = j*NB_LIGNE+i;

this.cases[position] = new Button();


this.cases[position].Location = new
System.Drawing.Point(TAILLE_CASE*i, TAILLE_CASE*j);


this.cases[position].Size = new
System.Drawing.Size(TAILLE_CASE, TAILLE_CASE);

this.cases[position].Text = " ";

this.cases[position].Name = position.ToString();


this.cases[position].Click += new
System.EventHandler(this.cmdCases_Click);

this.Controls.Add(cases[position] );

}

}

}



protected void cmdCases_Click(object sender, System.EventArgs e)

{

Button cmd = (Button) sender;

MessageBox.Show("C'est la case " + cmd.Name.ToString());

}



static void Main()

{

Principale appli = new Principale();

Application.Run(appli);

}

}



Quelques explications maintenant...

public Button[] cases = new Button[NB_LIGNE*NB_COL];

Là, je crée le tableau de Boutons





this.cases[position].Location = new System.Drawing.Point(TAILLE_CASE*i, TAILLE_CASE*j);
this.cases[position].Size = new System.Drawing.Size(TAILLE_CASE, TAILLE_CASE);

Ici, je dessine les cases



this.cases[position].Name = position.ToString();
Je donne un nom en rapport avec la position de la case, pour la retrouver plus facilement dasn l'événement



this.cases[position].Click += new System.EventHandler(this.cmdCases_Click);

Je déclare l'événement au gestionnaire



Button cmd = (Button) sender;

MessageBox.Show("C'est la case " + cmd.Name.ToString());

Et là, grace à ça, tu peux retrouver l'indice de ta case (ou PictureBox dasn ton cas). Tu fais :

int indice;

indice = System.Convert.ToInt32(((Button)sender).Name);



En espérant avoir été compréhensible...
0
Designotik_51 Messages postés 16 Date d'inscription vendredi 17 décembre 2004 Statut Membre Dernière intervention 7 janvier 2005
3 janv. 2005 à 22:03
Merci, j'ai essayé de faire ce que je voulais, j'arrive a créer un tableau de Picture Box au chargement du formulaire (load), à l'afficher, j'arrive aussi a changer la couleur d'une cellule (ds mon exemple : 2,1) qd je double-clik sur une picture box que j'ai ajouté en mode design (elle s'appelle pbox_test), mais par contre je n'arrive pas à créer ce même événement sur les cellules du tableau qui me permetterai de modifier la couleur de la case sur laquelle je double-clik.

Voici mon code :



using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;


namespace Bataille_navale_g
{



   public 
class Form1 : System.Windows.Forms.Form
   {
         
private System.Windows.Forms.PictureBox [,] tab;
         
private System.Windows.Forms.PictureBox pbox_test;



         private System.ComponentModel.Container components = 
null;



         public Form1()
         {
               
InitializeComponent();

         }

         
protected 
override 
void Dispose( 
bool disposing )
         {
               
if( disposing )
               {
                     
if (components != 
null) 
                     {
                           components.Dispose();
                     }
               }

         
base.Dispose( disposing );


          }


         #region Code g&#233;n&#233;r&#233; par le Concepteur Windows Form



         private 
void InitializeComponent()
         {
               System.Resources.ResourceManager resources = 
new System.Resources.ResourceManager  (
typeof(Form1));

               
this.pbox_test = 
new System.Windows.Forms.PictureBox();



               this.SuspendLayout();



// pbox_test



               this.pbox_test.BackColor = System.Drawing.Color.Red;
               
this.pbox_test.Location = 
new System.Drawing.Point(712, 16);
               
this.pbox_test.Name = "pbox_test";
               
this.pbox_test.Size = 
new System.Drawing.Size(32, 24);

               this.pbox_test.TabIndex = 0;
               
this.pbox_test.TabStop = 
false;
               
this.pbox_test.DoubleClick += 
new System.EventHandler(
this.pbox_test_DoubleClick);



//  Form1



               this.AutoScaleBaseSize = 
new System.Drawing.Size(5, 13);
               
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
               
this.ClientSize = 
new System.Drawing.Size(760, 494);
               
this.Controls.Add(
this.pbox_test);
               
this.Cursor = System.Windows.Forms.Cursors.Default;
               
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
               
this.MaximizeBox = 
false;
               
this.Name = "Form1";
               
this.Text = "Form1";
               
this.Load += 
new System.EventHandler(
this.Form1_Load);
               
this.ResumeLayout(
false);


          }


         #endregion



/// <summary>



/// Point d'entr&#233;e principal de l'application.



/// </summary>


         [STAThread]



         static 
void Main() 


         {


            Application.Run(
new Form1());


         }



         private 
void Form1_Load(
object sender, System.EventArgs e)


         {



               //Cr&#233;ation du tableau de picture Box



               this.tab = 
new System.Windows.Forms.PictureBox[9,9];



               for(
int j=0;j<8;j++)
               {
                        
int i;

                        
for(i=0;i<9;i++)
                        {
                                 
this.tab[i,j]=
new PictureBox();
                                 
this.tab[i,j].Size=
new System.Drawing.Size(40, 40);
                                 
this.tab[i,j].Location = 
new System.Drawing.Point(i*50+125, j*50+70);
                                 
this.tab[i,j].Name = "Tab";
                                 
this.tab[i,j].TabIndex = 0;
                                 
this.tab[i,j].BackColor = System.Drawing.Color.Green;
                                 
this.Controls.Add(
this.tab[i,j]); 
                        }


                        i=0;
               }
         
}



         private 
void pbox_test_DoubleClick(
object sender, System.EventArgs e)
         {
               pbox_test.BackColor=Color.Blue;
               tab[2,1].BackColor=Color.Blue;
         }



         private 
void tab_DoubleClick(
object sender, System.EventArgs e)
         {
                 pbox_test.BackColor=Color.Blue;
                 tab[0,0].BackColor=Color.Blue;
         }

     }
}



| Designotik |
0
Designotik_51 Messages postés 16 Date d'inscription vendredi 17 décembre 2004 Statut Membre Dernière intervention 7 janvier 2005
4 janv. 2005 à 01:01
Ca y g enfin trouvé !


J'ai bien étudié ton code (ce qui n'était pas une tache facile vu que c'etait pas totu a fait pareil)


et g reussi ce que je voulais.


Voici le code pour ceux qui en ont besoin.


using System;


using System.Drawing;


using System.Collections;


using System.ComponentModel;


using System.Windows.Forms;


using System.Data;


namespace Bataille_navale_g


{



/// <summary>



/// Description résumée de Form1.



/// </summary>



public
class Form1 : System.Windows.Forms.Form


{



private System.Windows.Forms.PictureBox [,] tab;



private System.Windows.Forms.PictureBox pbox_test;



/// <summary>



/// Variable nécessaire au concepteur.



/// </summary>



private System.ComponentModel.Container components =
null;



public Form1()


{



//



// Requis pour la prise en charge du Concepteur Windows Forms



//


InitializeComponent();



//



// TODO : ajoutez le code du constructeur après l'appel à InitializeComponent



//


}



/// <summary>



/// Nettoyage des ressources utilisées.



/// </summary>



protected
override
void Dispose(
bool disposing )


{



if( disposing )


{



if (components !=
null)


{


components.Dispose();


}


}



base.Dispose( disposing );


}


#region Code généré par le Concepteur Windows Form



/// <summary>



/// Méthode requise pour la prise en charge du concepteur - ne modifiez pas



/// le contenu de cette méthode avec l'éditeur de code.



/// </summary>



private
void InitializeComponent()


{


System.Resources.ResourceManager resources =
new System.Resources.ResourceManager(
typeof(Form1));





this.pbox_test =
new System.Windows.Forms.PictureBox();



this.SuspendLayout();



//



// pbox_test



//



this.pbox_test.BackColor = System.Drawing.Color.Red;



this.pbox_test.Location =
new System.Drawing.Point(712, 16);



this.pbox_test.Name = "pbox_test";



this.pbox_test.Size =
new System.Drawing.Size(32, 24);



this.pbox_test.TabIndex = 0;



this.pbox_test.TabStop =
false;



this.pbox_test.DoubleClick +=
new System.EventHandler(
this.pbox_test_DoubleClick);



//



// Form1



//



this.AutoScaleBaseSize =
new System.Drawing.Size(5, 13);



this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));



this.ClientSize =
new System.Drawing.Size(760, 494);



this.Controls.Add(
this.pbox_test);



this.Cursor = System.Windows.Forms.Cursors.Default;



this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;



this.MaximizeBox =
false;



this.Name = "Form1";



this.Text = "Form1";



this.Load +=
new System.EventHandler(
this.Form1_Load);



this.ResumeLayout(
false);


}


#endregion



/// <summary>



/// Point d'entrée principal de l'application.



/// </summary>


[STAThread]



static
void Main()


{


Form1 appli =
new Form1();


Application.Run(appli);




}



private
void Form1_Load(
object sender, System.EventArgs e)


{



//Création du tableau de picture Box







this.tab =
new System.Windows.Forms.PictureBox[9,9];



for(
int j=0;j<8;j++)


{



int i;





for(i=0;i<9;i++)


{



this.tab[i,j]=
new PictureBox();



this.tab[i,j].Size=
new System.Drawing.Size(40, 40);



this.tab[i,j].Location =
new System.Drawing.Point(i*50+125, j*50+70);



this.tab[i,j].Name = "tab["+i+","+j+"]";



this.tab[i,j].TabIndex = 0;



this.tab[i,j].BackColor = System.Drawing.Color.Green;



this.tab[i,j].Click +=
new System.EventHandler(
this.cmdTab_Click);



this.Controls.Add(
this.tab[i,j]);






}


i=0;


}






}



private
void pbox_test_DoubleClick(
object sender, System.EventArgs e)


{


pbox_test.BackColor=Color.Blue;


tab[2,1].BackColor=Color.Blue;


}



private
void cmdTab_Click(
object sender, System.EventArgs e)


{


pbox_test.BackColor=Color.Cyan;


PictureBox PB = (PictureBox)sender;




PB.BackColor=Color.Cyan;


}


}


}

| Designotik |
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
cs_gulix Messages postés 25 Date d'inscription lundi 31 mars 2003 Statut Membre Dernière intervention 14 mars 2005
4 janv. 2005 à 17:31
Content d'avoir pu aider



>>J'ai bien étudié ton code (ce qui n'était pas une tache facile vu que c'etait pas totu a fait pareil)
0
Rejoignez-nous