double d1 = double.Parse(TextBox1.Text.Trim() == "" ? 0 : TextBox1.Text);
double d2 = double.Parse(TextBox2.Text.Trim() == "" ? 0 : extBox2.Text);
double d = d1 + d2;
MessageBox.Show(d.ToString());
double d2 = double.Parse(TextBox2.Text.Trim() == "" ? 0 : extBox2.Text);
double d = d1 + d2;
MessageBox.Show(d.ToString());
{
double d1,d2;
double.TryParse(textBox1.Text,out d1);
double.TryParse(textBox2.Text,out d2);
double d = d1 + d2;
MessageBox.Show(d.ToString());
}
private void button1_Click(object sender, EventArgs e)
{ double d1;
try
{
d1 = double.Parse(textBox1.Text);
}
catch
{
d1 = 0;
}
double d2;
try
{
d2 = double.Parse(textBox2.Text);
}
catch
{
d2 = 0;
} double d = d1 + d2; MessageBox.Show(d.ToString());
}
double a = 0, b = 0, c = 0;
double.TryParse(textBox1.Text, out a);
double.TryParse(textBox2.Text, out b);
c = a + b;
MessageBox.Show(c.ToString());
double d2=0;
if(TextBox1.Text.Trim()==""
d1=0;
else
d1 = double.Parse(TextBox1.Text.Trim());
if(TextBox2.Text.Trim()==""
d2=0;
else
d2 = double.Parse(TextBox2.Text.Trim());
double d = d1 + d2;
MessageBox.Show(d.ToString());
书上有:
http://www.china-pub.com/195430&ref=xilie
http://www.china-pub.com/195661&ref=xilie
呵呵,应该这样
private void button9_Click(object sender, EventArgs e)
{
double d1,d2;
if (!double.TryParse(textBox1.Text, out d1)) { MessageBox.Show("text1 不是有效数据"); textBox1.Focus(); return; }
if (!double.TryParse(textBox2.Text, out d2)) { MessageBox.Show("text2 不是有效数据"); textBox2.Focus(); return; };
double d = d1 + d2;
MessageBox.Show(d.ToString());
}
各个textBox的Validated事件,指定下面方法.
private void textBox_Validated(object sender, EventArgs e)
{
double d0;
if (((TextBox)sender).Text.Trim() != "" && !double.TryParse(((TextBox)sender).Text,out d0))
{
MessageBox.Show("数据格式无效");((TextBox)sender).Focus();
}
}
//
private void button9_Click(object sender, EventArgs e)
{
double d1,d2;
double.TryParse(textBox1.Text,out d1);
double.TryParse(textBox2.Text,out d2);
double d = d1 + d2;
MessageBox.Show(d.ToString());
}
{
double d1,d2;
double.TryParse(textBox1.Text,out d1);
double.TryParse(textBox2.Text,out d2);
double d = d1 + d2;
MessageBox.Show(d.ToString());
}
用下面这个,不管你多少个都行 private void button1_Click(object sender, EventArgs e)
{
double dbResulet = 0;
double tempDouble = 0;
foreach (Control tb in this.Controls)
{
if (tb is System.Windows.Forms.TextBox)
{
double.TryParse(((TextBox)tb).Text, out tempDouble);
dbResulet += tempDouble;
}
}
MessageBox.Show(dbResulet.ToString());
}