473,405 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

get object from name

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
Nov 15 '05 #1
12 20296
This should do it:

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

-mike
MVP

"Aamir Ghanchi" <an*******@discussions.microsoft.com> wrote in message
news:04****************************@phx.gbl...
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

Nov 15 '05 #2
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.
This should do it:

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

Nov 15 '05 #3
Aamir <gh*****@bigfoot.com> wrote:
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.


Please post a short but complete example. See
http://www.pobox.com/~skeet/csharp/complete.html for what I mean by
this.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
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
Please post a short but complete example....

Nov 15 '05 #5
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" <gh*****@bigfoot.com> wrote in message
news:4a**************************@posting.google.c om...
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
Please post a short but complete example....


Nov 15 '05 #6
Aamir <gh*****@bigfoot.com> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7
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.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Aamir <gh*****@bigfoot.com> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #8
Rene <no****@nospam.nospam> wrote:
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #9
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());
}
}
}
Nov 15 '05 #10
Rene <no****@nospam.nospam> wrote:
private System.Windows.Forms.TextBox myChkBox;
<snip>
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #11
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.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Rene <no****@nospam.nospam> wrote:
private System.Windows.Forms.TextBox myChkBox;


<snip>
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #12
Try making myChkBox (which is a TextBox btw), public, or using the GetField
overload that takes some flags.
-mike
MVP
"Rene" <no****@nospam.nospam> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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());
}
}
}

Nov 15 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
9
by: Keith Rowe | last post by:
Hello, I am trying to reference a Shockwave Flash Object on a vb code behind page in an ASP.NET project and I receive the following error: Guid should contain 32 digits with 4 dashes...
6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
54
by: tshad | last post by:
I have a function: function SalaryDisplay(me) { var salaryMinLabel = document.getElementById("SalaryMin"); salaryMinLabel.value = 200; alert("after setting salaryMinLabel = " +...
2
by: Aaron | last post by:
I have a data sructure setup and I populate it in a loop like so: y=0 while X: DS.name = "ASDF" DS.ID = 1234 list = DS; y = y + 1
5
by: Matthew | last post by:
I have a nice little Sub that saves data in a class "mySettings" to an XML file. I call it like so: Dim mySettings As mySettings = New mySettings mySettings.value1 = "someText" mySettings.value2...
16
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener...
26
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized...
11
by: Andrus | last post by:
I'm implementing entity object which should populate its properties from database when property is first referenced. In RDL reports I use object properties like MyObject.MyProperty MyObject...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.