Turn user input into readable, formatted output
Many real-world apps need to take user input, process it, and display the results β often in multiple lines or structured format. Whether youβre building a calculator, form processor, or a report viewer, learning how to parse and format content is a key skill in WinForms development.
π§° What is Parsing?
Parsing is the process of converting a string input into usable data types like int, double, or DateTime.
π Common Parsing Methods
| Method | Use For |
|---|---|
int.Parse() | Whole numbers |
double.Parse() | Decimal numbers |
bool.Parse() | true / false strings |
DateTime.Parse() | Dates like 01/01/2025 |
TryParse() | Safer version (no crash) |
π Example: Parse and Display User Profile
Imagine you have 3 input fields: Name, Age, and Location.
π UI Controls
- 3
TextBoxes:txtName,txtAge,txtLocation - 1
Button:btnShowProfile - 1
TextBox:txtOutput(multiline = true, ReadOnly = true)
π§ Code Example
private void btnShowProfile_Click(object sender, EventArgs e)
{
string name = txtName.Text.Trim();
string location = txtLocation.Text.Trim();
int age;
if (int.TryParse(txtAge.Text, out age))
{
string profile = $"π€ User Profile\n";
profile += $"---------------------\n";
profile += $"Name: {name}\n";
profile += $"Age: {age}\n";
profile += $"Location: {location}\n";
profile += $"Joined: {DateTime.Now.ToShortDateString()}";
txtOutput.Text = profile;
}
else
{
MessageBox.Show("Please enter a valid number for Age.");
}
}
π Output Example
π€ User Profile
---------------------
Name: Alice
Age: 14
Location: Saltash
Joined: 20/07/2025
β
Clean, readable, and structured output using \n for line breaks.
π₯ Tip: Use a Multiline TextBox for Output
To allow multi-line display:
- Set
txtOutput.Multiline = true - Optionally set:
ScrollBars = VerticalReadOnly = trueFont = Consolas,Size = 10(for terminal-style output)
π Displaying Lists
Want to show a list of names entered into a TextBox, separated by commas or lines?
private void btnSplitNames_Click(object sender, EventArgs e)
{
string input = txtNames.Text;
string[] names = input.Split(',');
txtOutput.Text = "Names Entered:\n";
foreach (string name in names)
{
txtOutput.AppendText("- " + name.Trim() + "\n");
}
}
π‘ Use AppendText() for better performance in long outputs.
π§ͺ Quick Challenge
π§© Build a βProfile Summaryβ form:
- Inputs: Name, Age, Favorite Food
- Output: A formatted paragraph with line breaks:
Hello Sarah! You are 12 years old and love pizza. Welcome to our app!
π Summary
| Feature | Description |
|---|---|
Parse() | Converts strings to data types (may crash) |
TryParse() | Safer parsing β avoids exceptions |
\n | Line break inside string |
Multiline TextBox | Display multiple lines of output |
β Best Practices
- β
Always validate input using
TryParse - β Trim inputs to remove extra spaces
- β
Use
StringBuilderfor large, dynamic text - β Display data with line breaks for clarity
- β Catch parse errors and inform the user
π Want to Go Further?
- Format results with
string interpolationorstring.Format() - Use
List<string>or arrays for structured parsing - Read input from files and display results in your form
- Output to
RichTextBoxwith formatting and colors
π¬ Want help building a summary generator, report viewer, or results display in WinForms, we can train you.
Send me your form idea β Iβll help wire up the parsing and output.