Naming Your Controls
Always make a habit of naming controls. We name our control using its Name property. Naming controls follows the guideline for naming variables such as spaces, special characters, and the use of keywords are prohibited. There have been many naming conventions that emerged when it comes to naming controls.
You can name the control depending on its use. For example, a text box use to retrieve the first name of the user can simply be named firstName just like a normal variable. But when naming controls, it is better to prefix the actual names with the name of the control. For example, instead of simply firstName, we can use textBoxFirstName. With that convention, we will know, using IntelliSense, that we are working with a text box or any control.
Another technique used by others is abbreviating the control names. For example, instead of using textBoxFirstName, you can use txtFirstName. The txt is short for the textbox. There has been a list of abbreviations for every control and you can even use your own abbreviation as long as it is clear to you and to others who will look at your code.
Another naming convention is the reverse of the first one where you place the descriptive name first followed by the type of control and also uses camel casing. For example, a text box for retrieving the first name of the person can be named firstNameTextBox or a button used for calculating can be named calculateButton.
The following lessons use the first naming convention where you simply use the name of the control in camelCasing style followed by a descriptive name. You don’t have to memorize a set of abbreviations or invent one. When you drag a control from the ToolBox, you simply need to remove the number suffix and add a descriptive name. When you are typing and you want to easily find what control you want to work with, simply type what kind of control it is, for example, a text box, and all the text box controls will show up in the IntelliSense. The only downside is some names might become too long. It is still up to you as to what naming convention you are more comfortable to use.
The following gives you some example name depending on the control and its use.
Scenario | Name |
---|---|
A Button used to confirm a message. | buttonConfirm, confirmButton, btnConfirm |
A TextBox used to accept email address from user. | textBoxAddress, addressTextBox, txtAddress |
A Form used for obtaining personal information. | formPersonalInformation, personalInformationForm, frmPersonalInformation |
A ComboBox to show a list of products. | comboBoxProducts, productsComboBox, cmbProducts |
A RadioButton which tells if a person is male. | radioButtonMale, maleRadioButton, radMale |
A MenuItem for saving a file. | menuItemSave, saveMenuItem, mnuSave |
A CheckBox to subscribe to newletter. | checkBoxSubscribe, subscribeCheckBox, chkSubscribe |
It is not necessary to name every control in the form. Controls that will never be accessed in code can be left by their default name. Examples of these are the labels that are merely used to label other controls. Have a habit of naming the control after you place them onto the form.