C
C#5w ago
Lorenzo

Format text in file - Windows Forms

I'm creating a class creation automator, basically a Windows Forms that has components like the class name, and the properties that the user wants to insert, when he clicks on add class, a savefiledialog opens. The problem is that I need help formatting the text in the class file, I tested with String builder but it didn't work well, because I can't indent the keys, or can I? I'm a layman on the subject so any help or path to follow will help a lot. Thank you.
No description
5 Replies
Lorenzo
LorenzoOP5w ago
No description
jcotton42
jcotton425w ago
Well, this is entirely a case of unnecessary whitespace before internal, that first {, and a spurious closing brace. (Or did you mean to use block-scoped namespaces, instead of the newer and better file-scoped namespaces)? If you intend to do anything nontrivial with this, I would suggest writing a class that wraps StringBuilder and tracks the indent level. You'd have a StartBlock method that bumps the indent level, and an EndBlock that decreases it. Then your various Append methods would insert the number of spaces (or tabs) corresponding to that indent level before inserting the text you passed in.
Lorenzo
LorenzoOP5w ago
Okay, I'm kind of a layman on this subject, can you send me a website or a video that explains this better? Or if it's not, a technical term for me to look up, can I see a step by step? Thanks!
jcotton42
jcotton425w ago
something like
public class IndentingStringBuilder
{
private readonly StringBuilder builder = new StringBuilder();
private int indent = 0;

public IndentingStringBuilder StartBlock()
{
indent++;
return this;
}

public IndentingStringBuilder EndBlock()
{
indent--;
return this;
}

public IndentingStringBuilder AppendLine(string line)
{
builder.Append(' ', indent * 4).AppendLine(line);
return this;
}

// etc.

public override string ToString()
{
return builder.ToString();
}
}
public class IndentingStringBuilder
{
private readonly StringBuilder builder = new StringBuilder();
private int indent = 0;

public IndentingStringBuilder StartBlock()
{
indent++;
return this;
}

public IndentingStringBuilder EndBlock()
{
indent--;
return this;
}

public IndentingStringBuilder AppendLine(string line)
{
builder.Append(' ', indent * 4).AppendLine(line);
return this;
}

// etc.

public override string ToString()
{
return builder.ToString();
}
}
which you can use like
IndentingStringBuilder isb = new IndentingStringBuilder();
isb.AppendLine("namespace Foo;");
isb.AppendLine("public class MyClass");
isb.AppendLine("{");
isb.StartBlock();
isb.AppendLine("public string S { get; set; }");
isb.EndBlock();
isb.AppendLine("}");

Console.WriteLine(isb.ToString());
IndentingStringBuilder isb = new IndentingStringBuilder();
isb.AppendLine("namespace Foo;");
isb.AppendLine("public class MyClass");
isb.AppendLine("{");
isb.StartBlock();
isb.AppendLine("public string S { get; set; }");
isb.EndBlock();
isb.AppendLine("}");

Console.WriteLine(isb.ToString());
to get
namespace Foo;
public class MyClass
{
public string S { get; set; }
}
namespace Foo;
public class MyClass
{
public string S { get; set; }
}
@Lorenzo Optionally, you could put the writing of { and } into StartBlock and EndBlock.
Lorenzo
LorenzoOP5w ago
Ok thanks for the feedback and for your patience!

Did you find this page helpful?