VB questions

Started by
2 comments, last by 5MinuteGaming 18 years, 10 months ago
After trying to figure out how to do this I decided to post it here for help. This is rather a "noobish" questions regarding Visualbasic so here goes I currently have a keypad type interface and I have 10 buttons, these cmd buttons are in a control array (i think thats the correct term) so I have got there captions to work by going through an array like so Private Sub Form_Load() For x = 0 To 9 cmdkey(x).Caption = x Next x End Sub So when the form loads it gives each of the cmdkeys a correct number. Im just wondering how do I actually use indervidual buttons to display their caption now. I want it so when the user clicks a button it will print out the number in txtdisplay.text but not sure how to do this. Thank you for any help you can provide. Sorry if this is not detailed enough.
Advertisement
When you create an array of Controls in Visual Basic each control has an index number associated with it. These index numbers are sequenced from the starting controls index number up to how ever many controls you copy and paste. This is assuming that your controls all have the same name.

If you double click on one of the buttons that you've created you should get to the Click sub in the code there should be an index number passed to that event sub. That index variable passed to the Click event sub is the array index number of the button that was just pressed.

The array index of the button can be found by selecting the button in the visual representation of the form that its on and looking in the properties window for a property "index"

if you want to set the txtDisplay.text to the current button pressed in the Click subroutine you would have a similar statement as the one you have in the Form_Load() subroutine.

Tip: Any control that has a '.Caption' property you can set by simply using the '=' sign. for example say you wanted to set the forms caption to "First Timer at VB" you could do it like this: me = "First Timer At VB" or in your case
cmdkey(x) = x 'should work
Ahhhh thansk for you help I got it working by doing the following

Private Sub Form_Load()
For x = 0 To 9
cmdkey(x).Caption = x
Next x
x = 0
End Sub
Private Sub cmdkey_Click(Index As Integer)
txtdisplay.Text = cmdkey(Index).Caption
End Sub

Now when i click on a command button it makes what ever is in the caption of that button display in the correct area.

Just wanted to do it this way rather than make each command button different that would have just meant I would have to go through putting in the same code for each command button.

Thank you :)
No Problem!

And it was a really good idea to use the control array for the buttons.
It is the best thing to do when using that many buttons that have similar functionality.

This topic is closed to new replies.

Advertisement