我正在C#和Win窗体中使用VS 2012构建非常基本的BMI计算器,我也是C#的新手。我跟着一些例子,这段代码应该可以工作,但是当运行代码时,我会遇到这些错误。无法隐式转换字符串 - C#
Error 3 Argument 1: cannot convert from 'System.Windows.Forms.TextBox' to 'string' c:\users\dell\documents\visual studio 2012\Projects\bmi_calc\bmi_calc\Form1.cs 44 31 bmi_calc
Error 5 Argument 1: cannot convert from 'System.Windows.Forms.TextBox' to 'string' c:\users\dell\documents\visual studio 2012\Projects\bmi_calc\bmi_calc\Form1.cs 45 31 bmi_calc
Error 1 Cannot implicitly convert type 'string' to 'System.Windows.Forms.TextBox' c:\users\dell\documents\visual studio 2012\Projects\bmi_calc\bmi_calc\Form1.cs 39 25 bmi_calc
Error 2 The best overloaded method match for 'double.Parse(string)' has some invalid arguments c:\users\dell\documents\visual studio 2012\Projects\bmi_calc\bmi_calc\Form1.cs 44 17 bmi_calc
Error 4 The best overloaded method match for 'double.Parse(string)' has some invalid arguments c:\users\dell\documents\visual studio 2012\Projects\bmi_calc\bmi_calc\Form1.cs 45 17 bmi_calc
这里是我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace bmi_calc
{
public partial class Form1 : Form
{
double v;
double t;
double r;
public Form1()
{
InitializeComponent();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
txtTezina.Clear(); //Btn that resets height and weight field values.
txtVisina.Clear();
txtBmiRez = "";
}
private void button1_Click(object sender, EventArgs e)
{
v = Double.Parse (txtVisina);
t = Double.Parse (txtTezina);
r = t/(v * v);
txtBmiRez.Text = String.Format("{0:f}", r);
}
private void button3_Click(object sender, EventArgs e)
{
Application.Exit(); // Close app
}
}
}
如果有人能解释我这一点,我将永远感激。
你应该使用文本属性。如果来自vb6/vba,则没有隐式文本属性的概念。 –
'v = Double.Parse(txtVisina.Text);''t = Double.Parse(txtTezina.Text);' – Innat3