无法隐式转换字符串 - C#

时间:2016-11-18 13:19:48

标签: c# winforms

我正在使用VS 2012在C#和Win Forms中构建非常基本的BMI计算器,我也是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
        }

    }
}

如果有人能解释我,我会永远感激。

5 个答案:

答案 0 :(得分:5)

v = Double.Parse (txtVisina.Text); txtVisina都是txtTezina个对象,而不是其中包含的字符串。您需要使用来自这些对象的TextBox属性来访问用户界面中的字符串值。

例如:

.Text

v = Double.Parse (txtVisina.Text);
t = Double.Parse (txtTezina.Text);

有趣的是,您对txtBmiRez.Text = ""; 的第二次使用实际上是正确的。

当解析双精度(或字符串中的任何对象)时,还建议使用txtBmiRez来处理任何可能的错误。如果字符串不是有效数字,TryParse将抛出异常,而Parse将返回TryParse。例如,将Click方法更改为类似的方法是有利的,并且可以减少任何崩溃的可能性:

false

答案 1 :(得分:2)

变量txtVisinatxtTezinaTextBoxes,但您尝试将它们用作字符串。您需要使用的只是他们的Text属性,即

v = Double.Parse (txtVisina.Text);
t = Double.Parse (txtTezina.Text);

答案 2 :(得分:1)

您需要访问Text的{​​{1}}属性才能获取其文字。你正试图自己投射文本框。

例如,而不是

TextBox

以下是正确的:

v = Double.Parse (txtVisina);

答案 3 :(得分:1)

您需要使用Textbox.Text属性来获取输入到文本框的值,例如

string enteredValue = textbox1.Text;

当您开始使用WinForms时,很容易犯错误。我做了很多!

答案 4 :(得分:0)

对于错误3和5,您必须使用TextBox的.Text属性。

要将任何字符串分配给文本框,您必须使用以下语法:

textboxid.Text = “”; 如果要为其分配任何非字符串值,则使用:.ToString()函数将变量转换为字符串,然后再将其分配给文本框。 例如 int i = 10; Textbox1.Text = i.ToString();