Click here to Skip to main content
15,888,166 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear sir,

I'm working in delphi-2010.
I have added 2 TSpeedButton.

I want to add another button in each button.
For Example-
In photoshop from Toolbox which contain a Slice tool.
Then there are 3 different types of slice available but at a time only one is activated.

How can I make this type of button.

Please Help me.
Posted
Updated 19-Dec-10 22:34pm
v2
Comments
Dalek Dave 20-Dec-10 4:34am    
Edited for Readability.

1 solution

Hello,

There are many possible solutions to the feature you want to implement. Here is one of the more easier ones (besides buying a set of third party components that have that functionality ;P):

Ingredients
2 x TSpeedButton
1 x TPopupMenu component
n x TMenuItem (for your different 'button' options)

Recipe
Place the 2 buttons side-by-side. Make the second button just wide enough to hold the down arrow image. Depending on how you want to display the 'selected' event/action for the button, you can fiddle around with the size of the first button. On your second button click event, you will to do something like:
procedure SecondButtonClickEvent(Sender : TObject);
begin
  // NOTE: You can define your own calculation to find a particular 
  // position where you want the menu to popup, like the bottom-right
  // of the button for instance...
  MyPopupMenu.Popup(Mouse.CursorPos.X, Mouse.CursorPos.Y);
end;

In your menu item events, make sure you have a line of code that goes something like this:
procedure SetupMyFirstButton(SelectedItem : TMenuItem);
begin
  FirstSpeedButton.OnClick := SelectedItem.OnClick;
  // You may also want to set the caption and/or image
  // as well as anything else that may come to mind...
end;

procedure SomeMenuItemOnClickEvent(Sender : TObject);
begin
  // Perform some stuff for the event...
  // Now do this:
  if (Sender is TMenuItem) then
  // We only want to do this if the calling component is
  // actually a menu item because it would be pointless to
  // assign this when we click the button.
  begin
    SetupMyFirstButton(TMenuItem(Sender));
  end;
end;

By doing this, you can make sure you have your 'last selected' menu item is always linked to the first button. In addition, you can assign a 'default' event to the first button when you initialise the form that you are building this on like so:
//In some initialisation part of your form...
SetupMyFirstButton(TheMenuItemYouWantToBeDefault);

DISCLAIMER: This is by no means the 'most correct' way of doing this. I take no responsiblity if this code, for some or other reason, causes the lifecycle of the common sea cucumber to become irrevocably broken... :-D
 
Share this answer
 
Comments
Dalek Dave 20-Dec-10 4:35am    
Good Answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900