Recent

Author Topic: Disable Button when No Rows In StringGrid  (Read 410 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: 3239
  • 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: 2147
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 2.0.4/2.0.6  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.