Nazwa wybranego koloru w TEdit
Cel

Chcemy uzyskać efekt, aby po wybraniu przez urzytkownika koloru z palety, nazwa koloru została wpisana do pola Edit.

Końcowy efekt



Potrzebne komponenty

Nazwa

Klasa

Button1

TButton

ColorDialog1

TColorDialog

Edit1

TEdit



Metoda

1) Wstawiamy komponenty, wypisane w powyższej tabeli i zmieniamy im właściwość name na taką jaka jest w kolumnie "Nazwa"

2) Do obsługi zdarzenia OnClick przycisku dodajemy kod:

ColorDialog1.Execute; Edit1.Text := IntToStr(ColorDialog1.Color); if Edit1.Text = '0' then Edit1.Text := 'Black'; if Edit1.Text = '16711680' then Edit1.Text := 'Blue'; if Edit1.Text = '16776960' then Edit1.Text := 'Aqua'; if Edit1.Text = '65535' then Edit1.Text := 'Yellow'; if Edit1.Text = '255' then Edit1.Text := 'Red'; if Edit1.Text = '65280' then Edit1.Text := 'Lime'; if Edit1.Text = '32768' then Edit1.Text := 'Green'; if Edit1.Text = '16711935' then Edit1.Text := 'Fuchsia'; if Edit1.Text = '8388736' then Edit1.Text := 'Purple'; if Edit1.Text = '12632256' then Edit1.Text := 'Silver'; if Edit1.Text = '8421504' then Edit1.Text := 'Gray'; if Edit1.Text = '16777215' then Edit1.Text := 'White'; if Edit1.Text = '8421376' then Edit1.Text := 'Teal'; if Edit1.Text = '128' then Edit1.Text := 'Maroon';


3) Uruchamiamy program.

Porady

Powyższy kod zapisuje kod liczbowy wybranej liczby do pola Edit, jeżeli numer został zdefiniowany przez jedną z dalszych metod zmienia nazwę na angielską. W przypadku jeżeli kolor nie został zdefiniowany wyświetla kod numerowy. Listę kolorów możemy powiększyć poprzez dodanie kodu:

if Edit1.Text = 'numer' then Edit1.Text := 'nazwa';


Kod źródłowy

unit Unit1;   interface   uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;   type TForm1 = class(TForm) Button1: TButton; ColorDialog1: TColorDialog; Edit1: TEdit; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;   var Form1: TForm1;   implementation   {$R *.DFM}   procedure TForm1.Button1Click(Sender: TObject); begin ColorDialog1.Execute; Edit1.Text := IntToStr(ColorDialog1.Color); if Edit1.Text = '0' then Edit1.Text := 'Black'; if Edit1.Text = '16711680' then Edit1.Text := 'Blue'; if Edit1.Text = '16776960' then Edit1.Text := 'Aqua'; if Edit1.Text = '65535' then Edit1.Text := 'Yellow'; if Edit1.Text = '255' then Edit1.Text := 'Red'; if Edit1.Text = '65280' then Edit1.Text := 'Lime'; if Edit1.Text = '32768' then Edit1.Text := 'Green'; if Edit1.Text = '16711935' then Edit1.Text := 'Fuchsia'; if Edit1.Text = '8388736' then Edit1.Text := 'Purple'; if Edit1.Text = '12632256' then Edit1.Text := 'Silver'; if Edit1.Text = '8421504' then Edit1.Text := 'Gray'; if Edit1.Text = '16777215' then Edit1.Text := 'White'; if Edit1.Text = '8421376' then Edit1.Text := 'Teal'; if Edit1.Text = '128' then Edit1.Text := 'Maroon'; end;   end.


Autorem tekstu jest: Michał Siudziński
http://cpw.net.pl
Materiał dodany przez użytkownika: alphan