【问题标题】:Error when trying to get an int from textbox尝试从文本框中获取 int 时出错
【发布时间】:2014-02-03 06:06:39
【问题描述】:

我是 C# 和一般编程的新手。我能够在 Console 中创建所需的程序,但也希望获得一个使用 Forms 的程序。尝试从文本框中获取 int 时遇到问题。

在调试时出现错误:

错误 3 'int' 不包含 'Text' 的定义,并且找不到接受第一个类型为 'int' 的参数的扩展方法 'Text'(您是否缺少 using 指令或程序集引用?) 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;
    }

【问题讨论】:

  • 我强烈建议不要定义与类级成员同名的局部变量。抛开范围问题不谈,它让代码非常难以理解。
  • 只是一个建议 - 不要将您的内部变量命名为与文本框控件相同的名称,这非常令人困惑。
  • 我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
  • 谢谢大家。约翰我不知道,将来会修改。感谢大家启发我并继续前进,我不会用同样的名字命名。非常感谢。

标签: c# winforms


【解决方案1】:

您正在声明将替换您的实际文本框的本地范围变量。您应该使用具有不同名称的局部变量,例如:

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

【讨论】:

    【解决方案2】:

    在你的情况下最好重命名整数,但你也可以在类似的情况下使用它

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

    我认为,如果您是编程新手,最好了解这一点,而不是在不理解的情况下重命名变量。

    【讨论】:

      【解决方案3】:

      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;
      

      【讨论】:

        【解决方案4】:

        为什么你把你的整数命名为你的文本框?这确实是一种糟糕的做法,并且在最根本的层面上令人困惑。如您所见,编译器认为您使用的是 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 将用户输入的数据转换为有效整数。如果您的用户键入的内容无法转换为整数,Parse 方法将引发异常,而不是 TryParse 返回 false 而不会产生代价高昂的异常

        例如

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

        当 Int32.TryParse 返回时,输出参数 (l) 包含 32 位有符号整数值,相当于您的数字中包含的数字 文本框,如果转换成功,或者如果转换失败则为零

        【讨论】:

        • 史蒂夫,谢谢!仍然低于 15 名声望,所以我不能投票给你,但你以一种我很容易理解的方式解释了它。这是针对在线课程的,虽然书籍和在线材料很有帮助,但当您有问题时,很难对某个问题进行推断。
        【解决方案5】:

        我更新了您的代码,您使用的变量名称与您的实际文本框名称相同...这不是一个好主意:

        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;
                }
        

        【讨论】:

        • 您的新变量名称有点误导。我会直观地期望像 textBoxLengthtextBoxWidth 这样的东西来描述 TextBox 的尺寸。
        • 赞成,因为你是对的。我用新名称更新了代码... :)
        • 作为任何类型编程的菜鸟,谢谢。出于某种原因,我认为实体的名称必须与文本框的名称相同。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-26
        相关资源
        最近更新 更多