iterate through controls on forms, groupbox and panels

D

dantheman

Hi,

i'm doing a programe in a few languages so i've found this code that
iterates through the form controls and returns there names
so i can switch languages

but the problem is after re-parenting the controls to optimize the code
i don't get the names of the controls on my groupbox or my panels see
note in getcontrolname what can i do to make it work

this is for the .net compact framework so getchild etc is not available

any help would be apreciated

here's the code

//will do translation of form controls
private void TranslateForm()
{
string s;

foreach (Control ctrl in this.Controls)
if ((object)(s = resManager.GetString(GetControlName(ctrl),
currentCulture)) != null)
ctrl.Text = s;
}

//will get control names
private string GetControlName(object sourceControl)
{
FieldInfo[] fi = ((Control)sourceControl).Parent.GetType().GetFields(
BindingFlags.NonPublic | BindingFlags.Instance |
BindingFlags.Public | BindingFlags.IgnoreCase );

foreach (FieldInfo f in fi)
if ( f.GetValue(((Control)sourceControl).Parent).Equals(sourceControl)
) //this line is true only if the control is on the form directly not
if the control is on the groupbox or panel
return f.Name;

return null;
}
 
T

Thore Berntsen

Today I just finished multiple language support on a big .NET CF project.
And I did quite a bit of research before I decided on how to do it. I found
that the best way to translate the controls in a form was to create a
TranslateMethod where I hardcoded the translation of each control. That may
seem like a lot of work, but it wasn't really. How many controls does one
have in a .NET CF form? I also tried the method in your sample, but I the
reflection bit was to slow for me. Another problem with the sample is that
if you change the name of a control, and forget to change it in the
resource, the translation is gone.

Translating other strings was done by loading the correct resource with the
ResorceManager class.

We have bougth Multilizer for translation of our sytems wich includes Delph
and .NET . Great tool , but not cheap.

Thore Berntsen
 

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

Similar Threads


Top