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
| //---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
radios=NULL;
matXsize=3;
matYsize=3;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
// Construit les lignes de la matrice de radiobuttons
int group=0;
radios=new TRadioButton**[matYsize];
for (int y=0; y<matYsize; ++y) {
// Pour chaque ligne, construit les colonnes de radiobuttons
radios[y]=new TRadioButton*[matXsize];
for (int x=0; x<matXsize; ++x) {
// Construit un TPanel
TPanel* panel=new TPanel(this);
panel->Parent=this;
panel->Width=23;
panel->Height=23;
panel->Top=y*(panel->Height);
panel->Left=x*(panel->Width);
panel->BorderStyle=bsNone;
panel->BevelOuter=bvNone;
// Construit chaque radiobutton
radios[y][x]=new TRadioButton(this);
radios[y][x]->Parent=panel;
radios[y][x]->Width=17;
radios[y][x]->Height=17;
radios[y][x]->Caption="";
radios[y][x]->OnClick=RadioButtonClick;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
// Delete the matrix of buttons
for (int y=0; y<matYsize; ++y) {
delete[] radios[y];
}
delete[] radios;
}
//---------------------------------------------------------------------------
// PArcourt la matrice des bouttons pour retrouver les corrdonnées XY d'un radiobutton
TPoint TForm1::GetXY(TRadioButton* him) const {
for (int y=0; y<matYsize; ++y) {
for (int x=0; x<matXsize; ++x) {
if (radios[y][x]==him) {
return TPoint(x,y);
}
}
}
throw Exception("button not in the list");
}
//---------------------------------------------------------------------------
//! Test si un autre boutton de la colonne ou de la ligne est activé
void __fastcall TForm1::RadioButtonClick(TObject *Sender)
{
TRadioButton* radio=dynamic_cast<TRadioButton*>(Sender);
if ((radio)&&(radio->Checked)) {
TPoint xy=GetXY(radio);
bool found=false;
// Test si un bouton de la ligne est actif
for (int x=0; x<matXsize; ++x) {
if ((radios[xy.y][x]->Checked)&&(radios[xy.y][x]!=radio)) {
Application->MessageBoxA("Il y a déjà un bouton de coché dans cette ligne",
"Erreur",0);
radios[xy.y][x]->Checked=false;
found=true;
break;
}
}
// Test si un bouton de la ligne est actif
for (int y=0; y<matYsize; ++y) {
if ((radios[y][xy.x]->Checked)&&(radios[y][xy.x]!=radio)) {
Application->MessageBoxA("Il y a déjà un bouton de coché dans cette colonne",
"Erreur",0);
radios[y][xy.x]->Checked=false;
found=true;
break;
}
}
// Aucun bouton coché?
if (found) {
// radio->Checked=false;
}
}
}
//--------------------------------------------------------------------------- |