Project 檔案
Winform 畫面
把 Relpicate() 函數寫成擴充事件來使用
namespace NumericUpDown
{
public static class ExtChar
{
public static string Replicate(this char value , int count)
{
// 輸入負值或 0,就把 char 回傳
count = Math.Max(count,1);
return new string(value,count);
}
}
}
C# Codenamespace NumericUpDown
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnRun_Click(object sender, EventArgs e)
{
// numericUpDonw.Value property 回傳值是 decimal
int count = (int)numericUpDown1.Value;
// 輸入 0 或負值,就跑一次就好
count = Math.Max(count, 1);
char sign = '*' ;
lblResult.Text = string.Empty;
for (int i = 1; i <= count; i++)
{
lblResult.Text += string.Concat(sign.Replicate(i), Environment.NewLine);
}
}
}
}
執行結果一個突發奇想,讓自己的函式庫內又多一個擴充事件,^^
- 參考資料
- 小山的 C# 教學-第13課-NumericUpDown
- Best way to repeat a character in C#
- ASP.NET 3.5 圖表與實務案例模組大全 P14-8



沒有留言:
張貼留言