I have a toolbar… it has buttons… buttons have captions, name and tags
Is there a way to refer to a button by its caption or name?
or do I have to loop thru them all until I hit the one I want?
// given the INDEX of a button.. enable/disable it
Dim i As Integer
For i=0 To rbToolbar1.Count
If rbToolbar1.Item(i).Tag=index And rbtoolbar1.item(i).enabled<>new_value Then
rbToolbar1.Item(i).Enabled=new_value
Exit For
End If
Next i
Maybe this will help. It extends Toolbar to allow syntax like:
Toolbar1.ButtonName("Edit").Enabled = false
Function ButtonName(extends t As Toolbar, nm as string) As ToolButton
dim i as Integer
dim tb as ToolButton
dim ti as ToolItem
for i = 0 to t.Count -1
ti = t.Item(i)
tb = ToolButton(ti)
if tb.Name= nm then
return ToolButton(t.item(i))
end if
next
End Function
I modified it from some source on the the old RB forums. (Forgot the author, sorry)
The problem is when you try to enable/disable toolbaritems based on a condition. We don’t have direct access to the name but only to the index. You can’t say
toolbar.item1.disabled = true
Intead you have to loop over the items in a toolbar and test for the name and then you can do
if toolbar.item(i).name = “something” then do something.
If you’re treating specific items specially then make it a property. This allows for auto-complete and not reliant on name matching. Set the property when adding this target item and nil it when removing the item.
So if you need to manipulate the “allow edits” toggle button add the property ‘buttonAllowEdits As ToolButton’ and set the reference. Changing it is as easy as…
+1
Like M Stolove and Dave.
It makes reading the source a lot easier.
This reminds me a lot of the discussion earlier on this for forum about the increment operator.
Create an empty toolbar and add it to your window.
For each item, create a property for each ToolButton
In the Open event of the toolbar add all your ToolButton.
For example :
TBSave = new ToolButton
TBSave.Caption="Save"
TBSave.Name="SaveTB"
TBSave.icon=saveIcon
TBSave.Style= ToolButton.ToolStylePushButton
me.Append TBSave
and when you want to enable your toolbutton you just have to write
TBSave.Enabled = false
or if you want to change the caption :
TBSave.Caption = "Save as..."
Not sure this is the best way, but most of the time, if you have only one toolbar and only wants to enable/disable some of them, this tips can be useful.