2011-09-26 59 views
10

我已經在C#中創建了Windows窗體程序。我在本地化方面遇到一些問題。我有兩種語言的資源文件(一個是英文,另一個是法文)。我想單擊每個語言按鈕並在運行時更改語言。如何在運行時更改WinForms應用程序的文化

但是,當我點擊按鈕,它不起作用。我正在使用此代碼。

private void btnfrench_Click(object sender, EventArgs e) 
{ 
    getlanguage("fr-FR"); 
} 

private void getlanguage(string lan) 
{ 
    foreach (Control c in this.Controls) 
    { 
     ComponentResourceManager cmp = 
      new ComponentResourceManager(typeof(BanksForm)); 
     cmp.ApplyResources(c, c.Name, new CultureInfo(lan)); 
    } 
} 

將任何請在此幫助......

非常感謝....

回答

18

這工作:

private void button1_Click(object sender, EventArgs e) 
{ 
    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr-BE"); 
    ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1)); 
    resources.ApplyResources(this, "$this"); 
    applyResources(resources, this.Controls); 
} 

private void applyResources(ComponentResourceManager resources, Control.ControlCollection ctls) 
{ 
    foreach (Control ctl in ctls) 
    { 
     resources.ApplyResources(ctl, ctl.Name); 
     applyResources(resources, ctl.Controls); 
    } 
} 

小心避免添加任何人都不會使用的口哨。

+0

對不起,我已經試過這個,但它不適用於我.. –

+0

我是否需要添加任何資源文件來形成,我已經將本地化屬性更改爲true,並將語言英語更改爲比利時,但它沒有顯示語言我選擇了......並且我看到任何額外的資源文件被添加到窗體中... –

+1

您甚至沒有開始使用它,並且想要知道如何切換?不知道什麼「不顯示我選擇的語言」可能意味着什麼。你需要編輯屬性。更改語言屬性後,設置表單的Text屬性爲例。這將自動創建Form1.fr-BE.resx文件。打開窗體旁邊的節點以查看它。 –

5

您可能需要遞歸調用ApplyResources上的控件:

private void btnfrench_Click(object sender, EventArgs e) 
{ 
    ApplyResourceToControl(
     this, 
     new ComponentResourceManager(typeof(BanksForm)), 
     new CultureInfo("fr-FR")) 
} 

private void ApplyResourceToControl(
    Control control, 
    ComponentResourceManager cmp, 
    CultureInfo cultureInfo) 
{ 
    cmp.ApplyResources(control, control.Name, cultureInfo); 

    foreach (Control child in control.Controls) 
    { 
     ApplyResourceToControl(child, cmp, cultureInfo); 
    } 
} 
+0

我曾嘗試你的代碼,但它不工作.... –

0

在運行時更新CultureInfo可能會重置組件大小。此代碼保持控制 的大小和位置(還是會有明顯的閃爍,雖然,這使用SuspendLayout()無法修復)


    private void langItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e) 
    { 
     //I store the language codes in the Tag field of list items 
     var itemClicked = e.ClickedItem; 
     string culture = itemClicked.Tag.ToString().ToLower(); 

     Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(culture); 
     ApplyResourceToControl(
     this, 
     new ComponentResourceManager(typeof(GUI)), 
     new CultureInfo(culture));   
    } 

    private void ApplyResourceToControl(
     Control control, 
     ComponentResourceManager cmp, 
     CultureInfo cultureInfo) 
    { 
     foreach (Control child in control.Controls) 
     { 
      //Store current position and size of the control 
      var childSize = child.Size; 
      var childLoc = child.Location; 
      //Apply CultureInfo to child control 
      ApplyResourceToControl(child, cmp, cultureInfo); 
      //Restore position and size 
      child.Location = childLoc; 
      child.Size = childSize; 
     } 
     //Do the same with the parent control 
     var parentSize = control.Size; 
     var parentLoc = control.Location; 
     cmp.ApplyResources(control, control.Name, cultureInfo); 
     control.Location = parentLoc; 
     control.Size = parentSize; 
    } 
相關問題