2014-01-14 51 views
3

我是C#和編程的新手。我能夠在控制檯中創建所需的程序,但也希望使用Forms來處理一個。嘗試從文本框中獲取int時遇到問題。嘗試從文本框中獲取int時出錯

在調試我得到錯誤:

Error 3 'int' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?) D:\Dropbox\Classwork\C_Sharp\InProgress\PaintDeterminator\Paint Determinator Form\Paint Determinator Form\Form1.cs 30 57 Paint Determinator Form

這裏是我到目前爲止已經編寫的代碼。

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 Paint_Determinator_Form 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      int LengthtextBox; 
      int WidthtextBox; 
      int HeighttextBox; 
      int paint; 
      int answer; 

       LengthtextBox = int.Parse(LengthtextBox.Text); 
       WidthtextBox = int.Parse(WidthtextBox.Text); 
       HeighttextBox = int.Parse(HeighttextBox.Text); 
       paint = 350; 

       answer = (LengthtextBox * WidthtextBox * HeighttextBox)/paint; 

       MessageBox.Show(answer.ToString()); 

     } 

    } 
} 

namespace Paint_Determinator_Form 
{ 
    partial class Form1 
    { 
     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     private System.ComponentModel.IContainer components = null; 

     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
     protected override void Dispose(bool disposing) 
     { 
      if (disposing && (components != null)) 
      { 
       components.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 

     #region Windows Form Designer generated code 

     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     private void InitializeComponent() 
     { 
      this.label1 = new System.Windows.Forms.Label(); 
      this.label2 = new System.Windows.Forms.Label(); 
      this.label3 = new System.Windows.Forms.Label(); 
      this.label4 = new System.Windows.Forms.Label(); 
      this.WidthtextBox = new System.Windows.Forms.TextBox(); 
      this.HeighttextBox = new System.Windows.Forms.TextBox(); 
      this.LengthtextBox = new System.Windows.Forms.TextBox(); 
      this.button1 = new System.Windows.Forms.Button(); 
      this.SuspendLayout(); 
      // 
      // label1 
      // 
      this.label1.AutoSize = true; 
      this.label1.Location = new System.Drawing.Point(28, 29); 
      this.label1.Name = "label1"; 
      this.label1.Size = new System.Drawing.Size(454, 13); 
      this.label1.TabIndex = 0; 
      this.label1.Text = "Welcome to Paint Determinator! Please enter the measurements in the appropriate f" + 
    "ields below!"; 
      // 
      // label2 
      // 
      this.label2.AutoSize = true; 
      this.label2.Location = new System.Drawing.Point(28, 91); 
      this.label2.Name = "label2"; 
      this.label2.Size = new System.Drawing.Size(35, 13); 
      this.label2.TabIndex = 1; 
      this.label2.Text = "Width"; 
      // 
      // label3 
      // 
      this.label3.AutoSize = true; 
      this.label3.Location = new System.Drawing.Point(28, 139); 
      this.label3.Name = "label3"; 
      this.label3.Size = new System.Drawing.Size(38, 13); 
      this.label3.TabIndex = 2; 
      this.label3.Text = "Height"; 
      // 
      // label4 
      // 
      this.label4.AutoSize = true; 
      this.label4.Location = new System.Drawing.Point(28, 183); 
      this.label4.Name = "label4"; 
      this.label4.Size = new System.Drawing.Size(40, 13); 
      this.label4.TabIndex = 3; 
      this.label4.Text = "Length"; 
      // 
      // WidthtextBox 
      // 
      this.WidthtextBox.Location = new System.Drawing.Point(175, 83); 
      this.WidthtextBox.Name = "WidthtextBox"; 
      this.WidthtextBox.Size = new System.Drawing.Size(100, 20); 
      this.WidthtextBox.TabIndex = 5; 
      // 
      // HeighttextBox 
      // 
      this.HeighttextBox.Location = new System.Drawing.Point(175, 131); 
      this.HeighttextBox.Name = "HeighttextBox"; 
      this.HeighttextBox.Size = new System.Drawing.Size(100, 20); 
      this.HeighttextBox.TabIndex = 6; 
      // 
      // LengthtextBox 
      // 
      this.LengthtextBox.Location = new System.Drawing.Point(175, 183); 
      this.LengthtextBox.Name = "LengthtextBox"; 
      this.LengthtextBox.Size = new System.Drawing.Size(100, 20); 
      this.LengthtextBox.TabIndex = 7; 
      // 
      // button1 
      // 
      this.button1.Location = new System.Drawing.Point(349, 402); 
      this.button1.Name = "button1"; 
      this.button1.Size = new System.Drawing.Size(75, 23); 
      this.button1.TabIndex = 9; 
      this.button1.Text = "Paint"; 
      this.button1.UseVisualStyleBackColor = true; 
      // 
      // Form1 
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      this.ClientSize = new System.Drawing.Size(511, 447); 
      this.Controls.Add(this.button1); 
      this.Controls.Add(this.LengthtextBox); 
      this.Controls.Add(this.HeighttextBox); 
      this.Controls.Add(this.WidthtextBox); 
      this.Controls.Add(this.label4); 
      this.Controls.Add(this.label3); 
      this.Controls.Add(this.label2); 
      this.Controls.Add(this.label1); 
      this.Name = "Form1"; 
      this.Text = "Form1"; 
      this.ResumeLayout(false); 
      this.PerformLayout(); 

     } 

     #endregion 

     private System.Windows.Forms.Label label1; 
     private System.Windows.Forms.Label label2; 
     private System.Windows.Forms.Label label3; 
     private System.Windows.Forms.Label label4; 
     private System.Windows.Forms.TextBox WidthtextBox; 
     private System.Windows.Forms.TextBox HeighttextBox; 
     private System.Windows.Forms.TextBox LengthtextBox; 
     private System.Windows.Forms.Button button1; 
    } 
+1

我強烈建議不要定義具有相同名稱的局部變量我們的班級成員。不考慮問題範圍,它使代碼非常難以理解。 – David

+1

只是一個建議 - 不要將您的內部變量命名爲與文本框控件相同,這很混亂。 –

+0

我編輯了你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –

回答

7

爲什麼你的名字你的文本喜歡你的文本框?這是一個很糟糕的做法,並在最底層令人困惑。正如你所看到的,編譯器認爲你正在使用int變量而不是文本框,並且抱怨int類型沒有名爲Text的屬性。

所以,簡單地改變整數的名稱點擊方法

private void button1_Click(object sender, EventArgs e) 
    { 
     int l; 
     int w; 
     int h; 
     int paint; 
     int answer; 

     l = int.Parse(LengthtextBox.Text); 
     w = int.Parse(WidthtextBox.Text); 
     h = int.Parse(HeighttextBox.Text); 
     paint = 350; 

     answer = (l * w * h)/paint; 

     MessageBox.Show(answer.ToString()); 
    } 

說,我建議使用Int32.TryParse由用戶輸入的數據轉換爲有效的整數內。如果您的用戶類型的東西,不能轉換爲整數,而不是TryParse返回false parse方法將拋出一個異常無需昂貴的例外

例如

int l; 
    if(!Int32.TryParse(LengthtextBox.Text, out l)) 
    { 
     MessageBox.Show("Please type a valid number for Length"); 
     return; 
    } 

When the Int32.TryParse returns, the out parameter (l) contains the 32-bit signed integer value equivalent of the number contained in your textbox, if the conversion succeeds, or zero if the conversion fails

+1

+1問題描述和更好的解決方案 –

+0

Steve,謝謝!仍然羞於15聲望,所以我不能讚賞你,但你用一種容易理解的方式解釋了它。這是一個在線課程,雖然這本書和在線資料很有幫助,但在遇到問題時很難獲得有關問題的推斷。 – Zerodown

2

你聲明本地範圍的變量,將取代你的實際文本框。呦ushould使用有像不同的名字局部變量:

int length = int.Parse(LengthtextBox.Text); 
int width = int.Parse(WidthtextBox.Text);; 
int height = int.Parse(HeighttextBox.Text);; 
1

在你situatuation,最好重新命名整型,但你ablealso在simmilar情況下使用這個

LengthtextBox = int.Parse(this.LengthtextBox.Text); 
WidthtextBox = int.Parse(this.WidthtextBox.Text); 
HeighttextBox = int.Parse(this.HeighttextBox.Text); 

我想如果你是新在編程中也很好地瞭解這一點,而不是僅僅在不理解的情況下重命名變量。

1

button1_Click()方法來刪除這些定義,你使用相同的名字,因爲這方法裏面的類變量:

int LengthtextBox; 
int WidthtextBox; 
int HeighttextBox; 

的變化而變化的變量的名稱:

int length; 
int width; 
int height; 

length= int.Parse(LengthtextBox.Text); 
width= int.Parse(WidthtextBox.Text); 
height= int.Parse(HeighttextBox.Text); 
paint = 350; 

answer = (length* width* height)/paint; 
2

我更新了你的代碼,你使用與您的實際文本框名稱相同的變量...這不是一個好主意:

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 Paint_Determinator_Form 
    { 
     public partial class Form1 : Form 
     { 
      public Form1() 
      { 
       InitializeComponent(); 
      } 

      private void button1_Click(object sender, EventArgs e) 
      { 
       int length; 
       int width; 
       int height; 
       int paint; 
       int answer; 

        length = int.Parse(LengthtextBox.Text); 
        width = int.Parse(WidthtextBox.Text); 
        height = int.Parse(HeighttextBox.Text); 
        paint = 350; 

        answer = (length* width* height)/paint; 

        MessageBox.Show(answer.ToString()); 

      } 

     } 
    } 

    namespace Paint_Determinator_Form 
    { 
     partial class Form1 
     { 
      /// <summary> 
      /// Required designer variable. 
      /// </summary> 
      private System.ComponentModel.IContainer components = null; 

      /// <summary> 
      /// Clean up any resources being used. 
      /// </summary> 
      /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
      protected override void Dispose(bool disposing) 
      { 
       if (disposing && (components != null)) 
       { 
        components.Dispose(); 
       } 
       base.Dispose(disposing); 
      } 

      #region Windows Form Designer generated code 

      /// <summary> 
      /// Required method for Designer support - do not modify 
      /// the contents of this method with the code editor. 
      /// </summary> 
      private void InitializeComponent() 
      { 
       this.label1 = new System.Windows.Forms.Label(); 
       this.label2 = new System.Windows.Forms.Label(); 
       this.label3 = new System.Windows.Forms.Label(); 
       this.label4 = new System.Windows.Forms.Label(); 
       this.WidthtextBox = new System.Windows.Forms.TextBox(); 
       this.HeighttextBox = new System.Windows.Forms.TextBox(); 
       this.LengthtextBox = new System.Windows.Forms.TextBox(); 
       this.button1 = new System.Windows.Forms.Button(); 
       this.SuspendLayout(); 
       // 
       // label1 
       // 
       this.label1.AutoSize = true; 
       this.label1.Location = new System.Drawing.Point(28, 29); 
       this.label1.Name = "label1"; 
       this.label1.Size = new System.Drawing.Size(454, 13); 
       this.label1.TabIndex = 0; 
       this.label1.Text = "Welcome to Paint Determinator! Please enter the measurements in the appropriate f" + 
     "ields below!"; 
       // 
       // label2 
       // 
       this.label2.AutoSize = true; 
       this.label2.Location = new System.Drawing.Point(28, 91); 
       this.label2.Name = "label2"; 
       this.label2.Size = new System.Drawing.Size(35, 13); 
       this.label2.TabIndex = 1; 
       this.label2.Text = "Width"; 
       // 
       // label3 
       // 
       this.label3.AutoSize = true; 
       this.label3.Location = new System.Drawing.Point(28, 139); 
       this.label3.Name = "label3"; 
       this.label3.Size = new System.Drawing.Size(38, 13); 
       this.label3.TabIndex = 2; 
       this.label3.Text = "Height"; 
       // 
       // label4 
       // 
       this.label4.AutoSize = true; 
       this.label4.Location = new System.Drawing.Point(28, 183); 
       this.label4.Name = "label4"; 
       this.label4.Size = new System.Drawing.Size(40, 13); 
       this.label4.TabIndex = 3; 
       this.label4.Text = "Length"; 
       // 
       // WidthtextBox 
       // 
       this.WidthtextBox.Location = new System.Drawing.Point(175, 83); 
       this.WidthtextBox.Name = "WidthtextBox"; 
       this.WidthtextBox.Size = new System.Drawing.Size(100, 20); 
       this.WidthtextBox.TabIndex = 5; 
       // 
       // HeighttextBox 
       // 
       this.HeighttextBox.Location = new System.Drawing.Point(175, 131); 
       this.HeighttextBox.Name = "HeighttextBox"; 
       this.HeighttextBox.Size = new System.Drawing.Size(100, 20); 
       this.HeighttextBox.TabIndex = 6; 
       // 
       // LengthtextBox 
       // 
       this.LengthtextBox.Location = new System.Drawing.Point(175, 183); 
       this.LengthtextBox.Name = "LengthtextBox"; 
       this.LengthtextBox.Size = new System.Drawing.Size(100, 20); 
       this.LengthtextBox.TabIndex = 7; 
       // 
       // button1 
       // 
       this.button1.Location = new System.Drawing.Point(349, 402); 
       this.button1.Name = "button1"; 
       this.button1.Size = new System.Drawing.Size(75, 23); 
       this.button1.TabIndex = 9; 
       this.button1.Text = "Paint"; 
       this.button1.UseVisualStyleBackColor = true; 
       // 
       // Form1 
       // 
       this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
       this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
       this.ClientSize = new System.Drawing.Size(511, 447); 
       this.Controls.Add(this.button1); 
       this.Controls.Add(this.LengthtextBox); 
       this.Controls.Add(this.HeighttextBox); 
       this.Controls.Add(this.WidthtextBox); 
       this.Controls.Add(this.label4); 
       this.Controls.Add(this.label3); 
       this.Controls.Add(this.label2); 
       this.Controls.Add(this.label1); 
       this.Name = "Form1"; 
       this.Text = "Form1"; 
       this.ResumeLayout(false); 
       this.PerformLayout(); 

      } 

      #endregion 

      private System.Windows.Forms.Label label1; 
      private System.Windows.Forms.Label label2; 
      private System.Windows.Forms.Label label3; 
      private System.Windows.Forms.Label label4; 
      private System.Windows.Forms.TextBox WidthtextBox; 
      private System.Windows.Forms.TextBox HeighttextBox; 
      private System.Windows.Forms.TextBox LengthtextBox; 
      private System.Windows.Forms.Button button1; 
     } 
+3

你的新變量名稱是有誤導性的。我會直觀地期望像'textBoxLength'和'textBoxWidth'這樣的東西來描述'TextBox'的尺寸。 – David

+0

因爲你是對的,所以加價。我用新的名字更新了代碼...... :) – Faraday

+0

作爲任何類型編程的noob,謝謝。出於某種原因,我認爲實體必須與文本框的名稱相同。 – Zerodown

相關問題