Example: Creating a text control

To create a text control, you need to:

  1. Create a text control state.
  2. Instantiate TTextControl, passing the control state in the constructor.
  3. Launch the control in a window.
You can also control the appearance and behavior of the text control by calling TTextControl functions. The following lists some functions you will probably use:

See the online class and member function documentation for a complete list of TTextControl functions.

This code demonstrates how to create a text control in a window. The resulting text control resizes itself to fit the input text.

      // Copyright (C) 1994 Taligent, Inc. All rights reserved.
      TGPoint kWindowSize = TGPoint(575, 125);
      TGPoint kWindowOffset = TGPoint(100, 25);
      TGPoint kTextControlOffset = TGPoint( 25, 25);
      
      
    TRequestHandler* rh = new TRequestHandler( new TRequestQueue() );
      TWindowGroup windowGroup( rh->GetRequestQueue() );
      
      TStandardText windowTitle( "SampleText Control" );
      TStandardWindow* window = new TStandardWindow(windowTitle, TRUE, TRUE, TRUE, TRUE, TRUE);
      
      
    windowGroup.AdoptContentWindow( window );
      window->SetWindowSize( kWindowSize );
      window->MoveWindowTo( kWindowOffset );
      
      
    TSimpleView* contentView = new TSimpleView;
      window->AdoptChildView( contentView );
      
      
    TStandardText initialText( "This is only a text of the text control system." );
      TSimpleTextControlState* state = new TSimpleTextControlState( initialText );
      TTextLabel* label = new TTextLabel( TStandardText( "Test Field:" );
      TTextControl* control = new TTextControl( state, label );
      
      
    control->SetTextBorderThickness( 5 );
      control->SetControlLayout( MControl::kLeftToRight );
      control->SetEnabled( TRUE );
      control->SetAllocatedArea( TGRect( TGPoint::GetOrigin(), TGPoint( 525, 50 ) ) );
      
      TStyleSet initialStyles;
      initialStyles.Add( TTextColorStyle::GetRed() );
      control->SetInitialTextStyles( initialStyles );
      
      
    contentView->AdoptChild( control );
      control->TranslateAllocatedAreaInParentTo( kTextControlOffset );
      
      
    windowGroup->Activate();
      window->Open();
      
      while ( window->IsOpen() ) {
          rh->WaitAndDispatchNextRequest();
      }
      
      delete rh;
To create a control that resizes itself to fit the input text, use this call instead of specifying a fixed size:

    control->SetAllocatedArea( TGRect( TGPoint::GetOrigin(), control->GetPreferredSize() ) );
You can also use text controls in conjunction with other controls to create more complicated user interface elements--for example, enabling push buttons when the user enters text. See the manual Desktop Frameworks Concepts for general information on including controls in your applications. You can also use the UI Builder to build controls. See the manual UI Builder for the CommonPoint Application System for information on creating controls.


[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker