Home

Windows Control: The Speed Checked Button

 

The Checked Speed Button

 

Introduction

As seen already, the speed button is a variant of the button control as this object is defined in Microsoft Windows. In the Win32, a button can be a command, a round object, or a check box. In this latter option, one of the characteristics of a check box is to appear as a normal rectangular button that, when clicked, behaves like a check box. This means that, when the button is clicked, it stays down. If it was down already and it gets clicked, it becomes up. A button can follow this description independently of any other control on the same container. This means that, if you add more than one button to the container, you can have one or a few of them down while the other(s) is (are) up.

 

Practical LearningPractical Learning: Introducing Checked Speed Buttons

The application we are going to create allows a user to perform the conjunction or disjunction Boolean operation.

  1. Start Embarcadero RAD Studio
  2. To create a new application, on the Welcome Page, click New Project...
  3. In the left list of the New Items dialog box, click C++Builder Projects.
    In the right list, click VCL Forms Application
  4. While the default form is selected, in the Object Inspector, change its properties as follows:
    Caption: Boolean Algebra
    Name: frmBoolean
    Position: poScreenCenter

 

Creating a Checked Speed Button

To create a rectangular button that behaves like a check box, from the Additional tab of the Tool Palette, click the TSpeedButton control Speed Button  and add it to the desired container. Of course, to programmatically create the control, declare a variable of type TSpeedButton and appropriately initialize it.

Practical LearningPractical Learning: Creating Checked Speed Buttons

  • Design the form as follows:
     
    Boolean Algebra
    Control Caption Name Font
    Font Size Font style Color
    TStaticText TLabel A sttOperand1 Tiffany Lt BT (*) 32 Demi Blue
    TStaticText TLabel B sttOperand2 Tiffany Lt BT (*) 32 Demi Blue
    TStaticText TLabel A ^ B sttOperation Tiffany Lt BT (*) 32 Demi Blue
    TSpeedButton Speed Button False sbtOperand1 Tiffany Lt BT (*) 28 Demi  
    TSpeedButton Speed Button False sbtOperand2 Tiffany Lt BT (*) 28 Demi  
    TSpeedButton Speed Button False btnResult Tiffany Lt BT (*) 28 Demi  
    TButton TButton Check btnCheck Tiffany Lt BT (*) 28 Demi  
    TButton TButton New Operation btnNewOperation Tiffany Lt BT (*) 28 Demi  
    TButton TButton Close btnClose Tiffany Lt BT(*) 28 Demi  
     

    (*) If you don't have the "Tiffany Lt BT" font, select any Serif font of your choice (such as Garamond, Georgia, or Times New Roman)

Characteristics of the Checked Speed Button

 

Introduction

A checked speed button is primarily a normal Windows control. It has a location and size, which you can easily set at design time. One of the characteristics you will pay attention to is the caption. A speed button doesn't need a caption. In fact, its behavior can be sufficient to indicate what it is used for. But if you decide not to give it a caption, you should make sure the user knows or can figure out what the role of that button is in your application.

Implementing a Checked Speed Button

Two characteristics make a speed button adopt the behave of a check box:

  • The button must has a unique GroupIndex value. It doesn't matter what that value is as long as it is an integer:
    • If you are using just one speed button in the container, you can give it any value you want and not pay attention to the other controls
    • If you are using various speed buttons in the same container and you want each speed button to behave like a check box, each speed button must have a different GroupIndex value
  • The button must have the AllowUp property set to True. If there are more than one speed button that must behave like check boxes, each one of them must have this property set to True

 

Practical LearningPractical Learning: Implementing Checked Speed Buttons

  1. Change the characteristics of the speed buttons as follows:
     
    Control AllowUp GroupIndex
    sbtOperand1 True 12
    sbtOperand2 True 14
    sbtOperation True 16
  2. Double-click the top-left speed button
  3. Implement its event as follows:
    //---------------------------------------------------------------------------
    void __fastcall TfrmBoolean::sbtOperand1Click(TObject *Sender)
    {
        if( sbtOperand1->Down == true )
    	sbtOperand1->Caption = L"True";
        else
    	sbtOperand1->Caption = L"False";
    }
    //---------------------------------------------------------------------------
  4. Return to the form
  5. Double-click the top-center speed button
  6. Implement its event as follows:
    //---------------------------------------------------------------------------
    void __fastcall TfrmBoolean::sbtOperand2Click(TObject *Sender)
    {
        if( sbtOperand2->Down == true )
    	sbtOperand2->Caption = L"True";
        else
    	sbtOperand2->Caption = L"False";
    }
    //---------------------------------------------------------------------------
  7. Return to the form
  8. Double-click the top-right speed button
  9. Implement its event as follows:
    //---------------------------------------------------------------------------
    void __fastcall TfrmBoolean::sbtResultClick(TObject *Sender)
    {
        if( sbtResult->Down == true )
    	sbtResult->Caption = L"True";
        else
    	sbtResult->Caption = L"False";
    }
    //---------------------------------------------------------------------------
  10. Return to the form
  11. Double-click the New Operation button
  12. Implement its event as follows:
     
    //---------------------------------------------------------------------------
    void __fastcall TfrmBoolean::btnNewOperationClick(TObject *Sender)
    {
    	UnicodeString strBooleanValues[] = { "True", "False" };
    	UnicodeString strOperations[] = { "A ^ B", "A V B" };
    
    	sbtOperand1->Caption = strBooleanValues[Random(2)];
    	sbtOperand2->Caption = strBooleanValues[Random(2)];
    
    	if( sbtOperand1->Caption == L"True" )
    		sbtOperand1->Down = true;
    	else
    		sbtOperand1->Down = false;
    
    	if( sbtOperand2->Caption == L"True" )
    		sbtOperand2->Down = true;
    	else
    		sbtOperand2->Down = false;
    
    	sttOperation->Caption = strOperations[Random(2)];
    }
    //---------------------------------------------------------------------------
  13. Return to the form
  14. Double-click the Checkl button
  15. Implement its event as follows:
    //---------------------------------------------------------------------------
    void __fastcall TfrmBoolean::btnCheckClick(TObject *Sender)
    {
        // Logical Conjunction
        if( sttOperation->Caption == "A ^ B" )
        {
    	// If A = true
    	if( sbtOperand1->Down == true )
    	{
    	    if( sbtOperand2->Down == true )
    	    {
    		if( sbtResult->Down == true )
    		{
    		    // | A | B | A ^ B |
    		    // | T | T |   T   |
    		    ShowMessage(L"Bravo - Good Answer");
    		}
    		else if( sbtResult->Down == false )
    		{
    		    // | A | B | A ^ B |
    		    // | T | T |   F   |
    		    ShowMessage(L"Wrong - Get it right next time!");
    		}
    	    }
    	    else if( sbtOperand2->Down == false )
    	    {
    		if (sbtResult->Down == false)
    		{
    		    // | A | B | A ^ B |
    		    // | T | F |   F   |
    		    ShowMessage(L"Bravo - Good Answer");
    		}
    		else  if (sbtResult->Down == true)
    		{
    		    // | A | B | A ^ B |
    		    // | T | F |   T   |
    		    ShowMessage(L"Wrong - Get it right next time!");
    		}
    	    }
    	}
    	else if( sbtOperand1->Down == false )
    	{
    	    if( sbtOperand2->Down == true )
    	    {
    		if( sbtResult->Down == false )
    		{
    		    // | A | B | A ^ B |
    		    // | F | T |   F   |
    		    ShowMessage(L"Bravo - Good Answer");
    		}
    		else  if (sbtResult->Down == true )
    		{
    		    // | A | B | A ^ B |
    		    // | F | T |   T   |
    		    ShowMessage(L"Wrong - Get it right next time!");
    		}
    	    }
    	    else if( sbtOperand2->Down == false )
    	    {
    		if (sbtResult->Down == false)
    		{
    		    // | A | B | A ^ B |
    		    // | F | F |   F   |
    		    ShowMessage(L"Bravo - Good Answer");
    		}
    	        else  if (sbtResult->Down == true)
    		{
    		    // | A | B | A ^ B |
    		    // | F | F |   T   |
    		    ShowMessage(L"Wrong - Get it right next time!");
    		}
    	    }
    	}
        }
        else if( sttOperation->Caption == "A V B") // Logical Disjunction:
        {
    	// If A = true
    	if( sbtOperand1->Down == true)
    	{
    	    if( sbtOperand2->Down == true)
    	    {
    		if( sbtResult->Down == true)
    		{
    		    // | A | B | A V B |
    		    // | T | T |   T   |
    		    ShowMessage(L"Bravo - Good Answer");
    		}
    		else if (sbtResult->Down == false)
    		{
    		    // | A | B | A V B |
    		    // | T | T |   F   |
    		    ShowMessage(L"Wrong - Get it right next time!");
    		}
    	    }
    	    else if (sbtOperand2->Down == false)
    	    {
    		if (sbtResult->Down == true)
    		{
    		    // | A | B | A V B |
    		    // | T | F |   T   |
    		    ShowMessage(L"Bravo - Good Answer");
    		}
    		else if (sbtResult->Down == false)
    		{
    		    // | A | B | A V B |
    		    // | T | F |   F   |
    		    ShowMessage(L"Wrong - Get it right next time!");
    		}
    	    }
    	}
    	else if (sbtOperand1->Down == false)
    	{
    	    if (sbtOperand2->Down == true)
    	    {
    		if( sbtResult->Down == true)
    		{
    		    // | A | B | A V B |
    		    // | F | T |   T   |
    		    ShowMessage(L"Bravo - Good Answer");
    		}
    		else if( sbtResult->Down == false)
    		{
    		    // | A | B | A V B |
    		    // | F | T |   F   |
    		    ShowMessage(L"Wrong - Get it right next time!");
    		}
    	    }
    	    else if( sbtOperand2->Down == false)
    	    {
    		if( sbtResult->Down == false)
    		{
    		    // | A | B | A V B |
    		    // | F | F |   F   |
    		    ShowMessage(L"Bravo - Good Answer");
    		}
    		else if( sbtResult->Down == true)
    		{
    		    // | A | B | A V B |
    		    // | F | F |   T   |
    		    ShowMessage(L"Wrong - Get it right next time!");
    		}
    	    }
    	}
        }
    }
    //---------------------------------------------------------------------------
  16. Return to the form
  17. Double-click the Close button
  18. Implement its event as follows:
    //---------------------------------------------------------------------------
    void __fastcall TfrmBoolean::btnCloseClick(TObject *Sender)
    {
        Close();
    }
    //---------------------------------------------------------------------------
  19. Press F9 to test the application
  20. Click the New Operation button. Here is an example
     
    Boolean Algebra
  21. Click the top-right speed to express your answer
  22. Click the Check button. Here is an example:
     
    Boolean Algebra
  23. Perform another operation and check it
     
    Boolean Algebra
     
    Boolean Algebra
  24. Close the form and return to your programming environment

A Flat Button

The Flat property can be used to enhance the appearance of a checked speed button. If you set it to True, when the control is not checked, it appears completely flat. In fact, if the control doesn't have a caption (or a picture), it would appear invisible. Only when the mouse is positioned on top of the control would it signal its presence.

The Glyphs of a Speed Button

If you plan to use a bitmap on the speed button, its Glyph, its Spacing, its NumGlyphs, its Margin, and its Layout properties function similar to the same properties of the bitmap button. The Glyph property of the speed button provides a small addition. Like the bitmap button, the speed button can use a bitmap made of one or more glyphs. Unlike the bitmap button, the speed button can use up to four glyphs and here is how they work:

  • If the bitmap contains 1 glyph, this single bitmap would always be used except when the button is disabled. The operating system is in charge of displaying or painting the face of the button when it is disabled
  • If the bitmap contains 2 glyphs, the first glyph would be used both when the button is clicked or down and when the button is not clicked or is not down. The second glyph would be used when the button is disabled
  • If the bitmap contains 3 glyphs, the first would be used when the button displays normally. The second glyph would display when the button is disabled. The third glyph would display while the button is down when it has been clicked
  • If the bitmap contains 4 glyphs, this style is valid only if the button is a member of a group (like radio buttons). The first would be used when the button displays normally. The second glyph would display when the button is disabled. The third glyph would display while the button is down when it has been clicked, exactly like the button with a NumGlyphs value equal to 3 but with the following exception. If the button is a member of a group, the third glyph would display while the mouse is still “pushing” it; that is, while the button is down but held by the mouse. The fourth glyph is used only if the button is part of a group and when it is down after the mouse has been released
 
 
 

Previous Copyright © 2010-2011 FunctionX, Inc. Home