IndentedTextWriter 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供文字寫入器,可依索引卷標字串標記縮排新行。
public ref class IndentedTextWriter : System::IO::TextWriter
public class IndentedTextWriter : System.IO.TextWriter
type IndentedTextWriter = class
inherit TextWriter
Public Class IndentedTextWriter
Inherits TextWriter
- 繼承
範例
下列程式代碼範例示範如何使用 IndentedTextWriter 在不同的縮排層級撰寫文字。
#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 ref class Form1: public System::Windows::Forms::Form
{
private:
System::Windows::Forms::TextBox^ textBox1;
String^ CreateMultilevelIndentString()
{
// Creates a TextWriter to use as the base output writer.
System::IO::StringWriter^ baseTextWriter = gcnew 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 = gcnew 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();
}
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: {0}", 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( "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: {0}", level );
}
void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
textBox1->Text = CreateMultilevelIndentString();
}
public:
Form1()
{
System::Windows::Forms::Button^ button1 = gcnew System::Windows::Forms::Button;
this->textBox1 = gcnew 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 = "textBox1";
this->textBox1->Size = System::Drawing::Size( 391, 242 );
this->textBox1->TabIndex = 0;
this->textBox1->Text = "";
button1->Location = System::Drawing::Point( 11, 8 );
button1->Name = "button1";
button1->Size = System::Drawing::Size( 229, 23 );
button1->TabIndex = 1;
button1->Text = "Generate string using IndentedTextWriter";
button1->Click += gcnew 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 = "Form1";
this->Text = "IndentedTextWriter example";
this->ResumeLayout( false );
}
};
[STAThread]
int main()
{
Application::Run( gcnew Form1 );
}
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());
}
}
}
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
備註
IndentedTextWriter 藉由提供插入索引標籤字串並追蹤目前縮排層級的方法,來擴充 TextWriter。 使用多個縮排層級格式化的文字對於產生的程式代碼很有用,因此 CodeDOM 程式代碼產生器實作會使用此類別。
索引標籤字串是每個縮排所組成的字串。 索引標籤字串通常包含空格符。
注意
這個類別包含適用於所有成員之類別層級的連結需求和繼承需求。 當立即呼叫端或衍生類別沒有完全信任許可權時,就會擲回 SecurityException。 如需安全性需求的詳細資訊,請參閱 連結需求 和 繼承需求。
建構函式
IndentedTextWriter(TextWriter) |
使用指定的文字寫入器和預設索引標籤字串,初始化 IndentedTextWriter 類別的新實例。 |
IndentedTextWriter(TextWriter, String) |
使用指定的文字寫入器和索引標籤字串,初始化 IndentedTextWriter 類別的新實例。 |
欄位
CoreNewLine |
儲存用於這個 |
DefaultTabString |
指定預設索引標籤字串。 此欄位是常數。 |
屬性
Encoding |
取得要使用的文字寫入器編碼。 |
FormatProvider |
取得控制項格式設定的物件。 (繼承來源 TextWriter) |
Indent |
取得或設定要縮排的空格數目。 |
InnerWriter |
取得要使用的 TextWriter。 |
NewLine |
取得或設定要使用的新行字元。 |
方法
明確介面實作
IDisposable.Dispose() |
如需此成員的描述,請參閱 Dispose()。 (繼承來源 TextWriter) |
擴充方法
ConfigureAwait(IAsyncDisposable, Boolean) |
設定如何執行從異步可處置專案傳回的工作等候。 |