How can I get the name of a ToolBarButton object.

G

Guest

I need to iterate the Buttons collection in a toolbar and get the name of each button. The objects in the collection are not normal Button objects. They are ToolBarButton objects, which do not appear to have a Name property. Is there some other place to get the name?
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Bill,

ToolbarButton doesn't have Name property simply because it is not a Control.
Event though control has Name it is not it is not guarantee to be set. And
it is not set in a case of dynamicaly created controls, unless the code
doesn't set that property explicitly.

Maybe you are confused because you see (Name) property in the property
browser. Pay attention on the parenthesis around the name. That means this
is a design-time property. Even though the control has real 'Name' property
in the property browser you see the design-time one.

Anyways, there is no Name property, but if you want to save same user info
in the buttons you can always use the Tag property.

--
HTH
Stoitcho Goutsev (100) [C# MVP]


Bill Wilson said:
I need to iterate the Buttons collection in a toolbar and get the name of
each button. The objects in the collection are not normal Button objects.
They are ToolBarButton objects, which do not appear to have a Name property.
Is there some other place to get the name?
 
H

Herfried K. Wagner [MVP]

* "=?Utf-8?B?QmlsbCBXaWxzb24=?= said:
I need to iterate the Buttons collection in a toolbar and get the name
of each button. The objects in the collection are not normal Button
objects. They are ToolBarButton objects, which do not appear to have a
Name property. Is there some other place to get the name?

Why do you need the name? Maybe there is a better solution for your
task.
 
G

Guest

The objective is to have the entire form re-display in a different language on the fly. This should happen without changing the language of the application.

Take a canadian sales clerk. The clerk can receive calls in either French or English. They have trouble speaking French to a customer while reading a screen in English. The goal is to let them changed the language screen being used immediately, without having to close it and reload.

I want to use the name to lookup the button (or other sub component) text for display in the resource file for the selected culture. Is there a better way to change the language of a single form on the fly?
 
M

morfy

Hi,

I'm working in a project where we also want to change the language on the
fly. I think you have two options:

- put a string as an identifier to the tag as proposed earlier (well you can
put a class if you need more than just a name)
- create a helper that iterates all controls from Form
(a helper for example that takea the Forms control collection and walks
through all the controls and if the control has controls in it's control
collection gets these too)
you can put all these controls to a hashtable or something while
iterating the controls check if the control you are about to add to the
hashtable is a toolbar if so iterate the toolbar.buttons-collection and find
the identifier from the tag and use that as a key for the toolbarbutton
which you will also add to the hashtable.

so eventually you will have a hashtable which holds a reference to each
control on your form and also the toolbars toolbarbuttons. Then when you
want to change the language for all these you simply get your 'texts' from
resources and you can change the texts (resource name maps to the hashtables
key)

-another option would be to use reflection to get the toolbarbutton fieds
from the form (field name maps to the resource name)
ex. you have private ToolBarbutton myFirstButton on the form and you have a
myFirstButton key in the resource files.

my 2 cents, morfy

Bill Wilson said:
Thanks for the reply.

"Anyways, there is no Name property, but if you want to save same user info
in the buttons you can always use the Tag property."

Unfortunately, I would need to store the name of the component, which I
can't get because, as you point out, it doesn't actually have one. I need to
use the name to lookup the text (and other properties) in the resource file.
 
G

Guest

Thanks for the reply

I'm probably going to use a variant of your suggestion. This requires that the developer of a form copy the name of the ToolbarButton to its Tag field. I was hoping to avoid requiring anything of the developer beyond changing the base class of the Form to my localization class

"another option would be to use reflection to get the toolbarbutton fied
from the form (field name maps to the resource name
ex. you have private ToolBarbutton myFirstButton on the form and you have
myFirstButton key in the resource files.

This is what I was trying to do. But, I can't figure out. I can't get the names of the toolbar buttons during runtime. They seem to exist only in the source code generated by the designer. I can get the ToolBarButton objects, but they don't have a Name property. Is there a way to get the names from somewhere else
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top