看到很多网友写的博客或发表文章(关于编程方面的),很是佩服啊!他们展示的不仅是能力,更多的是给那些求知者(还有像我这样的菜鸟级的)提供了重要的帮助或指点。
因为我是一个初级入门者(在C#方面),给不了大家技术上什么太大的帮助,不过我想说的是,既然我们在网络上发表我们的论述或在某一方面或者说某一领域的见解和总结,其结果就是为了让更多的人去了解,去明白自己的见解,也是为了解人之所惑;对于程序员来说除了技术,编程思想也很重要,所以希望各位大虾在解人之惑时,能够把程序更加简洁明了。因为我在写程序时,上网搜索疑问时遇到的一些情况,就拿我写的一个简易计算器举个示例吧:
就拿计算器0-9举个例子吧
单击0-9数字按钮的代码方法一:
private void Button(object sender, EventArgs e)
{
Button btn = sender as Button;
textBox1.Text += btn.Text;
}
单击0-9数字按钮的代码方法二:
private void Button(object sender, EventArgs e)
{
textBox1.Text += Button1.Text;
textBox1.Text += Button2.Text;
textBox1.Text += Button3.Text;
textBox1.Text += Button4.Text;
textBox1.Text += Button5.Text;
textBox1.Text += Button6.Text;
........
}
虽说效果一样,但这两种写法给人的感觉肯定不一样!!!这就是我想说的。
下面是我写的计算器代码,供大家参考,高手要发现什么不适或更好的写法,请多多指教,Thank you!:
namespace Counter
{
public partial class Form1 : Form
{
string _operStr = "";// 操作符
double _dFirst = 0;
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 构造按钮的单击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button(object sender, EventArgs e)
{
var Fvalue = textBox1.Text;
var Newvalue = (sender as Button).Text;
textBox1.Text = Convert.ToDouble(Fvalue + Newvalue).ToString();
}
/// <summary>
/// 构造操作运算符的单击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Operator(object sender, EventArgs e)
{
_operStr = (sender as Button).Text;// 记录操作符,+,-,*,/,%
//Button bt = sender as Button;
//_operStr = bt.Text;
_dFirst = Convert.ToDouble(textBox1.Text);
textBox1.Text = "";
}
/// <summary>
/// 点击等号事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnEqual_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(_operStr)) return;
switch (_operStr)
{
case "+":
textBox1.Text = (_dFirst + Convert.ToDouble(textBox1.Text)).ToString();
break;
case "-":
textBox1.Text = (_dFirst - Convert.ToDouble(textBox1.Text)).ToString();
break;
case"*":
textBox1.Text = (_dFirst * Convert.ToDouble(textBox1.Text)).ToString();
break;
case"/":
textBox1.Text = (_dFirst / Convert.ToDouble(textBox1.Text)).ToString();
break;
}
}
/// <summary>
/// 使小数点只出现一次
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnDot_Click(object sender, EventArgs e)
{
if (textBox1.Text.Contains(".")) return;
textBox1.Text += (sender as Button).Text;
}
/// <summary>
/// 退格
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnQuit_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length > 0)
{
textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
}
}
/// <summary>
/// 清空
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnClear_Click(object sender, EventArgs e)
{
textBox1.Text = "0";
}
/// <summary>
/// 百分号
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnPercent_Click(object sender, EventArgs e)
{
string s = textBox1.Text;
double a_Per=Convert.ToDouble(s);
double b_Per = a_Per / 100.0;
textBox1.Text = b_Per.ToString();
}
}
}
第一次写博客,基本是有啥说啥,大家要觉得有啥说的不对或不好的地方请指正,先谢过了!!!
转载于:https://www.cnblogs.com/leeice/archive/2012/03/09/2387783.html