Salve, risconto dei problemi con questo semplice esercizio. Praticamente anche se il massimo di numeri che dovrebbe essere accettati è (ad. es) 3, si ferma a due.
Form1.cs
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 _12_12_2017
{
public partial class Form1 : Form
{
int somma = 0, n = 0, nmax;
string st = "";
public Form1()
{
InitializeComponent();
}
private void btnCancella_Click(object sender, EventArgs e)
{
lblN.Text = "";
lblNumeri.Text = "";
lblSomma.Text = "";
UpDown1.Value = 5;
txtDato.Text = "";
somma = 0;
n = 0;
btnSomma.Enabled = true;
st = "";
}
private void btnSomma_Click(object sender, EventArgs e)
{
int numero;
if (n<=nmax && txtDato.Text!="")
{
n++;
st += txtDato.Text + "\n";
numero = Convert.ToInt32(txtDato.Text);
somma += numero;
lblN.Text = n.ToString();
lblNumeri.Text = st;
lblSomma.Text = somma.ToString();
}
else
{
if(n>nmax)
{
btnSomma.Enabled = false;
}
if(txtDato.Text != "")
{
txtDato.Text = "";
txtDato.Focus();
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
nmax = Convert.ToInt32(UpDown1.Value);
}
}
}
Form1.Designer.cs
namespace _12_12_2017
{
partial class Form1
{
/// <summary>
/// Variabile di progettazione necessaria.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Pulire le risorse in uso.
/// </summary>
/// <param name="disposing">ha valore true se le risorse gestite devono essere eliminate, false in caso contrario.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Codice generato da Progettazione Windows Form
/// <summary>
/// Metodo necessario per il supporto della finestra di progettazione. Non modificare
/// il contenuto del metodo con l'editor di codice.
/// </summary>
private void InitializeComponent()
{
this.btnSomma = new System.Windows.Forms.Button();
this.btnCancella = new System.Windows.Forms.Button();
this.txtDato = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.lblN = new System.Windows.Forms.Label();
this.UpDown1 = new System.Windows.Forms.NumericUpDown();
this.lblNumeri = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.lblSomma = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.UpDown1)).BeginInit();
this.SuspendLayout();
//
// btnSomma
//
this.btnSomma.Location = new System.Drawing.Point(213, 9);
this.btnSomma.Name = "btnSomma";
this.btnSomma.Size = new System.Drawing.Size(75, 26);
this.btnSomma.TabIndex = 1;
this.btnSomma.Text = "Somma";
this.btnSomma.UseVisualStyleBackColor = true;
this.btnSomma.Click += new System.EventHandler(this.btnSomma_Click);
//
// btnCancella
//
this.btnCancella.Location = new System.Drawing.Point(294, 9);
this.btnCancella.Name = "btnCancella";
this.btnCancella.Size = new System.Drawing.Size(75, 26);
this.btnCancella.TabIndex = 2;
this.btnCancella.Text = "Cancella";
this.btnCancella.UseVisualStyleBackColor = true;
this.btnCancella.Click += new System.EventHandler(this.btnCancella_Click);
//
// txtDato
//
this.txtDato.Location = new System.Drawing.Point(100, 13);
this.txtDato.Name = "txtDato";
this.txtDato.Size = new System.Drawing.Size(89, 20);
this.txtDato.TabIndex = 3;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label2.Location = new System.Drawing.Point(222, 76);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(66, 16);
this.label2.TabIndex = 4;
this.label2.Text = "Addendi";
//
// lblN
//
this.lblN.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblN.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblN.Location = new System.Drawing.Point(294, 69);
this.lblN.Name = "lblN";
this.lblN.Size = new System.Drawing.Size(75, 23);
this.lblN.TabIndex = 5;
//
// UpDown1
//
this.UpDown1.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.UpDown1.Location = new System.Drawing.Point(12, 54);
this.UpDown1.Maximum = new decimal(new int[] {
10,
0,
0,
0});
this.UpDown1.Minimum = new decimal(new int[] {
1,
0,
0,
0});
this.UpDown1.Name = "UpDown1";
this.UpDown1.Size = new System.Drawing.Size(59, 24);
this.UpDown1.TabIndex = 7;
this.UpDown1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this.UpDown1.Value = new decimal(new int[] {
1,
0,
0,
0});
//
// lblNumeri
//
this.lblNumeri.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblNumeri.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblNumeri.Location = new System.Drawing.Point(100, 54);
this.lblNumeri.Name = "lblNumeri";
this.lblNumeri.Size = new System.Drawing.Size(89, 205);
this.lblNumeri.TabIndex = 8;
//
// label5
//
this.label5.AutoSize = true;
this.label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label5.Location = new System.Drawing.Point(228, 115);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(60, 16);
this.label5.TabIndex = 9;
this.label5.Text = "Somma";
//
// lblSomma
//
this.lblSomma.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblSomma.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblSomma.Location = new System.Drawing.Point(294, 108);
this.lblSomma.Name = "lblSomma";
this.lblSomma.Size = new System.Drawing.Size(75, 23);
this.lblSomma.TabIndex = 10;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.Location = new System.Drawing.Point(34, 18);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(37, 15);
this.label1.TabIndex = 11;
this.label1.Text = "Dato";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(391, 286);
this.Controls.Add(this.label1);
this.Controls.Add(this.lblSomma);
this.Controls.Add(this.label5);
this.Controls.Add(this.lblNumeri);
this.Controls.Add(this.UpDown1);
this.Controls.Add(this.lblN);
this.Controls.Add(this.label2);
this.Controls.Add(this.txtDato);
this.Controls.Add(this.btnCancella);
this.Controls.Add(this.btnSomma);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.UpDown1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button btnSomma;
private System.Windows.Forms.Button btnCancella;
private System.Windows.Forms.TextBox txtDato;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label lblN;
private System.Windows.Forms.NumericUpDown UpDown1;
private System.Windows.Forms.Label lblNumeri;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label lblSomma;
private System.Windows.Forms.Label label1;
}
}