StringFormat.SetTabStops(Single, Single[]) 方法

定義

設定這個 StringFormat 物件的定位停駐點。

public:
 void SetTabStops(float firstTabOffset, cli::array <float> ^ tabStops);
public void SetTabStops (float firstTabOffset, float[] tabStops);
member this.SetTabStops : single * single[] -> unit
Public Sub SetTabStops (firstTabOffset As Single, tabStops As Single())

參數

firstTabOffset
Single

文字行的開頭到第一個定位停駐點的空格數目。

tabStops
Single[]

定位停駐點的間距陣列 (以 PageUnit 屬性所指定的單位表示)。

範例

下列範例是設計來搭配 Windows Forms 使用,而且需要 PaintEventArgse,這是事件處理程序的參數Paint。 此程式碼會執行下列動作:

  • 設定的 StringFormat製表位。

  • 繪製字串和版面配置矩形。 請注意,字串包含索引標籤。 的 StringFormat 索引標籤設定會指定索引標籤文字的位移。

  • 取得製表位,並使用或檢查值。

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 ] );
   }
}
public void GetSetTabStopsExample2(PaintEventArgs e)
{
    Graphics     g = e.Graphics;
             
    // Tools used for drawing, painting.
    Pen          redPen = new Pen(Color.FromArgb(255, 255, 0, 0));
    SolidBrush   blueBrush = new SolidBrush(Color.FromArgb(255, 0, 0, 255));
             
    // Layout and format for text.
    Font         myFont = new Font("Times New Roman", 12);
    StringFormat myStringFormat = new StringFormat();
    Rectangle    enclosingRectangle = new Rectangle(20, 20, 500, 100);
    float[]      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;
    float[] tabStopsObtained = myStringFormat.GetTabStops(out 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]);
    }
}
Public Sub GetSetTabStopsExample2(ByVal e As PaintEventArgs)
    Dim g As Graphics = e.Graphics

    ' Tools used for drawing, painting.
    Dim redPen As New Pen(Color.FromArgb(255, 255, 0, 0))
    Dim blueBrush As New SolidBrush(Color.FromArgb(255, 0, 0, 255))

    ' Layout and format for text.
    Dim myFont As New Font("Times New Roman", 12)
    Dim myStringFormat As New StringFormat
    Dim enclosingRectangle As New Rectangle(20, 20, 500, 100)
    Dim tabStops As Single() = {150.0F, 100.0F, 100.0F}

    ' Text with tabbed columns.
    Dim myString As String = "Name" & ControlChars.Tab & "Tab 1" _
    & ControlChars.Tab & "Tab 2" & ControlChars.Tab & "Tab 3" _
    & ControlChars.Cr & "George Brown" & ControlChars.Tab & "One" _
    & ControlChars.Tab & "Two" & ControlChars.Tab & "Three"

    ' Set the tab stops, paint the text specified by myString,
    ' and draw rectangle that encloses the text.
    myStringFormat.SetTabStops(0.0F, tabStops)
    g.DrawString(myString, myFont, blueBrush, _
    RectangleF.op_Implicit(enclosingRectangle), myStringFormat)
    g.DrawRectangle(redPen, enclosingRectangle)

    ' Get the tab stops.
    Dim firstTabOffset As Single
    Dim tabStopsObtained As Single() = _
    myStringFormat.GetTabStops(firstTabOffset)
    Dim j As Integer
    For j = 0 To tabStopsObtained.Length - 1

        ' Inspect or use the value in tabStopsObtained[j].
        Console.WriteLine(ControlChars.Cr & "  Tab stop {0} = {1}", _
        j, tabStopsObtained(j))
    Next j
End Sub

備註

陣列中的每個 tabStops 製表位位移,但第一個位移除外,相對於前一個位移。 第一個製表位位移相對於 所 firstTabOffset指定的初始位移位置。 例如,如果初始位移位置為8,而第一個製表位位移為50,則第一個製表位點位於位置58。 如果初始位移位置為零,則第一個製表位位移相對於位置 0,即字串原點。

適用於