473,657 Members | 2,604 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 NullReferenceEx ception when trying to access any controls via their name using "this.Contr ols[CONTROL_NAME]".

private void mtbPh0_Leave(ob ject 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 NullReferenceEx ception.
Is my syntax incorrect?
Jun 11 '07 #1
9 20433
TRScheel
638 Recognized Expert Contributor
I followed discussion "access form control propertys via control name as string " but am getting NullReferenceEx ception when trying to access any controls via their name using "this.Contr ols[CONTROL_NAME]".

private void mtbPh0_Leave(ob ject 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 NullReferenceEx ception.
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).Visibl e = 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.Contr ols" to access the controls. Hmmm.

[HTML] this.Controls.F ind(callingCont rol, 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
3081
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 label, a radiobuttonlist, etc. I'm driving myself insane trying to figure out how to get
1
2414
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 EditStuff.ascx which reads from an xml file and loads controls to its self based on string value in xml nodes collection of the xml. There are several controls that can be loaded and for each one there exists a public method called 'IntiControl'...
5
3507
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; Response.Form.GetEnumerator(), etc. while, identifying specific controls by programmatically assigned unique id (e.g. MyButton_AnswerID_123). However, I am not sure if that is the proper way. What is the common practice?
3
2781
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 System.Web.UI.Control.BuildProfileTree(String parentId, Boolean calcViewState) +201 System.Web.UI.Control.BuildProfileTree(String parentId, Boolean calcViewState) +263
1
2262
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 these controsl to extract the string Letter and then use this string later to refer to the Controls?? I can think of a number of ways in my coding that this would be very useful - especially in a number of applications where I have a number of...
8
1695
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 populations with different access rights and "roles" will be visiting the site. I will be using ASP.NET 2.0's membership, roles, and profiles stuff to manage access. User controls need to be visible or not visible depending on user role. In some...
3
3094
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 postback - but if I press a submit button the textboxes keep the value. My problem is the following: I would like to use this Code to collect the data: 'Collects values - is calles from a button
1
1987
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 am seeking is available...my problem is I don't know how to put the pieces together to make it happen. My questions are: 1) does Access 97 provide the functionality such that one can obtain a collection (or some other iterable list) of all...
9
2504
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 -- comp.lang.javascript FAQ <URL: http://jibbering.com/faq/ >
0
8303
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8821
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8723
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8602
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7316
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1601
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.