how do i refer to a button using a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DavidMurrayEsq
    New Member
    • Sep 2010
    • 2

    how do i refer to a button using a string

    Ok, I have a form with about 750 buttons and i want to be able to refer to a button based on a string.

    For example, instead of writing...

    Button1_A.BackC olor = Color.ForestGre en

    I want to write...

    Dim Q_No As String
    Dim Btn_Type As String
    Dim Btn As String

    Private Sub Another_Button_ Click(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles Another_Button. Click
    Q_No = "1"
    Btn_Type = "A"
    Btn = "Button" & Q_No & "_" & Btn_Type
    Btn.BackColor = Color.ForestGre en()
    End Sub

    But i just get the error message "'BackColor ' is not a member of 'String'"
  • MikeTheBike
    Recognized Expert Contributor
    • Jun 2007
    • 640

    #2
    Hi

    Is this Access 2007 or later as I dont recognise the Click argument (look like .Net arguments)?

    But if it is Access and it is compatable with earlier versions then something like this should work

    Code:
    Btn = "Button" & Q_No & "_" & Btn_Type
    Me.Controls(Btn).BackColor = Color.ForestGreen()
    ???


    MTB

    Comment

    • DavidMurrayEsq
      New Member
      • Sep 2010
      • 2

      #3
      No its Visual Basic 2008,

      Dont worry though i found the answer to be
      Code:
        
              Dim Q_No As String
              Dim Btn_Type As String
              Dim BtnName As String
       
              Dim Btn As Button
       
              Q_No = "1"
              Btn_Type = "A"
              BtnName = "Button" & Q_No & "_" & Btn_Type
       
              Btn = TryCast(Me.Controls.Find(BtnName, True)(0), Button)
       
              If Not Btn Is Nothing Then
                  Btn.BackColor = Color.ForestGreen
              End If
      But thank you for your reply

      Comment

      • MikeTheBike
        Recognized Expert Contributor
        • Jun 2007
        • 640

        #4
        Hi again

        Just been looking ad VB Express 2008 (which as far as I can see is .Net by any other name), and find that the code in my last post does seem to work in .Net and a little easier to follow (in my opinion, but depend what you are used to), ie.
        Code:
        BtnName = "Button" & Q_No & "_" & Btn_Type 
        Me.Controls(BtnName).BackColor = Color.ForestGreen
        This of course my not be the prefered way of doings thing in .Net !!??


        MTB

        Comment

        Working...