Naming text fields in vb2010

I am creating an application in vb2010 that requires a lot of text fields. Renaming each text field with a unique identifier is very time consuming. for example, renaming TextBox1 to txtName1 is a lengthy process when there are many of them.

Is there a way to speed this up to give a convenient name with a unique identification number? Or is it just a matter of plowing through each?

0
source share
2 answers

Always try to assign a meaningful name to a control. If you follow this approach, you will find that the computer cannot handle it, so it must be done manually. In your case, how does he know that TextBox1 needs to identify name ?

txtName1 not a good name for the control, because a) does not tell the developer whose name it is and b) that 1 ? Does this mean the first part of the name, that is, the name? Or the name of the first person on the list? The correct name will look like txtCustomerFirstName or txtFirstCustomerFullName (for a) and b) respectively), unless you have other considerations, such as keeping the control name as short as possible, match the database field name or another reason.

Source: I have developed an enterprise-class insurance management application with more than 1000 text fields and am the main contact person to support any problems with it at the moment. In my experience, the purpose of controls is not where you spend most of your time.

As a note, if you manually rename the control through the constructor, it will also rename all its uses in the code. This greatly reduces the work that needs to be done.

+3
source share

Try it: editing, as I am in front of the PC now;) It is really good to consider what the answer above says. In any case, here's what you need to know. When you add a text box or any other control sheet to Excel, it becomes an Embedded type, as shown in the figure.

enter image description here

Usually, if you want to check if a control is a text field , we can use 17 which represents the msoTextBox , an enumeration of the mso shape type for the TextBox. However, those built -in form controls (Forms 2.0) are Shapes and do not distinguish between each type with different values, but the usual 12 , type enumeration that stands for msoMixedShapes . After the 2nd code, it works for FORM , but it’s hard to get the same as for Excel sheet forms (text fields) ... Click on the link to view the entire MsoShapeType enumeration .

If you really need meaningful names for the Shape text fields in the Sheet, do one of the following:

 Option Explicit Sub reNameMeaningfully() Dim shp As Shape Dim arrNames As Variant Dim i As Integer arrNames = WorksheetFunction.Transpose(Sheets(1).Range("D2:D7").Value) '-- assuming that you have exact number of names for the exact number of controls For i = LBound(arrNames) To UBound(arrNames) Set shp = Sheets(1).Shapes(i) '-- we can't check Type here. So we check if default-name begins with Text If shp.Name Like "Text*" Then shp.Name = arrNames(i) End If Next i End Sub 

Output:

enter image description here

If these tecboxes are in FORM , try this: you can use the same logic to put all the names in a range and then iterate over it;)

 Dim Cont as Control Dim i as integer i = 1 For Each Cont In Me.Controls If TypeName(Cont) = "TextBox" Then Cont.name = "txt" & i '-- Rename End If Next Cont 
+2
source share

All Articles