Difference between revisions of "TRadioButton/fr"
Line 59: | Line 59: | ||
= Groupement = | = Groupement = | ||
− | + | Si vous ajoutez un RadioButton sur votre fiche, celle-ci est le [[Parent/fr|parent]] (le contrôle qui inclut votre RadioButton). A chaque fois que vous sélectionnez un RadioButon (par clic ou par code), l'autre RadioButton de même parent précédemment sélectionné voit sa propriété ''Checked'' changée en False.<br/> | |
− | + | Si vous voulez utiliser différents RadioButtons conçus pour des choix différents et indépendants, vous devez regrouper les RadioButtons dans un même conteneur (celui qui a le rôle de parent). Vous pouvez utiliser un composant [[TRadioGroup/fr|TRadioGroup]] ou grouper vos RadioButtons sur un contrôle (p.ex. [[TPanel/fr|TPanel]], [[TGroupBox/fr|TGroupBox]], [[TNotebook/fr|TNotebook]], [[TPageControl/fr|TPageControl]] etc.).<br/> | |
− | + | L'exemple suivant montre comment vous pouvez regrouper les RadioButtons : | |
− | + | Vous pouvez modifier l'exemple [[#Un_simple_exemple|Un simple exemple]] ou créer une nouvelle application : | |
− | + | * En premier, vous placez un [[TGroupBox/fr|TGroupBox]] de la palette de composants Standard sur votre fiche. | |
− | * | + | * Vous changez son nom en ''gbColor'' et son intitulé en ''Color''. |
− | * | + | * Maintenant, vous sous-classez cette GroupBox les RadioButtons ''rbRed'', ''rbGreen'' et ''rblue'' : |
− | * | + | * * * A FINIR * * * |
** In the modified project, you can sequentially move the radiobuttons '''in the Object Inspector''' by drag and drop to ''gbColor''. | ** In the modified project, you can sequentially move the radiobuttons '''in the Object Inspector''' by drag and drop to ''gbColor''. | ||
** In a new project, you can insert the three radiobuttons one after the other, by clicking to insert in the GroupBox, then change the names to ''rbRed'', ''rbGreen'' and ''rbBlue'' and the captions to ''Red'', ''Green'' and ''Blue''. | ** In a new project, you can insert the three radiobuttons one after the other, by clicking to insert in the GroupBox, then change the names to ''rbRed'', ''rbGreen'' and ''rbBlue'' and the captions to ''Red'', ''Green'' and ''Blue''. |
Revision as of 08:03, 29 August 2017
│
Deutsch (de) │
English (en) │
suomi (fi) │
français (fr) │
日本語 (ja) │
Un TRadioButton est un composant qui affiche un bouton de sélection qui fonctionne avec d'autres RadioButtons d'une manière mutuellement exclusive - Si un button est sélectionné, aucun des autres dans le groupe ne peut être sélectionné. Le composant TRadioButton se trouve dans l'onglet Standard de la palette de composants.
Pour utiliser un RadioButton sur une fiche, vous pouvez simplement le sélectionner sur la palette de composants Standard et le placer, avec un clic sur la fiche.
Il ne sert à rien en général de poser un seul RadioButton, car les RadioButtons sont prévus pour choisir entre plusieurs alternatives. Ainsi, vous pouvez utiliser aussi un
TRadioGroup à la place de RadioButtons individuels.
Partout dans votre code source, vous pouvez obtenir l'état des RadioButtons, actif ou inactif, en interrogeant Status := <RadioButton>.Checked;
. Vous pouvez utiliser Checked comme un booléen normal. Ainsi, l'affectation <RadioButton>.Checked := True;
est possible.
Un simple exemple
- Créez une nouvelle application et placez trois RadioButtons sur la fiche.
- Dans l'onglet Propriétés de l'inspecteur d'objets, changez les noms des RadioButtons en rbRed, rbGreen et rbBlue.
- De la même façon, vous pouvez changer les intitulés des RadioButtons en Red, Green et Blue.
- Ajoutez à votre fiche, un bouton et changez son intitulé en Draw new et son nom en btnPaint.
- Créez le gestionnaire d'événement OnClick pour le bouton, en utilisant l'onglet Evénements de l'inspecteur d'objet, sélectionnez l'événement OnClick et cliquez sur le bouton [...] ou double-cliquez le bouton sur la fiche.
- Ajoutez le code suivant :
procedure TForm1.btnPaintClick(Sender: TObject);
begin
if rbRed.Checked then Color:=clRed;
if rbGreen.Checked then Color:=clLime;
if rbBlue.Checked then Color:=clBlue;
end;
- Ouvrez votre application, elle devrait ressembler à quelque chose comme cela :
Usage d'un événement
A la différence avec l'exemple précédent, nous ne repeignons pas la fiche en cliquant sur le bouton mais en cliquant sur les RadioButtons eux-mêmes.
Vous pouvez modifier l'exemple précédent, en supprimant le bouton et son gestionnaire d'événement OnClick dans le code source, vous pouvez créer un nouvel exemple mais aussi simple :
- Créez une nouvelle application et placez trois RadioButtons sur la fiche.
- Dans l'onglet Propriétés de l'inspecteur d'objets, changez les noms des RadioButtons en rbRed, rbGreen et rbBlue.
- De la même façon, vous pouvez changer les intitulés des RadioButtons en Red, Green etBlue.
- Maintenant vous pouvez créer les gestionnaires d'événement pour les RadioButtons. Pour chacun d'eux, vous pouvez utiliser l'onglet événements de l'inspecteur d'objet, sélectionner l'événement OnChange et cliquez sur le bouton [...], vous pouvez aussi double-cliquer dessus.
- Faites changer la couleur de la fiche par le gestionnaire d'événement OnChange selon le RadioButton sélectionné :
procedure TForm1.rbRedChange(Sender: TObject);
begin
Self.Color:=clRed; //with "Self", you select the object in which the method exists (method: rbRedChange / object: Form1)
end;
procedure TForm1.rbGreenChange(Sender: TObject);
begin
Form1.Color:=clLime; //You can directly select the object ''Form1'', but poor,
//because then no other object of class 'TForm1' can be created
end;
procedure TForm1.rbBlueChange(Sender: TObject);
begin
Color:=clBlue; //or you leave out "Self" and the compiler will automatically detect its own object
end;
- Ouvrez votre application, elle devrait ressembler à ceci :
Groupement
Si vous ajoutez un RadioButton sur votre fiche, celle-ci est le parent (le contrôle qui inclut votre RadioButton). A chaque fois que vous sélectionnez un RadioButon (par clic ou par code), l'autre RadioButton de même parent précédemment sélectionné voit sa propriété Checked changée en False.
Si vous voulez utiliser différents RadioButtons conçus pour des choix différents et indépendants, vous devez regrouper les RadioButtons dans un même conteneur (celui qui a le rôle de parent). Vous pouvez utiliser un composant TRadioGroup ou grouper vos RadioButtons sur un contrôle (p.ex. TPanel, TGroupBox, TNotebook, TPageControl etc.).
L'exemple suivant montre comment vous pouvez regrouper les RadioButtons : Vous pouvez modifier l'exemple Un simple exemple ou créer une nouvelle application :
- En premier, vous placez un TGroupBox de la palette de composants Standard sur votre fiche.
- Vous changez son nom en gbColor et son intitulé en Color.
- Maintenant, vous sous-classez cette GroupBox les RadioButtons rbRed, rbGreen et rblue :
* * * A FINIR * * *
- In the modified project, you can sequentially move the radiobuttons in the Object Inspector by drag and drop to gbColor.
- In a new project, you can insert the three radiobuttons one after the other, by clicking to insert in the GroupBox, then change the names to rbRed, rbGreen and rbBlue and the captions to Red, Green and Blue.
- Now place a second TGroupBox on your form named gbBrightness with the caption Brightness.
- Add this GroupBox also three radio buttons and give it the name rbBrightDark, rbBrightMedDark and rbBrightBright and the captions Dark, MediumDark and Bright.
- If you have created a new application, you must add a button with name btnPaint and caption Draw new to the form.
- In the OnClick event handler of btnPaint change the code to:
procedure TForm1.btnPaintClick(Sender: TObject);
begin
if rbRed.Checked then Color:=Brightness or clRed;
if rbGreen.Checked then Color:=Brightness or clLime;
if rbBlue.Checked then Color:=Brightness or clBlue;
end;
- Now create even the function Brightness, by enter the private section of TForm1, write
function Brightness: TColor;
and press the keys [CTRL] + [Shift] + [c] (code completion). The function is created. Enter there following code:
function TForm1.Brightness: TColor;
begin
Result:=0;
if rbBrightMedDark.Checked then Result:=$888888;
if rbBrightBright.Checked then Result:=$DDDDDD;
end;
- Start your application, you can use the grouped radio buttons separate, so it could look like:
Voir aussi
- Doc. TRadioButton
- TRadioGroup - Usage de RadioGroups
- TToggleBox - Usage de Toggleboxes
- TCheckBox - Usage de CheckBoxes