Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am using 3 textboxes
Instead of declaring the variables and the conversion (text -> int) of the 3 texts in all the command button codes
How can I declare the variables once and not repeat it.

I dunno which flavor of VB you're using, but with VB 6 at least, you can use a control array for your text boxes and keep an index variable.
During the textbox's change event, update the index... that way, you can use the index to manipulate data once under the command button's click event.
For example, with a VB 6 program and a textbox control array
Dim idx As Integer Private Sub Command1_Click() MsgBox CInt(Text1(idx).Text) End Sub Private Sub Text1_Change(Index As Integer) idx = Index End SubHope that helps
edit: oops - you don't really need the index as you can use the textbox array elements directly. Force of habit. :P

Thankyou for your response.
I am using VB6
There are 2 text boxes which need int input and one that provides 1 output but 3 command buttons which use the same textboxes.
I have declared each textbox in all 4 command button functions
Is there any other way I can declare them without repeating the declaration of the same variables in the command button functions
Please also advise me if there are other terms I should use, as I am new to VB
Would appreciate your assistance

If seems like you'd want to use a control array for the command buttons. You can create the array a couple of ways I guess.
Place the first command button on the form, and then right click on it. Choose copy and then paste. VB will prompt you if you want to create a control array. After confirming, paste two more times.
Another way is to place the four command buttons one at a time and then edit their properties so that the "(name)" field is all the same and the "index" field uniquely identifies each button: button1 = 0, button2 = 1, button3 = 2, and button4 = 3.
Depending on what you're up to, you probably wouldn't need to declare any locals, but use the text boxes directly.
Private Sub Command1_Click(Index as Integer) Select Case Index Case 0 Text3.text = Cint(Text1.text) some action with Cint(Text2.text) Case 1 Text3.text = ... Case 2 Text3.text = ... Case 3 Text3.text = ... Case Default ... End Select End SubIf you need them, you can convert to floats using CSng, to doubles using CDbl, and to long integers using CLng.
You might also want to validate the input before entering the select case statement with an if control block to make sure the two text boxes have numerical data.
Private Sub Command1_Click(Index as Integer) If Not IsNumeric(Text1.Text) Then MsgBox "Invalid input for number 1" Text1.Text = "": Text1.SetFocus: Exit Sub End If If Not IsNumeric(Text2.Text) Then MsgBox "Invalid input for number 2" Text2.Text = "": Text2.SetFocus: Exit Sub End If Select Case Index ... End Select End SubHope that helps

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |