Recent

Author Topic: Disable Button when No Rows In StringGrid  (Read 981 times)

Skaliwop

  • Newbie
  • Posts: 4
Disable Button when No Rows In StringGrid
« on: December 03, 2019, 05:26:44 pm »
Hello,

I have a StringGrid and two Button on my form. The StringGrid has one fixed row, ButtonAdd adds rows and ButtonRemove removes rows. I want it so that if the StringGrid has no rows (except for the fixed row), then the ButtonRemove is disabled. The following code isn't working as I intend it to:

Code: Pascal  [Select][+][-]
  1. procedure TForm.ButtonRemove(Sender: TObject);
  2. begin
  3.   DeleteCurrentRow(StringGrid);
  4.   if StringGrid.RowCount = 1 then;
  5.   begin
  6.     ButtonRemove.Enabled := False;
  7.   end;
  8. end;  

If anybody could tell me why this is not working, it would be much appreciated.

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Disable Button when No Rows In StringGrid
« Reply #1 on: December 03, 2019, 05:40:30 pm »
Hello Skaliwop.
Welcome to the forum.

The bug happens on your line #4.

This is yours:
Quote
if StringGrid.RowCount = 1 then;


This below is correct:
Quote
if StringGrid.RowCount = 1 then

Can you see the different?

Skaliwop

  • Newbie
  • Posts: 4
Re: Disable Button when No Rows In StringGrid
« Reply #2 on: December 03, 2019, 05:43:28 pm »
That works perfectly now, thank you Handoko.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Disable Button when No Rows In StringGrid
« Reply #3 on: December 03, 2019, 06:12:21 pm »
The following code isn't working as I intend it to:

Code: Pascal  [Select][+][-]
  1. procedure TForm.ButtonRemove(Sender: TObject);
  2. begin
  3.   DeleteCurrentRow(StringGrid);
  4.   if StringGrid.RowCount = 1 then;
  5.   begin
  6.     ButtonRemove.Enabled := False;
  7.   end;
  8. end;  

Is that exactly what you have in your code? Because if so the button will be disabled the first time you click it; because the ";" after the "then" that "if" statement does nothing.

ETA: Crossed postings; never mind  %)
« Last Edit: December 03, 2019, 06:21:57 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018