Find a specific Toolbar Button

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 
      Exit For
    End If
  Next i

You have to loop through them. Might make a nice extension method.

Function Item(extends bar as Toolbar, name as string) as ToolItem

was afraid of that…

Do you think this would speed things up? (I have yet to try it)

As the Toolbar has buttons appended to it… store the name and index in a dictionary

then instead of looping thru them… look up the index in the dictionary

It does cause issues for things like rearranging, inserting and deleteing buttons… but it might be a start

A dictionary is a great idea. Instead of storing the index, though, store the actual item.

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)

thanks… but that is just encapsulating the same code I posted above… makes it easier to implement… but not faster

No problem. Let us know if you find an easier/faster method.

Haven’t found a better solution myself. So I’m using the same code. So far there hasn’t been a problem with the performance.

Just out of curiosity: why do you use

Dim i As Integer For i=0 To rbToolbar1.Count

instead of

 For i  Integer =0 To rbToolbar1.Count


Oh man: For i as Integer =0 To rbToolbar1.Count

Hi all,

I do not understand your thread at all.

If you are talking about ToolBar’s buttons and how to associate code to it, there is an example in the docs…

If you want to call the button’s code from elsewhere: place the button’s code in a module and call it (two times: once from each location).

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.

Thank you Beatrix for your explanation.

Now I understand the problem.

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…

myToolbar.buttonAllowEdits.Pushed = true

or include a nil check…

if myToolbar.buttonAllowEdits <> nil then

[quote]Just out of curiosity: why do you use

Dim i As Integer For i=0 To rbToolbar1.Count[/quote]

Just a personal preference. I’ve never really been fond of inline declarations.

I have always (and will always) do it the same way

DIM is an operation … it declares a variable and its data type…
and only AFTER it exists do I use or assign a value to to.

Like “M Stolove”… personal preference

I also only put DIM at the start… even thought supposedly the compiler deals with scope differently if a DIM is inside a loop etc.

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. :slight_smile:

Yo can create your toolbar by code.

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.


this bother me the whole afternoon and I found another way to not execute code under condition:

add a Boolean and test the Boolean to execute or not the code.

Works fine; excepted the ToolButton can be pushed; on the other hand, I do not care, I make a report in a Label at Window’s bottom.