473,503 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

c# - controls access via control name as string

9 New Member
I followed discussion "access form control propertys via control name as string " but am getting NullReferenceException when trying to access any controls via their name using "this.Controls[CONTROL_NAME]".

private void mtbPh0_Leave(object sender, EventArgs e)
{
string callingControl = (sender as MaskedTextBox).Name;
this.Controls[callingControl].Visible=false;

I've manipulated callingControl to increment the last character, and I've tried just replacing the control name in the last line with a string (control name for a control that exists), but no matter what I do i get the NullReferenceException.
Is my syntax incorrect?
Jun 11 '07 #1
9 20429
TRScheel
638 Recognized Expert Contributor
I followed discussion "access form control propertys via control name as string " but am getting NullReferenceException when trying to access any controls via their name using "this.Controls[CONTROL_NAME]".

private void mtbPh0_Leave(object sender, EventArgs e)
{
string callingControl = (sender as MaskedTextBox).Name;
this.Controls[callingControl].Visible=false;

I've manipulated callingControl to increment the last character, and I've tried just replacing the control name in the last line with a string (control name for a control that exists), but no matter what I do i get the NullReferenceException.
Is my syntax incorrect?
Technically, your syntax is correct. Although we dont know where this is being ran. Also, if the object in question is a control within a control... within the page, asking for it from the list in the page is not going to retrieve it (it only knows of the children directly related to it, not indirectly).

Looking at your code, couldnt you just write:

((MaskedTextBox)sender).Visible = false;
Jun 12 '07 #2
redivider
9 New Member
I'm not quite sure what you mean "where this is being ran", but I am building a windows form using .Net 2.0. I left all my controls as the default, private, but I am calling this from within the form so I have access to them.
The reason I can't just use the sender is that I want to increment the last character (sender is mtbPh0, I want to change visibility on mtbPh1). I've just been testing it with various values. I know a round-about solution would be to throw the controls into an array, but now my mind won't give me peace if I don't figure this out.
Jun 12 '07 #3
TRScheel
638 Recognized Expert Contributor
I'm not quite sure what you mean "where this is being ran", but I am building a windows form using .Net 2.0. I left all my controls as the default, private, but I am calling this from within the form so I have access to them.
The reason I can't just use the sender is that I want to increment the last character (sender is mtbPh0, I want to change visibility on mtbPh1). I've just been testing it with various values. I know a round-about solution would be to throw the controls into an array, but now my mind won't give me peace if I don't figure this out.
Is it being ran within the form class? because then it will return the form's children. But the items have to be direct children of the form, else it wont find them.
Jun 12 '07 #4
Plater
7,872 Recognized Expert Expert
You need access to the parent of the control you wish to access, if you are in the form that has the button, you can use "this" (you can access parents with the .Parent)

The .Find() method will look for a control of the name you specify, and also takes a bool telling it to look in child controls for the specified control as well.
Expand|Select|Wrap|Line Numbers
  1. this.Controls.Find(NameOfControl, SearchAllChildren);
  2.  
Jun 12 '07 #5
redivider
9 New Member
OK.
Don't know what's going on, but I got the find to work, while the other still throws the exception. Both use "this.Controls" to access the controls. Hmmm.

[HTML] this.Controls.Find(callingControl, true)[0].Visible = false;
this.Controls[callingControl].Visible=false;//, false)[0].Show(); //throws error[/HTML]

I guess I'll be using the find, but I'm still curious about the error.
Jun 12 '07 #6
Plater
7,872 Recognized Expert Expert
if callingcontrol is not a direct child of "this", then it will return a null, and you are trying to NULL.Visible, since NULL does not have a .Visible property, you get an error.
Jun 13 '07 #7
redivider
9 New Member
if callingcontrol is not a direct child of "this", then it will return a null, and you are trying to NULL.Visible, since NULL does not have a .Visible property, you get an error.
That's what confuses the most.
In the exact same location the "find" line mentioned works, but the other line doesn't. The controls show up in the context-menu-popup-thingy if you type "this." in vs so they are direct children (right?).
Jun 13 '07 #8
Plater
7,872 Recognized Expert Expert
The controls show up in the context-menu-popup-thingy if you type "this." in vs so they are direct children (right?).
Not in the same fashion.
The context menu shows you all local variables (which is true for those things)
BUT, if your textbox is say inside a groupbox and the groupbox is on the form, then the textbox is a child of the groupbox and the groupbox is a child of the form.
form->groupbox->textbox.

So when "this" refers to the form (which it pretty much always does) doing this.controls[] will give you the groupbox and not the textbox.
The .FIND method however has that 2nd argument that tells it to continue to search in child controls for it.
Jun 14 '07 #9
redivider
9 New Member
... if your textbox is say inside a groupbox and the groupbox is on the form, then the textbox is a child of the groupbox and the groupbox is a child of the form.
Ahhhh.. He says, lightbulb above his head.
I didn't know that relationship existed. Thanks.
Jun 14 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
3067
by: V. Jenks | last post by:
What seems like a simple thing is apparently not so straightforward? I have a datalist. Inside of that datalist is an <itemtemplate> secion which contains other server controls such as a...
1
2407
by: Jeff Smith | last post by:
Can I load custom web user controls dynamically and access the properties and methods without having to explicitly define custom control types (example 2 below). I have custom web control named...
5
3495
by: Amelyan | last post by:
How can I get state of dynamically created controls (RadioButton, CheckBox, TextBox.Text) on post back when I click submit button? The only way I know is by traversing Response.Form enumberator;...
3
2768
by: Nathan Sokalski | last post by:
When I view any page in my application a second time, I recieve the following error: System.Web.TraceContext.AddNewControl(String id, String parentId, String type, Int32 viewStateSize) +313...
1
2244
by: Paul Bromley | last post by:
Can someone tell me if there is any way of doing this? As an example - let us say that I have a button called cmdLetter and a textbox called txtLetter, can I in some way manipulate the names of...
8
1685
by: | last post by:
I'm looking for some design guidance on a collection of projects I'm working on. The project involves a bunch of websites constructed out of a collection of user controls. Different user...
3
3089
by: Andreas Wöckl | last post by:
Hi Group! I have a web form that is created dynamically - so I create Textboxes, RadioButtonLists, CheckBoxLists and so on - I have found some articles that there may be some problems with...
1
1972
by: mirandacascade | last post by:
As I look through the drop-down list that appears after I type the word 'as' in a dim statement, I see options such as, collection, control, controls, form. That makes me think the functionality I...
9
2494
by: dhtml | last post by:
I have written an article "Unsafe Names for HTML Form Controls". <URL: http://jibbering.com/faq/names/ > I would appreciate any reviews, technical or otherwise. Garrett --...
0
7198
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
7072
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
7271
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6979
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
7449
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
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
373
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.