Reflection / Indirection - whatever you call it

kasdoffe

Regular
Joined
Aug 27, 2003
Messages
57
Help! This would be awesome! (i.e. Reflection?)

Here's a simple example (maybe not the best) of what I want to do.

I have a textbox defined on my form:
TextBox textBox1 = new TextBox();

A string contains the name of my textbox:
string strControlName = "textBox1";

A string contains the text I want my textbox to display:
string strControlText = "Hello World";

How can I set the text property of my TextBox using the string I defined without looping through the controls on my form and checking if the controlname = strControlName.

In a previous language I knew you could use 'Indirection'. This acted similar to this:
Set (@strControlName).Text = strControlText.

The end result was textBox1.Text = "Hello World".

I was told Reflection acts similarly. Please provide a code example similar to my situation above.
 
Last edited:
I am wondering the same thing. It doesn't appear possible in .NET except save for a backwards looping through all the controls to get to the one you want.

Spektre
 
PHP:
/// <summary>
/// Sets the text of a <see cref="TextBox"/> that belongs to some object.
/// </summary>
/// <param name="source">The object that contains a <see cref="TextBox"/>.</param>
/// <param name="fieldName">The name of the <see cref="TextBox"/>.</param>
/// <param name="value">The text content to assign to the <see cref="TextBox"/>.</param>
static void SetText(object source, string fieldName, string value)
{
	if(source == null)
		throw new ArgumentNullException("source");
	if(fieldName == null)
		throw new ArgumentNullException("fieldName");
	FieldInfo field = source.GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
	if(field == null)
		throw new ArgumentException(string.Format("Field '{0}' does not exist.", fieldName));
	((TextBox) field.GetValue(source)).Text = value;
}
 
You might find a solution if you used hash tables.

I guess you have full control over all your text boxes. In that case you could loop trough them (only once) and put them in a hashtable with the controls name as the key. Below is a full example of what I mean:

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

namespace HTControls
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	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;
		System.Collections.Hashtable ht = new Hashtable();
		private System.Windows.Forms.Button button1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			ht[textBox1.Name]=textBox1;
			ht[textBox2.Name]=textBox2;
			ht[textBox3.Name]=textBox3;
		}

		/// <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.textBox2 = new System.Windows.Forms.TextBox();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(48, 24);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(168, 20);
			this.textBox1.TabIndex = 0;
			this.textBox1.Text = "textBox1";
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(48, 48);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(168, 20);
			this.textBox2.TabIndex = 1;
			this.textBox2.Text = "textBox2";
			// 
			// textBox3
			// 
			this.textBox3.Location = new System.Drawing.Point(48, 72);
			this.textBox3.Name = "textBox3";
			this.textBox3.Size = new System.Drawing.Size(168, 20);
			this.textBox3.TabIndex = 2;
			this.textBox3.Text = "textBox3";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(96, 112);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(64, 32);
			this.button1.TabIndex = 3;
			this.button1.Text = "Action";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 266);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.textBox3);
			this.Controls.Add(this.textBox2);
			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 button1_Click(object sender, System.EventArgs e)
		{
			TextBox tb = (TextBox)ht[textBox1.Text];
			tb.Text = "Text added by indirection";
		}
	}
}
 

Attachments

Back
Top