View Single Post
  #1 (permalink)  
Old 29th January, 2007, 05:47 AM
MrSeanKon's Avatar
MrSeanKon MrSeanKon is offline
OcBible Creator
 
Join Date: June 2005
Location: Where the streets have no name
Posts: 766

Question GKR how can I use for loop to access textboxes in C#??

The source code is:

Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class Form1 : System.Windows.Forms.Form
{
	private System.Windows.Forms.TextBox textBox1;
	private System.Windows.Forms.TextBox textBox2;
	private System.Windows.Forms.TextBox textBox3;
	private System.Windows.Forms.TextBox textBox4;
	private System.Windows.Forms.Button button1;
	private System.ComponentModel.Container components = null;

	public Form1()
	{
		InitializeComponent();
	}
	protected override void Dispose( bool disposing )
	{
		if( disposing )
		{
			if (components != null) 
			{
				components.Dispose();
			}
		}
		base.Dispose( disposing );
	}

	#region Windows Form Designer generated code
	private void InitializeComponent()
	{
		this.textBox1 = new System.Windows.Forms.TextBox();
		this.textBox2 = new System.Windows.Forms.TextBox();
		this.textBox3 = new System.Windows.Forms.TextBox();
		this.textBox4 = new System.Windows.Forms.TextBox();
		this.button1 = new System.Windows.Forms.Button();
		this.SuspendLayout();
		// 
		// textBox1
		// 
		this.textBox1.Location = new System.Drawing.Point(16, 24);
		this.textBox1.Name = "textBox1";
		this.textBox1.Size = new System.Drawing.Size(48, 20);
		this.textBox1.TabIndex = 0;
		this.textBox1.Text = "";
		// 
		// textBox2
		// 
		this.textBox2.Location = new System.Drawing.Point(16, 56);
		this.textBox2.Name = "textBox2";
		this.textBox2.Size = new System.Drawing.Size(48, 20);
		this.textBox2.TabIndex = 1;
		this.textBox2.Text = "";
		// 
		// textBox3
		// 
		this.textBox3.Location = new System.Drawing.Point(16, 88);
		this.textBox3.Name = "textBox3";
		this.textBox3.Size = new System.Drawing.Size(48, 20);
		this.textBox3.TabIndex = 2;
		this.textBox3.Text = "";
		// 
		// textBox4
		// 
		this.textBox4.Location = new System.Drawing.Point(16, 120);
		this.textBox4.Name = "textBox4";
		this.textBox4.Size = new System.Drawing.Size(48, 20);
		this.textBox4.TabIndex = 3;
		this.textBox4.Text = "";
		// 
		// button1
		// 
		this.button1.FlatStyle = System.Windows.Forms.FlatStyle.System;
		this.button1.Location = new System.Drawing.Point(88, 72);
		this.button1.Name = "button1";
		this.button1.TabIndex = 4;
		this.button1.Text = "Read data";
		this.button1.Click += new System.EventHandler(this.button1_Click);
		// 
		// Form1
		// 
		this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
		this.ClientSize = new System.Drawing.Size(176, 150);
		this.Controls.Add(this.button1);
		this.Controls.Add(this.textBox4);
		this.Controls.Add(this.textBox3);
		this.Controls.Add(this.textBox2);
		this.Controls.Add(this.textBox1);
		this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
		this.MaximizeBox = false;
		this.MinimizeBox = false;
		this.Name = "Form1";
		this.Text = "Read textboxes";
		this.ResumeLayout(false);
	}
	#endregion

	[STAThread]
	static void Main() 
	{
		Application.EnableVisualStyles();
		Application.Run(new Form1());
	}

	private void button1_Click(object sender, System.EventArgs e)
	{
		double sum=0.0;

		sum=Read_textboxes(textBox1);        // Instead of four calls how we can do the same thing
		sum+=Read_textboxes(textBox2);      // using a for loop? For this example this is not a
		sum+=Read_textboxes(textBox3);      // problem but if the program has 700 different 
		sum+=Read_textboxes(textBox4);      // textboxes or 1000 comboboxes too many calls GKR...
		MessageBox.Show("Summary is " + sum.ToString(),"WOW!");
	}
	private double Read_textboxes (TextBox txtbox)
	{
		try
		{
        			return double.Parse(txtbox.Text,System.Globalization.NumberFormatInfo.InvariantInfo);
		}
		catch
		{
			return 0.0;
		}
	}
}
__________________

Last edited by MrSeanKon; 14th March, 2007 at 02:30 AM.
Reply With Quote