02. september 2004 - 08:57Der er
3 kommentarer og 1 løsning
Sum af Listbox.
Hej
Jeg er lidt ny på C#, men har en lille let som dem som lige ved det.
Jeg fylder en masse int tal i en alm ListBox, og vil derefter lave en function der finder gennensnittet af alle talne, men jeg kan ikke få fat på de enkelte elementer i listboxen når de først er .add til listboxen.
Jeg har prøvet flg. routine men den returnere bare 0.
private int SumTheIntegers(params object[] list) { // summerer alle intergers i en listbox int sum = 0; foreach (object o in list) if (o.GetType() == typeof(int)) sum += (int) o; return sum; }
her er et simpelt eksempel der virker. using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data;
public Form1() { // // Required for Windows Form Designer support // InitializeComponent();
// // TODO: Add any constructor code after InitializeComponent call // }
/// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (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.textBox1 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.listBox1 = new System.Windows.Forms.ListBox(); this.label1 = new System.Windows.Forms.Label(); this.lblsum = new System.Windows.Forms.Label(); this.lblresult = new System.Windows.Forms.Label(); this.SuspendLayout(); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(32, 288); this.textBox1.Multiline = true; this.textBox1.Name = "textBox1"; this.textBox1.TabIndex = 0; this.textBox1.Text = ""; // // button1 // this.button1.Location = new System.Drawing.Point(152, 32); this.button1.Name = "button1"; this.button1.TabIndex = 1; this.button1.Text = "beregn sum"; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Location = new System.Drawing.Point(144, 288); this.button2.Name = "button2"; this.button2.TabIndex = 2; this.button2.Text = "Indsæt tal"; this.button2.Click += new System.EventHandler(this.button2_Click); // // listBox1 // this.listBox1.Location = new System.Drawing.Point(16, 16); this.listBox1.Name = "listBox1"; this.listBox1.Size = new System.Drawing.Size(120, 225); this.listBox1.TabIndex = 3; // // label1 // this.label1.Location = new System.Drawing.Point(160, 72); this.label1.Name = "label1"; this.label1.TabIndex = 4; this.label1.Text = "Sum"; // // lblsum // this.lblsum.Location = new System.Drawing.Point(160, 104); this.lblsum.Name = "lblsum"; this.lblsum.TabIndex = 5; this.lblsum.Text = "-"; // // lblresult // this.lblresult.Location = new System.Drawing.Point(24, 328); this.lblresult.Name = "lblresult"; this.lblresult.Size = new System.Drawing.Size(208, 56); this.lblresult.TabIndex = 6; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(352, 462); this.Controls.Add(this.lblresult); this.Controls.Add(this.lblsum); this.Controls.Add(this.label1); this.Controls.Add(this.listBox1); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Controls.Add(this.textBox1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false);
} #endregion
/// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); }
private void button2_Click(object sender, System.EventArgs e) { try { int tal = Int32.Parse(textBox1.Text); listBox1.Items.Add(tal); textBox1.Text = ""; lblresult.Text = ""; } catch(Exception) { lblresult.Text = "fejl under indsættelse af tal"; } }
private void button1_Click(object sender, System.EventArgs e) { int sum = 0; foreach(object o in listBox1.Items) { sum += Int32.Parse(o.ToString()); } lblsum.Text = sum.ToString(); listBox1.Items.Clear(); }
Det var - foreach(object o in listBox1.Items) - der var kode ordet.
Thanks.
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.