Textbox in VB

June 8, 2009 at 21:26:09
Specs: Windows Vista
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.


See More: Textbox in VB

Report •


#1
June 9, 2009 at 02:33:46
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 Sub

Hope that helps


edit: oops - you don't really need the index as you can use the textbox array elements directly. Force of habit. :P


Report •

#2
June 9, 2009 at 21:52:04
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


Report •

#3
June 10, 2009 at 02:16:39
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 Sub

If 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 Sub

Hope that helps


Report •

Related Solutions


Ask Question