PDA

View Full Version : Hỏi về Delphi 8



ithichyou
14-01-2005, 13:35
Nhờ mọi người chỉ cách tạo và quản lý 1 mảng Control (như Button hay image chẳng hạn) . Chứ nếu mà muốn tạo cỡ 100 cái Button chẳng hạn ,mà nếu kéo thả 100 cái thì .. .

ithichyou
14-01-2005, 20:43
Vậy ta có thể dùng code để tạo không

ghosthd
14-01-2005, 21:54
Copy paste là được, còn nếu muốn quản lý nó thì tạo trực tiếng từ code bằng mảng button
button:array[1..100] of TButton;
rồi sử lý thôi

tạo button[1]:=tbutton.create(self);
button.control:=self;
button.left:=10;
button.top:=10;
button.width:=20;
button.height:=10;
Thế là ổn

ithichyou
15-01-2005, 19:08
Cám ơn ghosthd :). Nhưng hình code sai cú pháp thì phải , dặc biệt là dòng
" button.control:=self" . Nhờ bạn xem lại . Cám ơn nhiều .

ghosthd
16-01-2005, 11:25
button[1]:=tbutton.create(self);
button[1].control:=self;
button[1].left:=10;
button[1].top:=10;
button[1].width:=20;
button[1].height:=10;
button[1].show;
Như thế này là đúng quyên cái chỉ số [1] và phương thức trình bầy show
Còn button[1].control:=self là đúng không sai, đây là cách chỉ định điều khiển cha cho nó thôi
Chúc học tập tốt!

ithichyou
18-01-2005, 14:37
Mình đã thử nhưng vẫn không được , Delphi báo không biết "control" là gì cả , nhờ bạn xem lại cú pháp , mình đã xem thử , thấy có 1 giá trị là "controls" nhưng không phải kiểu boolean.

ghosthd
23-01-2005, 10:24
Xin lỗi bạn, vì cái parent không được delphi chuẩn hóa nên khai báo ở mỗi com một khác ở button nó gọi là parent bạn thay câu lện đó thành
button[1].parent:=self;

Thành thật xin lỗi!

ithichyou
24-01-2005, 14:08
vậy ở image là gì vậy anh ?

t2l3k4
07-02-2005, 07:32
Mày có dùng Delphi 5 chưa mà qua Delphi 8 hả Thành ặc ặc...

Còn nữa sao ko dùng Delphi 2005 luôn cho tiện

ithichyou
21-03-2005, 18:32
Em có 1 chương trình , trong đó có rất nhiều image , em muốn xử lý tình huống là khi 1image nào đó được click thì 1 label1 sẽ hiện lên vị trí của image đó , vậy em phải làm sao ? . Nhờ mọi người chỉ bảo .

jiSh@n
22-03-2005, 17:21
Gom sự kiện OnClick của tất cả các image lại thành 1 cái, typecast cái Sender dể phân biệt được cái nào bị click.

ithichyou
22-03-2005, 21:08
Anh có thể chỉ rõ hơn được không (em mới họcthôi ) .

ithichyou
29-03-2005, 20:44
Trùi , sao im ru vây nè , moi nguoi có ai có doan code vd không , cho em xin voi .

mathswt
31-03-2005, 21:08
Title: Using One Event for many Components
Author: Lou Adler
Product: Delphi 2.x (or higher)
Post Date: 01/24/2003

Problem/Question/Abstract:

Using One Event for many Components

Answer:

This tip is a pretty basic one, but if you haven't come across it, using it can make your coding a lot easier and more efficient.

Delphi allows one coded event to be assigned to many event handlers as long as the methods are of the same type - ie., they have the same arguments. The classic example is the OnClick event which is shared by many components. This can be done in code in the following way.

procedure ClickMyComponent(Sender: Tobject);
begin
//Some code goes here
ShowMessage('This is my Onclick handler');
end;

{You can then assign this procedure to as many event handlers as you wish.}

procedure AssignEvents;
begin
MyButton.OnClick := ClickMyComponent;
MyLabel.OnClick := ClickMyComponent;
MyCombo.Onclick := ClickMyComponent;
// You Get the message!
end;

Each time one of these components is clicked, the above procedure 'ClickMyComponent' is executed. This can be useful is certain circumstances, but mainly in this tip it serves to illustrate my main point - reduce designtime coding by assigning events.

Assigning at designtime.

In the object inspector you may have noticed that the right hand side of the events tab is a combo box. This allows you to choose to assign an already written event handler to the current components event (only event handlers of the right type are shown in the combo box). This is the design time way of doing the above code.

At a glance it can seem like a nice but essentially useless ability to have. How often are you likely to have two buttons, or a button and a label that share the same onclick code. This may be true but it does happen. Also an area where you do have many controls sharing code is in respect to Main Menus, Popup Menus and Speedbuttons.

eg.,

You want to let the user of your application be able to set the view type of a TlistView component at run time so you assign the four values to a View Menu on your main menu and you right the necessary code in each event handler of each menu item. Later on you decide to add speedbuttons, and a popupmenu in the listview to do the same thing.

Do you rewrite all the code? Redo all the work you have done (even if you cut and paste)?

No you assign each new speedbutton and popupmenu item in its onclick handler in the objectInspector the handler for the corresponding mainmenu item.

Voila, you've managed to save yourself a lot of hassle and time and made your code more efficient (although possibly slightly harder to follow - so comment well).

Hope this is of use.

THE LAST LEAF
01-04-2005, 11:14
Cách sau tương tự cách trên chỉ khác là làm lúc designtime

-Giữ Shift chọn các control cần chung event (ví dụ OnClick) (các control này đặt Tag = 1,2,..,n)
-Chọn tab Events, gõ tên vào events OnClick ví dụ "ControlsClick"
-Double Click vào đấy ta được
procedure TForm1.ControlsClick(Sender: TObject);
begin

end;

-Bây giờ để nhận biết Control nào được Click ta căn cứ vào thuộc tính Tag đã
đặt (giả sử các Control là TButton)

procedure TForm1.ControlsClick(Sender: TObject);
begin
if TButton(Sender).Tag = 1 then
//Xử lý nút 1 click
if TButton(Sender).Tag = 2 then
//Xử lý nút 2 click
end;