1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace GrilleCSharp
{
public partial class UserControlLbl : UserControl
{
// carreau à dessiner
private Rectangle[,] RC;
public UserControlLbl()
{
InitializeComponent();
this.Size = new Size(255, 332);
this.ResizeRedraw = true;
this.NbLignes = 10;
this.NbColonnes = 5;
this.CreeTabGrille();
}
private void UserControlLbl_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.Clear(Color.White);
//dessin de la grille
Pen penRect = new Pen(this.ForeColor, 1.0f);
Brush brushText = new SolidBrush(this.ForeColor);
//largeur et hauteur du carreau(regle des intervalles)
int largRC = this.Width / (NbColonnes- 1);
int hautRC = this.Height / (NbLignes - 1);
Rectangle myRect;
int li, col;
int origX=0;
int origY=0;
for (li = 0; li < this.NbLignes-1; li++)
{
origY = li * hautRC;
for (col = 0; col < this.NbColonnes-1; col++)
{
myRect= RC[li, col] ;
origX = col * largRC;
myRect.Location = new Point(origX, origY);
myRect.Size = new Size(largRC, hautRC);
g.DrawRectangle(penRect , myRect);
g.DrawString(this.strScore[li, col], this.Font, brushText, myRect.X, myRect.Y);
//utilise ce code si tu veux ecrire au centre du carreau
//g.DrawString(this.strScore[li, col], this.Font, brushText, (myRect.X + largRC / 2.0f), (myRect.Y + hautRC / 2.0f));
}
}
}
private void CreeTabGrille()
{
// rappel :
// Les tableaux sont indexés à partir de zéro .
// un tableau avec n éléments est indexé de 0 à n-1.
this.strScore = new string[this.NbLignes, this.NbColonnes];
this.RC = new Rectangle[this.NbLignes, this.NbColonnes];
int li, col;
for (li = 0; li < this.NbLignes; li++)
{
for (col = 0; col < this.NbColonnes; col++)
{
RC[li, col] = new Rectangle();
this.strScore[li, col] = "00";
}
}
}
//1 Prop lignes visible dans designer
[Browsable(true)]
[Category("Divers")]
private int _NbLignes = 10;
public int NbLignes
{
get { return _NbLignes;}
set
{
// nbre de lignes a change
// recalculer grille et redessiner
if (_NbLignes != value) //si nbre de lignes a change recalculer grille
{
_NbLignes = value;
this.CreeTabGrille();
this.Invalidate();
}
}
}
//1 Prop colonnes visible dans designer
[Browsable(true)]
private int _NbColonnes = 5;
public int NbColonnes
{
get { return _NbColonnes;}
set
{
if (_NbColonnes != value)
{
_NbColonnes = value;
this.CreeTabGrille();
this.Invalidate();
}
}
}
//1 Prop strScore
[Browsable(false)]
public string[,] strScore { get; set; }
}
} |
Partager