Make your WinForms methods powerful and flexible
Once you start creating your own methods, the next step is making them dynamic β by passing in arguments (parameters) and getting back return values.
This makes your code reusable and lets you separate logic from interface code β a core principle of clean app development.
π§° Why Use Parameters and Return Values?
- π Arguments let methods work on different data
- π Return values let methods give results back
- β Keeps your UI event handlers short and smart
π· Whatβs an Argument?
An argument (or parameter) is a value you pass into a method so it can work on it.
private void ShowGreeting(string name)
{
labelResult.Text = $"Hello, {name}!";
}
Call it from a button click:
string user = txtName.Text;
ShowGreeting(user);
β
Now ShowGreeting() works for any name you pass in.
π§± Multiple Parameters
private void ShowProfile(string name, int age)
{
labelResult.Text = $"Name: {name}, Age: {age}";
}
Then:
ShowProfile("Emma", 25);
Or dynamically:
string user = txtName.Text;
int age = int.Parse(txtAge.Text);
ShowProfile(user, age);
π Whatβs a Return Value?
You can create a method that calculates something and returns it.
private int Add(int a, int b)
{
return a + b;
}
Then:
int result = Add(5, 3);
labelAnswer.Text = $"Result: {result}";
β The method does the work, then gives back the result.
π‘ Example: Total Calculator
private double CalculateTotal(double price, int quantity)
{
return price * quantity;
}
Use it like:
double price = double.Parse(txtPrice.Text);
int qty = int.Parse(txtQuantity.Text);
double total = CalculateTotal(price, qty);
lblTotal.Text = $"Β£{total}";
β οΈ Return Types Must Match
This will cause an error:
private string AddNumbers(int a, int b)
{
return a + b; // β Type mismatch β returning int, not string
}
Fix it with conversion:
private string AddNumbers(int a, int b)
{
return (a + b).ToString();
}
π You Can Return bool, Too
private bool IsAdult(int age)
{
return age >= 18;
}
Then:
int age = int.Parse(txtAge.Text);
if (IsAdult(age))
label1.Text = "You are an adult.";
else
label1.Text = "You are under 18.";
π§ͺ Quick Challenge
π§© Create a form with:
- 2
TextBoxcontrols: Number 1, Number 2 - 1
Button: Calculate - 1
Label: Show result
Write a method:
private int AddNumbers(int a, int b)
Call it from the Button.Click event and show the result in the label.
π Summary
| Concept | Description |
|---|---|
| Argument | Value passed into a method |
| Return Value | Result sent back by a method |
void | Use when method doesnβt return anything |
| Return types | Can be int, string, bool, double, etc. |
β Best Practices
- β
Give clear, specific method names:
CalculateTotal(),IsValidName() - β Keep your methods short and focused on one task
- β
Validate inputs before parsing (
TryParse) - β Use return values instead of repeating logic inside events
π Want to Go Further?
- Use
refandoutto return multiple values - Combine logic and validation into returnable helper methods
- Create your own utility classes to organize methods
- Explore method overloading (same method name, different parameters)
π¬ Need help turning your UI logic into clean, reusable methods?
Send a screenshot or snippet β Iβll help you modularize and simplify it or join our WinForms C# Course to learn more.