StringFormat::SetTabStops Method (Single, array<Single>^)

 

Sets tab stops for this StringFormat object.

Namespace:   System.Drawing
Assembly:  System.Drawing (in System.Drawing.dll)

public:
void SetTabStops(
	float firstTabOffset,
	array<float>^ tabStops
)

Parameters

firstTabOffset
Type: System::Single

The number of spaces between the beginning of a line of text and the first tab stop.

tabStops
Type: array<System::Single>^

An array of distances between tab stops in the units specified by the Graphics::PageUnit property.

Each tab-stop offset in the tabStops array, except the first one, is relative to the previous one. The first tab-stop offset is relative to the initial offset position specified by firstTabOffset. For example, if the initial offset position is 8 and the first tab-stop offset is 50, then the first tab stop is at position 58. If the initial offset position is zero, then the first tab-stop offset is relative to position 0, the string origin.

The following example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. The code performs the following actions:

  • Sets the tab stops of the StringFormat.

  • Draws the string and the layout rectangle. Note that the string contains tabs. The tab settings of the StringFormat specify the offsets of the tabbed text.

  • Gets the tab stops and uses or inspects the values.

void GetSetTabStopsExample2( PaintEventArgs^ e )
{
   Graphics^ g = e->Graphics;

   // Tools used for drawing, painting.
   Pen^ redPen = gcnew Pen( Color::FromArgb( 255, 255, 0, 0 ) );
   SolidBrush^ blueBrush = gcnew SolidBrush( Color::FromArgb( 255, 0, 0, 255 ) );

   // Layout and format for text.
   System::Drawing::Font^ myFont = gcnew System::Drawing::Font( "Times New Roman",12 );
   StringFormat^ myStringFormat = gcnew StringFormat;
   Rectangle enclosingRectangle = Rectangle(20,20,500,100);
   array<Single>^tabStops = {150.0f,100.0f,100.0f};

   // Text with tabbed columns.
   String^ myString = "Name\tTab 1\tTab 2\tTab 3\nGeorge Brown\tOne\tTwo\tThree";

   // Set the tab stops, paint the text specified by myString, draw the
   // rectangle that encloses the text.
   myStringFormat->SetTabStops( 0.0f, tabStops );
   g->DrawString( myString, myFont, blueBrush, enclosingRectangle, myStringFormat );
   g->DrawRectangle( redPen, enclosingRectangle );

   // Get the tab stops.
   float firstTabOffset;
   array<Single>^tabStopsObtained = myStringFormat->GetTabStops( firstTabOffset );
   for ( int j = 0; j < tabStopsObtained->Length; j++ )
   {
      // Inspect or use the value in tabStopsObtained[j].
      Console::WriteLine( "\n  Tab stop {0} = {1}", j, tabStopsObtained[ j ] );
   }
}

.NET Framework
Available since 1.1
Return to top
Show: