get object from name

  • Thread starter Thread starter Aamir Ghanchi
  • Start date Start date
A

Aamir Ghanchi

How can I get reference to a control object on a form by
passing it's name. something like

TextBox txt = SomeClass.SomeGetObjectMethod
("MyTextBoxName");

wheras SomeClass and SomeGetObjectMethod is what I am
looking for.
Thanks in advance.

Aamir
 
This should do it:

TextBox someTextBox =
myForm.GetType().GetFields("MyTextBox").GetValue(myForm) as TextBox;

-mike
MVP
 
Did not work for me. I guess it should be GetField (ending without
"s") but even then its not working and giving null reference
exception.
Aamir.
 
I think, what i posted was clear enough. This is something I want to
do, how can I have its example. Please see again my original posting.
Thanks
 
The example I gave you (with your GetField correction) should work (I just
tested it 15 seconds ago). If it's not, post a short but complete example
so we can see what's wrong.
-mike
MVP
 
Aamir said:
I think, what i posted was clear enough. This is something I want to
do, how can I have its example.

You said you'd tried something, but that it didn't work. Post what you
tried, in the form of a short but complete example that anyone can
compile, run and then modify until it works.

I'm not going to write a short program which works and then guess which
possible error you made - you give us the code which fails, and we can
help to fix it.

Did you read the page I referred you to?
 
Well I know this is not my post but I thoght the solution was intersting so
I tryied it and it did't work, here is my code:

CheckBox someCheckBox = this.GetType().GetField("myChkBox").GetValue(this)
as CheckBox;

In my case, the keyword "this" refers to a Form object. The error returned:

An unhandled exception of type 'System.NullReferenceException' occurred in
Renegineering.HealthTracker.Interface.exe
Additional information: Object reference not set to an instance of an
object.
 
Rene said:
Well I know this is not my post but I thoght the solution was intersting so
I tryied it and it did't work, here is my code:

CheckBox someCheckBox = this.GetType().GetField("myChkBox").GetValue(this)
as CheckBox;

In my case, the keyword "this" refers to a Form object. The error returned:

An unhandled exception of type 'System.NullReferenceException' occurred in
Renegineering.HealthTracker.Interface.exe
Additional information: Object reference not set to an instance of an
object.

And did your Form have a field called "myChkBox"? That's why I always
ask for *complete* code...
 
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication5
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox myChkBox;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();

// ?????????????????????????????????
// ?????????????????????????????????
CheckBox someCheckBox =
this.GetType().GetField("myChkBox").GetValue(this) as CheckBox;
// ?????????????????????????????????
// ?????????????????????????????????
}

/// <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.myChkBox = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// myChkBox
//
this.myChkBox.Location = new System.Drawing.Point(88, 120);
this.myChkBox.Name = "myChkBox";
this.myChkBox.Size = new System.Drawing.Size(296, 20);
this.myChkBox.TabIndex = 0;
this.myChkBox.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(464, 366);
this.Controls.Add(this.myChkBox);
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());
}
}
}
 
Rene said:
private System.Windows.Forms.TextBox myChkBox;

CheckBox someCheckBox =
this.GetType().GetField("myChkBox").GetValue(this) as CheckBox;

<snip>

Right. Unfortunately, the documentation doesn't mention it at all, but
it seems that the single parameter version of Type.GetField only gets
public fields. If you specify the two parameter version with the second
parameter being BindingFlags.Instance | BindingFlags.NonPublic it works
fine.
 
Awesome!



By the way, if anyone is following up on the post, please note that the
sample code calls for a CheckBox control but the declared control in my
sample code is a TextBox control, change the TextBox to a CheckBox control
and the code works great.

Thanks.
 
Try making myChkBox (which is a TextBox btw), public, or using the GetField
overload that takes some flags.
-mike
MVP
 
Back
Top