IndentedTextWriter Class
Assembly: System (in system.dll)
IndentedTextWriter extends a TextWriter by providing methods that insert a tab string and track the current indentation level. Text formatted with multiple indentation levels is useful for generated code, so this class is used by CodeDOM code generator implementations.
The tab string is the string that each indentation consists of. Typically the tab string contains white space.
Note: |
|---|
| This class contains a link demand and an inheritance demand at the class level that applies to all members. A SecurityException is thrown when either the immediate caller or the derived class does not have full-trust permission. For details about security demands, see Link Demands and Inheritance Demands. |
The following code example demonstrates using an IndentedTextWriter to write text at different levels of indentation.
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
package IndentedTextWriterExample;
import System.*;
import System.CodeDom.*;
import System.CodeDom.Compiler.*;
import System.ComponentModel.*;
import System.IO.*;
import System.Windows.Forms.*;
public class Form1 extends 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.set_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();
} //CreateMultilevelIndentString
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: "
+ System.Convert.ToString(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.set_Indent(indentWriter.get_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.set_Indent(indentWriter.get_Indent() - 1);
}
// Outputs a string using the WriteLineNoTabs method.
else {
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: "
+ System.Convert.ToString(level));
} //WriteLevel
private void button1_Click(Object sender, System.EventArgs e)
{
textBox1.set_Text(CreateMultilevelIndentString());
} //button1_Click
public Form1()
{
System.Windows.Forms.Button button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
this.textBox1.set_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.set_Location(new System.Drawing.Point(8, 40));
this.textBox1.set_Multiline(true);
this.textBox1.set_Name("textBox1");
this.textBox1.set_Size(new System.Drawing.Size(391, 242));
this.textBox1.set_TabIndex(0);
this.textBox1.set_Text("");
button1.set_Location(new System.Drawing.Point(11, 8));
button1.set_Name("button1");
button1.set_Size(new System.Drawing.Size(229, 23));
button1.set_TabIndex(1);
button1.set_Text("Generate string using IndentedTextWriter");
button1.add_Click(new System.EventHandler(this.button1_Click));
this.set_AutoScaleBaseSize(new System.Drawing.Size(5, 13));
this.set_ClientSize(new System.Drawing.Size(407, 287));
this.get_Controls().Add(button1);
this.get_Controls().Add(this.textBox1);
this.set_Name("Form1");
this.set_Text("IndentedTextWriter example");
this.ResumeLayout(false);
} //Form1
/** @attribute STAThread()
*/
public static void main(String[] args)
{
Application.Run(new Form1());
} //main
} //Form1
- SecurityAction.LinkDemand for full trust for the immediate caller. This class cannot be used by partially trusted code.
- SecurityAction.InheritanceDemand for full trust for inheritors. This class cannot be inherited by partially trusted code.
System.MarshalByRefObject
System.IO.TextWriter
System.CodeDom.Compiler.IndentedTextWriter
Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.
Note: