2016-11-18 88 views
2

我正在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 
     } 

    } 
} 

如果有人能解释我这一点,我将永远感激。

+0

你应该使用文本属性。如果来自vb6/vba,则没有隐式文本属性的概念。 –

+0

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

回答

5

两个txtVisinatxtTezinaTextBox对象,而不是包含在其中的字符串。您需要使用来自这些对象的.Text属性来访问用户界面中的字符串值。

例如:

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

和:

txtBmiRez.Text = ""; 

有趣的是,你的txtBmiRez秒使用实际上是正确的。

当谈到解析双打(或字符串中的任何对象)时,还建议处理任何可能的错误,可以用TryParse完成。如果字符串不是有效数字,则Parse会引发异常,而TryParse将返回false。比如改变你点击的方法来这样的事情是有利,并会减少任何潜在的崩溃:

private void button1_Click(object sender, EventArgs e) 
{ 
    if(Double.TryParse (txtVisina.Text, out v) && 
     Double.TryParse (txtTezina.Text, out t)) { 
     r = t/(v * v); 
     txtBmiRez.Text = String.Format("{0:f}", r); 
    } else { 
     // Handle failure to parse 
     MessageBox.Show("Failed to parse text to number."); 
    } 
} 
+0

我接受你的回答是正确的,它没有错误地运行,但总是在尝试计算时值得0.00结果,这是非常奇怪的,我必须检查我的数学形式:D然而,非常感谢你! – Goran

+0

@GoranStojanovic没问题 - 记住你可以进行调试,以确保在计算'r'时这些值是正确的。它可能是你的字符串格式。你总是可以尝试'txtBmiRez.Text = r.ToString(“F2”);' –

1

您需要访问TextBoxText属性以获取其文本。您正在尝试投射文本框本身。

例如,而不是

v = Double.Parse (txtVisina); 

以下是正确的:

v = Double.Parse (txtVisina.Text); 
2

变量txtVisinatxtTezinaTextBoxes但您尝试使用它们作为字符串。所有你需要用是他们的Text财产,即

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

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

string enteredValue = textbox1.Text; 

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

0

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

为任何字符串指定必须使用以下语法一个文本框:

textboxid.Text =“”; ,如果你想给它分配任何非字符串值,可以使用:.ToString()函数在将变量分配给文本框之前将其转换为字符串。 例如 int i = 10; Textbox1.Text = i.ToString();

相关问题