试图从文本框一个int时出错 [英] Error when trying to get an int from textbox

查看:151
本文介绍了试图从文本框一个int时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一般是新的C#和编程。我能够创建控制台所需的程序,但要得到一个使用表单的工作为好。我想从文本框中获取INT当运行到一个问题。



在调试我得到错误:




错误3'廉政'不不包含'文字',没有扩展方法文本接受类型'诠释'的第一个参数的定义可以找到(是否缺少using指令或程序集引用?)D:\Dropbox\Classwork\ C_Sharp\InProgress\PaintDeterminator\Paint测定仪Form\Paint测定仪Form\Form1.cs 30 57油漆测定仪表格




这里是我到目前为止已经编写的代码

 使用系统; 
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;使用System.Threading.Tasks
;使用System.Windows.Forms的
;

命名空间Paint_Determinator_Form
{
公共部分Form1类:表格
{
公共Form1中()
{
的InitializeComponent() ;
}

私人无效的button1_Click(对象发件人,EventArgs五)
{
INT LengthtextBox;
INT WidthtextBox;
INT HeighttextBox;
INT漆;
INT的答案;

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

答案=(LengthtextBox * WidthtextBox * HeighttextBox)/漆;

MessageBox.Show(answer.ToString());

}

}
}

命名空间Paint_Determinator_Form
{
部分Form1类
{
///<总结>
///必需的设计变量。
///< /总结>
私人System.ComponentModel.IContainer成分= NULL;

///<总结>使用
///清理的任何资源。
///< /总结>
///< PARAM NAME =处理>真要是托管资源应释放;否则为false< /参数>
保护覆盖无效的Dispose(BOOL处置)
{
如果(处置和放大器;及(成分= NULL)!)
{
components.Dispose();
}
base.Dispose(处置);
}

#region Windows窗体设计器生成的代码

///<总结>
///设计器支持所需的方法 - 不要修改
///此方法的代码编辑器的内容。
///< /总结>
私人无效的Ini​​tializeComponent()
{
this.label1 =新System.Windows.Forms.Label();
this.label2 =新System.Windows.Forms.Label();
this.label3 =新System.Windows.Forms.Label();
this.label4 =新System.Windows.Forms.Label();
this.WidthtextBox =新System.Windows.Forms.TextBox();
this.HeighttextBox =新System.Windows.Forms.TextBox();
this.LengthtextBox =新System.Windows.Forms.TextBox();
this.button1 =新System.Windows.Forms.Button();
this.SuspendLayout();
//
// LABEL1
//
this.label1.AutoSize = TRUE;
this.label1.Location =新System.Drawing.Point(28,29);
this.label1.Name =LABEL1;
this.label1.Size =新System.Drawing.Size(454,13);
this.label1.TabIndex = 0;
this.label1.Text =欢迎到涂料测定仪请输入适当的F测量!+
的ields!;
//
// LABEL2
//
this.label2.AutoSize = TRUE;
this.label2.Location =新System.Drawing.Point(28,91);
this.label2.Name =LABEL2;
this.label2.Size =新System.Drawing.Size(35,13);
this.label2.TabIndex = 1;
this.label2.Text =宽度;
//
// LABEL3
//
this.label3.AutoSize = TRUE;
this.label3.Location =新System.Drawing.Point(28,139);
this.label3.Name =LABEL3;
this.label3.Size =新System.Drawing.Size(38,13);
this.label3.TabIndex = 2;
this.label3.Text =高度;
//
// label4
//
this.label4.AutoSize = TRUE;
this.label4.Location =新System.Drawing.Point(28,183);
this.label4.Name =label4;
this.label4.Size =新System.Drawing.Size(40,13);
this.label4.TabIndex = 3;
this.label4.Text =长度;
//
// WidthtextBox
//
this.WidthtextBox.Location =新System.Drawing.Point(175,83);
this.WidthtextBox.Name =WidthtextBox;
this.WidthtextBox.Size =新System.Drawing.Size(100,20);
this.WidthtextBox.TabIndex = 5;
//
// HeighttextBox
//
this.HeighttextBox.Location =新System.Drawing.Point(175,131);
this.HeighttextBox.Name =HeighttextBox;
this.HeighttextBox.Size =新System.Drawing.Size(100,20);
this.HeighttextBox.TabIndex = 6;
//
// LengthtextBox
//
this.LengthtextBox.Location =新System.Drawing.Point(175,183);
this.LengthtextBox.Name =LengthtextBox;
this.LengthtextBox.Size =新System.Drawing.Size(100,20);
this.LengthtextBox.TabIndex = 7;
//
//按钮1
//
this.button1.Location =新System.Drawing.Point(349,402);
this.button1.Name =Button1的;
this.button1.Size =新System.Drawing.Size(75,23);
this.button1.TabIndex = 9;
this.button1.Text =画图;
this.button1.UseVisualStyleBackColor = TRUE;
//
// Form1中
//
this.AutoScaleDimensions =新System.Drawing.SizeF(6F,13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize =新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(假);
this.PerformLayout();

}

#endregion

私人System.Windows.Forms.Label LABEL1;
私人System.Windows.Forms.Label LABEL2;
私人System.Windows.Forms.Label LABEL3;
私人System.Windows.Forms.Label label4;
私人System.Windows.Forms.TextBox WidthtextBox;
私人System.Windows.Forms.TextBox HeighttextBox;
私人System.Windows.Forms.TextBox LengthtextBox;
私人System.Windows.Forms.Button按钮1;
}


解决方案

为什么你命名你的整数喜欢你的文本框?这实在是一个不好的做法和混乱的极处的水平。正如你所看到的,编译器认为您使用的是INT变量而不是文本框,并抱怨说,一个int类型有没有财产被称为文本。



所以,简单地改变点击方法中的整数的名称



 私人无效的button1_Click(对象发件人,EventArgs五)
{
INT升;
INT瓦;
INT H;
INT漆;
INT的答案;

L = int.Parse(LengthtextBox.Text);
W = int.Parse(WidthtextBox.Text);
H = int.Parse(HeighttextBox.Text);
油漆= 350;

答案=(L * W * H)/漆;

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



说,我建议使用的Int32.TryParse ,以你的用户在有效的整数类型的数据转换。如果你的用户类型的东西,不能转换为整数,而不是的TryParse 无需昂贵的例外


$返回false parse方法将抛出一个异常b $ b

例如

  int类型l; 
如果
{
MessageBox.Show(请键入长度的有效数字)(Int32.TryParse(LengthtextBox.Text,OUT L)!);
的回报;
}




当Int32.TryParse返回时,出参数(L)包含
32位有符号整数等效<包含在你的
文本框的数量,如果转换成功,或零如果转换失败


/中BLOCKQUOTE>

I am new to C# and programming in general. I was able to create the required program in Console but want to get one working with Forms as well. I am running into an issue when trying to get int from textboxes.

On Debug I am getting error:

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

Here is the code I've written so far.

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

解决方案

Why do you name your ints like your textboxes? It is really a bad practice and confusing at the uttermost level. As you can see, the compiler thinks that you are using the int variables instead of the textboxes and complains that an int type have no property called Text.

So simply change the name of the ints inside the click method

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

Said that, I suggest to use Int32.TryParse to convert the data typed by your user in a valid integer. The Parse method will throw an exception if your user types something that cannot be translated to an integer, instead TryParse returns false without a costly exception

For example

  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

这篇关于试图从文本框一个int时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆