How to make a Delphi TSpeedButton stay pressed if

2019-02-12 08:58发布

I'm not sure why the TSpeedButton has this property but when a TSpeedButton is the only button of a given groupindex, it doesn't stay pressed, whether or not "AllowAllUp" is pressed. Maybe a Jedi control would suffice, but hopefully there's some fix. Any help or anecdotes are appreciated.

BTW, I'm (still) using Delphi 7, not sure if this is an across the board conundrum.

11条回答
【Aperson】
2楼-- · 2019-02-12 09:23

To get this to work, you can't just toggle the Down property, because it is always down in the OnClick event. You need to have another value:

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  MyBoolProperty := not MyBoolProperty;
  SpeedButton1.Down := MyBoolProperty;
end;
查看更多
混吃等死
3楼-- · 2019-02-12 09:27

Hm, strange, I remember using this quite a few times with success. Perhaps you should try playing with the SpeedButton's Down property? I don't think it toggles automatically when you click it --- you should explicitly toggle Down, I guess...

[edit: replaced Checked with Down --- TSpeedButton doesn't have a Checked property, sorry!]

查看更多
倾城 Initia
4楼-- · 2019-02-12 09:29

knight_killer is correct. i can tell you it'll work in any version of delphi:

object SpeedButton1: TSpeedButton
  Left = 152
  Top = 384
  Width = 23
  Height = 22
  AllowAllUp = True
  GroupIndex = 99
end
查看更多
SAY GOODBYE
5楼-- · 2019-02-12 09:33

I just tried that in Delphi 7 (Build 4.453):

  • create new application
  • add TSpeedButton to form
  • set AllowAllUp := true;
  • set GroupIndex := 1;
  • run application

When clicking the button it toggles its down state without any other code needed.

查看更多
再贱就再见
6楼-- · 2019-02-12 09:34

I was searching for a solution for my problem and I think this is kind of the same one. I wanted to make a SpeedButton toggle the up and down state just like a switch, and I managed this by setting the properties:

AllowAllUp := True; 
GroupIndex := 1;

Then in the OnClick event of the button I wrote:

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  if( SpeedButton1.AllowAllUp ) then 
  begin 
    SpeedButton1.AllowAllUp := False; 
    SpeedButton1.Down := True; 
  end else 
  begin 
    SpeedButton1.AllowAllUp := True; 
    SpeedButton1.Down := False; 
  end; 
end;

This toggles the button down when it's clicked and up when it's clicked again.

Hope it'll be of any help

查看更多
看我几分像从前
7楼-- · 2019-02-12 09:36

The trick is to set the GroupIndex to a unique value and set AllowAllUp to true. If you forget the first, it will not stay down, if you forget the second, it will not stay up, once it has been down.

查看更多
登录 后发表回答