How to reload a UserControl?

May 18, 2011 at 11:41:44
Specs: Windows 7
I'm making a Windows Form Application as a project for one of my classes. The user can choose between two languages. I use several user controls.
The method which sets the user control's labels and such to use the other language is of course in the user control (in the LoadControl function). Once added to the main form, how can I refresh the user control so it uses the language that the user selected after adding it to the form?

See More: How to reload a UserControl?

Report •

#1
May 18, 2011 at 14:41:22
I don't do much (read: any) ASP.NET coding, but couldn't you just enumerate the page's Controls and set the language as you go?

How To Ask Questions The Smart Way


Report •

#2
May 18, 2011 at 22:15:26
First of all, it's a Windows application, not a web application, which means the programing language is similar but not identical.
Second I have a main form, on which there is a menustrip. I use a global variable to store the usercontrol that was added to the form. So when a menu item is clicked, the value stored in that global variable is disposed, the wanted usercontrol is added, and the global variables value's set to be equal with the new usercontrol.
I'm open to any suggestions, even if it means to rewrite the way the usercontrols are called.

Report •

#3
May 19, 2011 at 08:04:25
First of all, it's a Windows application, not a web application, which means the programing language is similar but not identical.
Oh, that's fine then. It was the usage of LoadControl that threw me; it's a function in ASP.NET's Page object.

Probably the easiest way would be to dispose the form and reload it with the new culture info. That way, the hundred of little things you don't think of are correctly handled. (VB.NET example)

If you just want to change some controls on the forum, create your new control, dispose the old one, and add your new one.

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
May 19, 2011 at 11:05:42
I tried that, but for some reason I can't create the new user control with the help of my global variable. It's as if it's empty or something.
So I declare it globally like
UserControl cont = new UserControl();
So basically this is my menustrip item click function:
private void XToolStripMenuItem_Click(object sender, EventArgs e)
{
cont.Dispose(); // removes the previously added uc
myusercontrol bj = new myusercontrol();

int xpos = (this.Width-bj.Width)/2;
int ypos = (this.Height-bj.Height)/2;
bj.Location = new Point(xpos, ypos);
this.Controls.Add(bj);
cont = bj;
}

So... I'm at a loss at this point.


Report •

#5
May 19, 2011 at 13:03:01
Did you remember to remove the old control from the Controls collection?

How To Ask Questions The Smart Way


Report •

#6
May 19, 2011 at 23:42:41
Yes, I tried clearing the controls and readding the user control from the global variable, and it did add it again to the form but the language didn't change.

Report •

Ask Question