次の方法で共有


IndentedTextWriter クラス

タブ文字列トークンを使用して、新しい行にインデントを設定できるテキスト ライタを提供します。

この型のすべてのメンバの一覧については、IndentedTextWriter メンバ を参照してください。

System.Object
   System.MarshalByRefObject
      System.IO.TextWriter
         System.CodeDom.Compiler.IndentedTextWriter

Public Class IndentedTextWriter
   Inherits TextWriter
[C#]
public class IndentedTextWriter : TextWriter
[C++]
public __gc class IndentedTextWriter : public TextWriter
[JScript]
public class IndentedTextWriter extends TextWriter

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

IndentedTextWriter は、タブ文字列を挿入して現在のインデント レベルを追跡するメソッドを提供することによって、 TextWriter を拡張しています。複数のインデント レベルを使用して書式指定されたテキストは、生成されたコードで使用すると便利です。そのため、このクラスは CodeDOM コード ジェネレータの実装で使用されます。

タブ文字列は各インデントを構成する文字列です。通常、タブ文字列は空白を含みます。

使用例

[Visual Basic, C#, C++] IndentedTextWriter を使用して、複数の異なるインデント幅でテキストを入力するコード例を次に示します。

 
Imports System
Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports System.ComponentModel
Imports System.IO
Imports System.Windows.Forms

Public Class Form1
   Inherits System.Windows.Forms.Form
   Private textBox1 As System.Windows.Forms.TextBox 
   
   Private Function CreateMultilevelIndentString() As String
        ' Create a TextWriter to use as the base output writer.
        Dim baseTextWriter As New System.IO.StringWriter
      
        ' Create an IndentedTextWriter and set the tab string to use 
        ' as the indentation string for each indentation level.
        Dim indentWriter = New IndentedTextWriter(baseTextWriter, "    ")

        ' Set the indentation level.
        indentWriter.Indent = 0

        ' Output test strings at stepped indentations through a recursive loop method.
        WriteLevel(indentWriter, 0, 5)
      
        ' Return the resulting string from the base StringWriter.
        Return baseTextWriter.ToString()
    End Function

    Private Sub WriteLevel(ByVal indentWriter As IndentedTextWriter, ByVal level As Integer, ByVal totalLevels As Integer)
        ' Outputs a test string with a new-line character at the end.
        indentWriter.WriteLine(("This is a test phrase. Current indentation level: " + level.ToString()))

        ' If not yet at the highest recursion level, call this output method for the next level of indentation.
        If level < totalLevels Then
            ' Increase the indentation count for the next level of indented output.
            indentWriter.Indent += 1

            ' Call the WriteLevel method to write test output for the next level of indentation.
            WriteLevel(indentWriter, level + 1, totalLevels)

            ' Restores the indentation count for this level after the recursive branch method has returned.
            indentWriter.Indent -= 1

        Else
            ' Output a string using the WriteLineNoTabs method.
            indentWriter.WriteLineNoTabs("This is a test phrase written with the IndentTextWriter.WriteLineNoTabs method.")
        End If

        ' Outputs a test string with a new-line character at the end.
        indentWriter.WriteLine(("This is a test phrase. Current indentation level: " + level.ToString()))
    End Sub

    Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        textBox1.Text = CreateMultilevelIndentString()
    End Sub

    Public Sub New()
        Dim button1 As New System.Windows.Forms.Button
        Me.textBox1 = New System.Windows.Forms.TextBox
        Me.SuspendLayout()
        Me.textBox1.Anchor = CType(System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left Or System.Windows.Forms.AnchorStyles.Right, System.Windows.Forms.AnchorStyles)
        Me.textBox1.Location = New System.Drawing.Point(8, 40)
        Me.textBox1.Multiline = True
        Me.textBox1.Name = "textBox1"
        Me.textBox1.Size = New System.Drawing.Size(391, 242)
        Me.textBox1.TabIndex = 0
        Me.textBox1.Text = ""
        button1.Location = New System.Drawing.Point(11, 8)
        button1.Name = "button1"
        button1.Size = New System.Drawing.Size(229, 23)
        button1.TabIndex = 1
        button1.Text = "Generate string using IndentedTextWriter"
        AddHandler button1.Click, AddressOf Me.button1_Click
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(407, 287)
        Me.Controls.Add(button1)
        Me.Controls.Add(Me.textBox1)
        Me.Name = "Form1"
        Me.Text = "IndentedTextWriter example"
        Me.ResumeLayout(False)
    End Sub

    <STAThread()> _
    Shared Sub Main()
        Application.Run(New Form1)
    End Sub
End Class

[C#] 
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;

namespace IndentedTextWriterExample
{
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.TextBox textBox1;

        private string CreateMultilevelIndentString()
        {
            // Creates a TextWriter to use as the base output writer.
            System.IO.StringWriter baseTextWriter = new System.IO.StringWriter();            

            // Create an IndentedTextWriter and set the tab string to use 
            // as the indentation string for each indentation level.
            System.CodeDom.Compiler.IndentedTextWriter indentWriter = new IndentedTextWriter(baseTextWriter, "    ");           

            // Sets the indentation level.
            indentWriter.Indent = 0;

            // Output test strings at stepped indentations through a recursive loop method.
            WriteLevel(indentWriter, 0, 5);

            // Return the resulting string from the base StringWriter.
            return baseTextWriter.ToString();
        }

        private void WriteLevel(IndentedTextWriter indentWriter, int level, int totalLevels)
        {
            // Output a test string with a new-line character at the end.
            indentWriter.WriteLine("This is a test phrase. Current indentation level: "+level.ToString());
            
            // If not yet at the highest recursion level, call this output method for the next level of indentation.
            if( level < totalLevels )
            {
                // Increase the indentation count for the next level of indented output.
                indentWriter.Indent++;

                // Call the WriteLevel method to write test output for the next level of indentation.
                WriteLevel(indentWriter, level+1, totalLevels);

                // Restores the indentation count for this level after the recursive branch method has returned.
                indentWriter.Indent--;
            }
            else
                // Outputs a string using the WriteLineNoTabs method.
                indentWriter.WriteLineNoTabs("This is a test phrase written with the IndentTextWriter.WriteLineNoTabs method.");

            // Outputs a test string with a new-line character at the end.
            indentWriter.WriteLine("This is a test phrase. Current indentation level: "+level.ToString());           
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            textBox1.Text = CreateMultilevelIndentString();
        }

        public Form1()
        {
            System.Windows.Forms.Button button1 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();            
            this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
                | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right)));
            this.textBox1.Location = new System.Drawing.Point(8, 40);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(391, 242);
            this.textBox1.TabIndex = 0;
            this.textBox1.Text = "";
            button1.Location = new System.Drawing.Point(11, 8);
            button1.Name = "button1";
            button1.Size = new System.Drawing.Size(229, 23);
            button1.TabIndex = 1;
            button1.Text = "Generate string using IndentedTextWriter";
            button1.Click += new System.EventHandler(this.button1_Click);
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(407, 287);
            this.Controls.Add(button1);
            this.Controls.Add(this.textBox1);
            this.Name = "Form1";
            this.Text = "IndentedTextWriter example";
            this.ResumeLayout(false);        
        }

        [STAThread]
        static void Main() 
        {
            Application.Run(new Form1());
        }
    }
}

[C++] 
#using <mscorlib.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
#using <System.dll>
using namespace System;
using namespace System::CodeDom;
using namespace System::CodeDom::Compiler;
using namespace System::ComponentModel;
using namespace System::IO;
using namespace System::Windows::Forms;

public __gc class Form1 : public System::Windows::Forms::Form
{
private:
   System::Windows::Forms::TextBox* textBox1;

private:
   String* CreateMultilevelIndentString()
   {
      // Creates a TextWriter to use as the base output writer.
      System::IO::StringWriter* baseTextWriter = new System::IO::StringWriter();            

      // Create an IndentedTextWriter and set the tab string to use 
      // as the indentation string for each indentation level.
      System::CodeDom::Compiler::IndentedTextWriter* indentWriter = new IndentedTextWriter(baseTextWriter, S"    ");           

      // Sets the indentation level.
      indentWriter->Indent = 0;

      // Output test strings at stepped indentations through a recursive loop method.
      WriteLevel(indentWriter, 0, 5);

      // Return the resulting string from the base StringWriter.
      return baseTextWriter->ToString();
   }

private:
   void WriteLevel(IndentedTextWriter* indentWriter, int level, int totalLevels)
   {
      // Output a test string with a new-line character at the end.
      indentWriter->WriteLine(S"This is a test phrase. Current indentation level: {0}",__box(level));

      // If not yet at the highest recursion level, call this output method for the next level of indentation.
      if( level < totalLevels )
      {
         // Increase the indentation count for the next level of indented output.
         indentWriter->Indent++;

         // Call the WriteLevel method to write test output for the next level of indentation.
         WriteLevel(indentWriter, level+1, totalLevels);

         // Restores the indentation count for this level after the recursive branch method has returned.
         indentWriter->Indent--;
      }
      else
         // Outputs a string using the WriteLineNoTabs method.
         indentWriter->WriteLineNoTabs(S"This is a test phrase written with the IndentTextWriter.WriteLineNoTabs method.");

      // Outputs a test string with a new-line character at the end.
      indentWriter->WriteLine(S"This is a test phrase. Current indentation level: {0}",__box(level));           
   }

private:
   void button1_Click(Object* /*sender*/, System::EventArgs* /*e*/)
   {
      textBox1->Text = CreateMultilevelIndentString();
   }

public:
   Form1()
   {
      System::Windows::Forms::Button* button1 = new System::Windows::Forms::Button();
      this->textBox1 = new System::Windows::Forms::TextBox();
      this->SuspendLayout();            
      this->textBox1->Anchor = (System::Windows::Forms::AnchorStyles)(
         System::Windows::Forms::AnchorStyles::Top
         | System::Windows::Forms::AnchorStyles::Bottom 
         | System::Windows::Forms::AnchorStyles::Left 
         | System::Windows::Forms::AnchorStyles::Right);
      this->textBox1->Location = System::Drawing::Point(8, 40);
      this->textBox1->Multiline = true;
      this->textBox1->Name = S"textBox1";
      this->textBox1->Size = System::Drawing::Size(391, 242);
      this->textBox1->TabIndex = 0;
      this->textBox1->Text = S"";
      button1->Location = System::Drawing::Point(11, 8);
      button1->Name = S"button1";
      button1->Size = System::Drawing::Size(229, 23);
      button1->TabIndex = 1;
      button1->Text = S"Generate string using IndentedTextWriter";
      button1->Click += new System::EventHandler(this, &Form1::button1_Click);
      this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
      this->ClientSize = System::Drawing::Size(407, 287);
      this->Controls->Add(button1);
      this->Controls->Add(this->textBox1);
      this->Name = S"Form1";
      this->Text = S"IndentedTextWriter example";
      this->ResumeLayout(false);        
   }

};

[STAThread]
int main() 
{
   Application::Run(new Form1());
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.CodeDom.Compiler

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

アセンブリ: System (System.dll 内)

参照

IndentedTextWriter メンバ | System.CodeDom.Compiler 名前空間