VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Acces control thru variable

by James Travis (2 Submissions)
Category: Miscellaneous
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (3 Votes)

Be able to access multiple controls which require the same task with minimal code. Basically I wanted to be able to access controls without having to write numerous bits and peices to reach multiple controls with multiple forms and could not find what I needed so I decided to post. Here you are creating 2 control arrays with labels and all the code will demonstrate is randomly changeing the color of a randomly selected label.
This is my first post so if a bit lame hope you at least find it usefull.

Inputs
You need to create a form with the following items. Timer1 Set Interval = 1000 Label1 (0 - 7) Control Array Label2 (0 - 7) Control Array
Code Returns
None.
Side Effects
None.

Rate Acces control thru variable

Private Sub Timer1_Timer()
  Dim CtrlName As String, CtrlIdx As Integer
  Dim ClrSlct As Integer, ClrSlcted As Long
  Dim LblRow As Integer
  
  Randomize
  
  CtrlIdx = Rnd * 7
  
  Randomize
  
  ClrSlct = Rnd * 3
  
  Select Case ClrSlct
    Case 0
      ClrSlcted = &HFF00&   'Green
    Case 1
      ClrSlcted = &HFF&    'Red
    Case 2
      ClrSlcted = &HFF0000  'Blue
    Case 3
      ClrSlcted = &HFFFF&   'Yellow
  End Select
  
  Randomize
  
  LblRow = (Rnd * 1) + 1
  CtrlName = "Label" & LblRow
  
  Form1.Controls(CtrlName).Item(CtrlIdx).BackColor = ClrSlcted
  
End Sub

Download this snippet    Add to My Saved Code

Acces control thru variable Comments

No comments have been posted about Acces control thru variable. Why not be the first to post a comment about Acces control thru variable.

Post your comment

Subject:
Message:
0/1000 characters