none
Output Toggle Button Names When Selected RRS feed

  • Question

  • Hello,

    I have several toggle buttons that I've added to a form, where each of these is of course named differently but also when any of the buttons are set to "True" (i.e. they're clicked), the background color changes of that button. What I would like to be able to do is have a section below the toggle buttons that will output the toggle button names in a list or even a row. 

    For example, here's what I've set one of the toggle buttons with the name "TCZ":

    Private Sub TCZ_Click()
    If TCZ.Value = True 
    ThenMe.TCZ.BackColor = RGB(0, 240, 0)    
    Else    
    Me.TCZ.BackColor = RGB(240, 240, 240)
    End If
    End Sub

    So if the above were clicked and thus set to True, in another area the name of this field would be the output.  It's this last part that I am not sure how to do since I'm not very advanced in macros/VB. 

    Any help would be greatly appreciated.

    I'm using Word 2010 v14.0.6 on Windows 7. 

    Thanks

    Monday, August 27, 2012 3:38 PM

Answers

  • Hi Imran

    This is a UserForm, I take it? And you want to put the value TCZ into a textbox or a label?

    There's no way to derive the name of the control from the actual code, so it's something you'd need to hard-code into each control's Sub.

    Assuming you'd want to change a label's caption, you'd add a line like this to the Sub you show:

      Me.Label1.Caption = "TCZ"


    Cindy Meister, VSTO/Word MVP

    • Marked as answer by Imran Virk Thursday, August 30, 2012 2:46 PM
    Tuesday, August 28, 2012 8:35 AM
    Moderator
  • Hi Imran Virk,

    Thank you for posting in the MSDN Forum.

    Based on your issue, I think you may need the following code to output the togglebutton name in a field(such as a label)

    Private Sub TCZ_Click()
    If TCZ.Value = True Then
     Label1.Caption = TCZ.Name
    End If
    End Sub

    Hope it helps.

    Best Regards,


    Leo_Gao [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Imran Virk Thursday, August 30, 2012 2:46 PM
    Tuesday, August 28, 2012 8:56 AM
    Moderator

All replies

  • Hi Imran

    This is a UserForm, I take it? And you want to put the value TCZ into a textbox or a label?

    There's no way to derive the name of the control from the actual code, so it's something you'd need to hard-code into each control's Sub.

    Assuming you'd want to change a label's caption, you'd add a line like this to the Sub you show:

      Me.Label1.Caption = "TCZ"


    Cindy Meister, VSTO/Word MVP

    • Marked as answer by Imran Virk Thursday, August 30, 2012 2:46 PM
    Tuesday, August 28, 2012 8:35 AM
    Moderator
  • Hi Imran Virk,

    Thank you for posting in the MSDN Forum.

    Based on your issue, I think you may need the following code to output the togglebutton name in a field(such as a label)

    Private Sub TCZ_Click()
    If TCZ.Value = True Then
     Label1.Caption = TCZ.Name
    End If
    End Sub

    Hope it helps.

    Best Regards,


    Leo_Gao [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Imran Virk Thursday, August 30, 2012 2:46 PM
    Tuesday, August 28, 2012 8:56 AM
    Moderator
  • Thank you both Cindy and Leo, the info was very helpful. 

    Yes, this is a for a user form that will be distributed to an internal team.  Based on the info provided, I added the same number of labels as there are toggle buttons (16 in this case), and then I updated the code for each toggle button to be as follows:

    Private Sub TCZ_Click()
    If TCZ.Value = True Then
    
    Me.TCZ.BackColor = RGB(0, 240, 0)
    Me.Label5.Caption = TCZ.Name
    
        Else
    
        Me.TCZ.BackColor = RGB(240, 240, 240)
        Me.Label5.Caption = ""
        Me.Label5.ForeColor = RGB(79, 129, 189)
        
    End If
    End Sub

    As you can probably tell, when someone clicks on the toggle buttons the label caption will display the name of said button in a section below the toggle buttons. 

    On another related topic (well sort of), I've saved this as a .dotm template but when I close it and re-open it, it is opened as a new document and the code I have added to the template isn't always available.  I'll often have to go back into the code view to manually change it to the correct section (see attached image).

    Perhaps I'm doing something wrong when I save this file so I'm hoping you can provide some guidance on this point.

    Thanks so much!



    -ivdezine

    Tuesday, August 28, 2012 4:40 PM
  • Hi ivdezine

    IT's always a good idea to put new questions in a new message :-) I know it's tempting to just continue on the discussion with the same people, but no one person knows everything. If you start a new message then your chances of people seeing it who could make positive contributions are much better.

    <<but when I close it and re-open it, it is opened as a new document >>

    Since I can't look over your shoulder, I can only guess that perhaps you're double-clicking the template file in Windows Explorer? The default action is to generate a new document based on the template, rather than open the template. Try right-clicking and selecting "Open" from the list and see if that helps?


    Cindy Meister, VSTO/Word MVP

    Wednesday, August 29, 2012 7:39 AM
    Moderator
  • Hi Cindy,

    Thank you for the info on this - even though I clearly should've started a new thread.  I really appreciate the input as it's been a tremendous help to resolve the issues I was having! :-)

    Cheers!
    Imran


    -ivdezine

    Wednesday, August 29, 2012 1:24 PM
  • HI ivdezine

    Glad things are moving forward :-)

    When you have a moment, would you mind dropping by briefly and marking the message(s) in this thread that you consider to be the answer(s)? Thanks, and good luck on your project!


    Cindy Meister, VSTO/Word MVP

    Thursday, August 30, 2012 10:06 AM
    Moderator