Topic: Disable / Enable a button

I wanted disable a button when there is nothing selected on tGrid and enable it when something selected on tGrid.


I tried this, didn't work:

procedure Form1_OnShow (Sender: TObject; Action: string);
begin
  if Form1.tgMainCustomers.SelectedRow = -1 then Form1.btnAddInvSelectedCust.Enabled := False else Form1.btnAddInvSelectedCust.Enabled := True;

This one didn't work too:

procedure Form1_OnShow (Sender: TObject; Action: string);
begin
  if Form1.tgMainCustomers.SelectedRow = -1 then Form1.btnAddInvSelectedCust.Enabled := False;
  if Form1.tgMainCustomers.SelectedRow = 0 > then Form1.btnAddInvSelectedCust.Enabled := True;
end;

Ended up doing it with two procedures:

procedure Form1_tgMainCustomers_OnCellClick (Sender: TObject; ACol, ARow: Integer);
begin
  Form1.btnAddInvSelectedCust.Enabled := True;
end;

procedure Form1_OnShow (Sender: TObject; Action: string);
begin
  if Form1.tgMainCustomers.SelectedRow = -1 then Form1.btnAddInvSelectedCust.Enabled := False;
end;

Isn't it possible doing with only:
procedure Form1_OnShow (Sender: TObject; Action: string);
begin
......
end;

Adam
God... please help me become the person my dog thinks I am.

2 (edited by derek 2017-11-23 10:55:02)

Re: Disable / Enable a button

Hi Adam,
Perhaps I'm misunderstanding your question but is there any reason why you don't want to set the button's default enabled state to 'false'' in the button's object properties instead of scripting to disable it when your form is first shown?  Then you'd only need the one statement in your script - something like

procedure Form1_TableGrid1_OnClick (Sender: TObject);
begin
  if form1.tablegrid1.SelectedRow > -1 then form1.button2.enabled:= true else form1.button2.enabled := false;
end;

(although you don't really need the 'else.......' because once you start working with your grid, there will always be one of the rows that is highlighted).
Derek.

3 (edited by wenchester21 2017-11-23 15:49:15)

Re: Disable / Enable a button

You can also do it this way

procedure frmMain_TableGrid1_OnClick (Sender: TObject);
var
   s: string;
begin
   if frmMain.TableGrid1.SelectedRow > -1 then
   begin
   // check permission to print
   s := VarToStr( SQLExecute('SELECT print FROM users WHERE (id = ' + IntToStr(idUser) + ');') );
   if s<>'0' then frmMain.Button3.Enabled := True;  // activate the button to print the record
   end;
     end;

This way checks if a user has printing permission, before enabling or not the button.

Wen.

Re: Disable / Enable a button

Hi Derek,

Looks like it's a case of not seeing woods out of trees on my part. Thank you very much..........


Hi wenchester21,
Thank you very much for the alternative...........

Adam
God... please help me become the person my dog thinks I am.