Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

On click procedure

Status
Not open for further replies.

draakans

Programmer
Dec 20, 2007
13
CA
Hello again,
is it possible to make a procedure with this:

procedure TForm1.Button1Click(Sender: TObject);
begin
EditBox1.enabled := FALSE:
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
EditBox2.enabled := FALSE:
end

procedure TForm1.Button3Click(Sender: TObject);
begin
EditBox3.enabled := FALSE:
end
[.. and so on..]

Is is possible to use a procedure that would say :

procedure Button(i).click;
begin
EditBox(i).enabled := FALSE;
end;

thx
 
Well, sort of. If you dynamically created an Array of Buttons, you could access them by index.

This is easier and probably closer to what you are trying to do...
Code:
procedure TForm1.MyButtonClick(Sender: TObject);
begin
  with Sender as TButton do
    if Name = 'Button1' then
      ShowMessage('You clicked button-1')
    else if Name = 'Button2' then
      ShowMessage('You clicked button-2')
    else if Name = 'Button3' then
      ShowMessage('You clicked button-3')
    else;
end;
To understand, it may help to see the form this works with...
Code:
object Form1: TForm1
  Left = 212
  Top = 139
  Width = 584
  Height = 355
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 112
    Top = 216
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = MyButtonClick
  end
  object Button2: TButton
    Left = 256
    Top = 224
    Width = 75
    Height = 25
    Caption = 'Button2'
    TabOrder = 1
    OnClick = MyButtonClick
  end
  object Button3: TButton
    Left = 416
    Top = 224
    Width = 75
    Height = 25
    Caption = 'Button3'
    TabOrder = 2
    OnClick = MyButtonClick
  end
end
And just in case, the entire code...
Code:
unit buttontest;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure MyButtonClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.MyButtonClick(Sender: TObject);
begin
  with Sender as TButton do
    if Name = 'Button1' then
      ShowMessage('You clicked button-1')
    else if Name = 'Button2' then
      ShowMessage('You clicked button-2')
    else if Name = 'Button3' then
      ShowMessage('You clicked button-3')
    else;
end;

end.
An alternate to doing it by Sender.Name, you could do it by number with, say TButton.Tag and assign each button a different (property) Tag value. For 1..3 it might look like this:
Code:
with Sender as TButton do
    case Tag of
      1: ShowMessage('You clicked button-1');
      2: ShowMessage('You clicked button-2');
      3: ShowMessage('You clicked button-3');
    end; //case
Or if you're cool, you could shorten it to:
Code:
with Sender as TButton do ShowMessage('You clicked button-' + IntToStr(Tag));

HTH

Roo
Delphi Rules!
 
Tags are probably the way to go, seeing as there is no built-in way to relate a button to an edit box. You could set up your own relationship array that links each button to it's edit box (useful if your form is really complex).

Setting up a for loop and cycling through every component (see your form's Components and ComponentCount properties), checking each form component to see if it's a TEdit eg:
Code:
[b]procedure[/b] TForm1.ButtonClick(Sender: TObject);
[b]var[/b]
  c : Integer;
[b]begin[/b]
  [b]for[/b] c := [purple]0[/purple] [b]to[/b] ComponentCount - [purple]1[/purple] [b]do[/b]
    [b]if[/b] (Components[c] [b]is[/b] TEdit)
      [b]and[/b] TButton(Sender).Tag = (Components[c].Tag) [b]then[/b]
      TEdit(Components[c]).Enabled := False;
[b]end[/b];
 
Wow thanks a lot. To make it interact with Edit Boxes, I guess I'll have to make it as an array of TCheckBox and an array of TEdit right? For example, to put an edit box enable when a checkbox is checked, full unit would be smth like:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Memo1: TMemo;
Procedure MCheckBoxOnClickEvent(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }

end;

var
Form1: TForm1;
Edit : array [1..3] of TEdit;
MCheckBox : array [1..3] of TCheckBox;

implementation

{$R *.dfm}

Procedure Tform1.MCheckBoxOnClickEvent(Sender: TObject);
var i : integer;
begin
With Sender As TCheckBox do
for I := 1 to 3 do Edit.enabled := MCheckBox.checked;
end;

procedure TForm1.FormCreate(Sender: TObject);
var T, L, H, W, i : integer;
begin
{Load Caption from textfile}
Memo1.Lines.LoadFromFile('C:\Progs\Tutorial\test.txt');
{Create Check Boxes}
T := 24; L := 224; H := 17; W:= 97;
For i := 1 to 3 do
begin
MCheckBox := TCheckBox.create(self);
MCheckBox.Parent := Self;
MCheckBox.top:=t + (24 * i+1);
MCheckBox.left:=l;
MCheckBox.height:=h;
MCheckBox.width:=w;
MCheckBox.Tag := i;
MCheckBox.Caption := Memo1.Lines.Strings;
McheckBox.Enabled := TRUE;
MCheckBox.Checked := FALSE;
MCheckBox.OnClick := MCheckBoxOnClickEvent;
end;
{Create Edit Boxes}
T := 24; L := 328; H := 21; W:= 121;
For i := 1 to 3 do
begin
Edit:=TEdit.Create(Self);
Edit.parent:=Self;
Edit.top:=t + (24 * i+1);
Edit.left:=l;
Edit.height:=h;
Edit.width:=w;
Edit.tag := i;
Edit.Enabled := FALSE;
Edit.Text := '';
Edit.show;
end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
var I : Integer;
begin
for I := 1 to 3 do
begin
Edit.Free;
MCheckBox.Free;
end;
end;

end.


I can't create an array of Tbutton, Tcheckbox, TEdit [...] using the "view as Form". I mean I have to create properties on the "view as Text" and set my Top, Left, Width and Height (and so on) myself.

Also, To use the "For" and add a caption on the check boxes. Either I use

Case i of
1 : MCheckBox.caption := 'The First CheckBox'
2 : MCheckBox.caption := 'the second CheckBox'
3 : MCheckBox.caption := 'CheckBox number 3'

or I do as I did with an invisible Buffer Memo reading on an external filetext?

Again thanks a lot :)
 
Well, I mean I will still use the "View as form" but I'll have to make my own properties. I can't drop CheckBoxes on the form and put them in array after.

Sorry it wasn't clear.
 
Not sure why you're creating the Checkboxes and Edit boxes dynamically as you are. Using the form designer would simplify things immensely.

You can still using your caption string file, by again referring to the Tag property of the controls you're using. Just reuse my code above to match to the control type you're looking for.
 
Griffyn is correct, I would use dynamic creation only if the number of instances of the components is required to change and there could be a lot of them, thus saving on memory.
But some will say that that is the correct way of doing it no doubt!
I showed how to use 'findcomponent' to get at sequentially named components in another post.



Steve: N.M.N.F.
If something is popular, it must be wrong: Mark Twain
 
well I figured out both ways. It brought me back to an older program that I wanted to modify soon.

I read TCheckListBox.Items on an IniFile, when the item is checked it enabled an TEdit associated to it. And I was wondering how could I make the user able add new Items codes. First, I thought putting Edit.visible := FALSE.

But I'll use those Array of Tedit and TCheckListBox. (It's already done actually and it works :))

I successfully took off about 150 Ifs lines to a 5-6 function code lol.

I've also tried playing with Tags and Components. It's really nice and I won't forget to use it.

Thanks a lot to all you guys for your precious time. You helped me a lot!!

I think you can mark this thread as Solved.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top