Erreur lors de la tentative d’obtention d’un int de textbox

Je suis nouveau en C # et en programmation en général. J’ai pu créer le programme requirejs dans la console, mais je souhaite également en utiliser un avec les formulaires. Je rencontre un problème lorsque j’essaie d’obtenir un int à partir de zones de texte.

Sur le débogage, je reçois une erreur:

Erreur 3 ‘int’ ne contient pas de définition pour ‘Texte’ et aucune méthode d’extension ‘Texte’ acceptant un premier argument de type ‘int’ n’a pu être trouvée (vous manque une directive using ou une référence d’assembly?) \ Classwork \ C_Sharp \ InProgress \ PaintDeterminator \ Formulaire de détermination de peinture \ Formulaire de détermination de peinture \ Form1.cs 30 57 Formulaire de détermination de peinture

Voici le code que j’ai écrit jusqu’à présent.

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.ToSsortingng() ); } } } namespace Paint_Determinator_Form { partial class Form1 { ///  /// Required designer variable. ///  private System.ComponentModel.IContainer components = null; ///  /// Clean up any resources being used. ///  /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code ///  /// Required method for Designer support - do not modify /// the contents of this method with the code editor. ///  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; } 

Pourquoi nommez-vous vos contenus comme vos champs de texte? C’est vraiment une mauvaise pratique et une source de confusion au plus haut niveau. Comme vous pouvez le constater, le compilateur pense que vous utilisez les variables int au lieu des zones de texte et se plaint qu’un type int ne possède aucune propriété appelée Text.

Donc, changez simplement le nom des ints dans la méthode de clic

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

Cela dit, je suggère d’utiliser Int32.TryParse pour convertir les données saisies par votre utilisateur en un entier valide. La méthode Parse lève une exception si votre utilisateur tape quelque chose qui ne peut pas être traduit en entier. Au lieu de TryParse , TryParse renvoie false sans exception coûteuse.

Par exemple

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

Lorsque Int32.TryParse est renvoyé, le paramètre out (l) contient la valeur entière signée sur 32 bits, équivalente au nombre contenu dans votre zone de texte, si la conversion aboutit, ou zéro si la conversion échoue.

Vous déclarez des variables de scope locale qui remplaceront vos zones de texte réelles. Vous devez utiliser des variables locales portant des noms différents, tels que:

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

J’ai mis à jour votre code, vous utilisiez des variables portant les mêmes noms que vos zones de texte réelles … Ce n’est pas une bonne idée:

 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.ToSsortingng() ); } } } namespace Paint_Determinator_Form { partial class Form1 { ///  /// Required designer variable. ///  private System.ComponentModel.IContainer components = null; ///  /// Clean up any resources being used. ///  /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code ///  /// Required method for Designer support - do not modify /// the contents of this method with the code editor. ///  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; } 

Supprimez ces définitions de la méthode button1_Click() car vous utilisez les mêmes noms que les variables de classe à l’intérieur de cette méthode:

 int LengthtextBox; int WidthtextBox; int HeighttextBox; 

Et changez le nom des variables:

 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; 

Dans votre situation, il est préférable de renommer les ressources, mais vous pouvez également l’utiliser dans des situations similaires.

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

Je pense que si vous êtes nouveau dans la programmation, il est bon de savoir à ce sujet également, mais simplement renommer les variables sans comprendre.