By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,276 Members | 2,137 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,276 IT Pros & Developers. It's quick & easy.

c# - controls access via control name as string

P: 9
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
Share this Question
Share on Google+
9 Replies


TRScheel
Expert 100+
P: 638
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

P: 9
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
Expert 100+
P: 638
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
Expert 5K+
P: 7,872
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

P: 9
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
Expert 5K+
P: 7,872
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

P: 9
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
Expert 5K+
P: 7,872
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

P: 9
... 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

Post your reply

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