Computing.Net > Forums > Programming > Textbox in VB

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Textbox in VB

Reply to Message Icon

Name: 2u4rmOFA
Date: June 8, 2009 at 21:26:09 Pacific
OS: Windows Vista
Subcategory: General
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: shutat
Date: June 9, 2009 at 02:33:46 Pacific
Reply:

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


0

Response Number 2
Name: 2u4rmOFA
Date: June 9, 2009 at 21:52:04 Pacific
Reply:

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


0

Response Number 3
Name: shutat
Date: June 10, 2009 at 02:16:39 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Textbox in VB

character length in VB www.computing.net/answers/programming/character-length-in-vb/4579.html

Dataformat in VB www.computing.net/answers/programming/dataformat-in-vb/3594.html

textbox in VB.net www.computing.net/answers/programming/textbox-in-vbnet/16962.html