vb.net 如何在VB.NET中单击按钮时切换表单的语言?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14426609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:57:37  来源:igfitidea点击:

How to switch a language of the form on button click in VB.NET?

vb.netvisual-studio-2010localization

提问by VIK

I want to create the localized application and want to implement a language switcher (e.g. special button). I use Visual Studio 2010 express (VB.NET).

我想创建本地化的应用程序并想要实现语言切换器(例如特殊按钮)。我使用 Visual Studio 2010 express (VB.NET)。

I created simple test app with one label and one button. I set form's property "Localizable" to "True" and edited components' text in 2 languages (English as default and Russian).

我创建了一个带有一个标签和一个按钮的简单测试应用程序。我将表单的属性“Localizable”设置为“True”,并以 2 种语言(默认英语和俄语)编辑组件的文本。

I know that it is needed to add

我知道需要添加

Imports System.Threading.Thread
Imports System.Globalization

at the beginning of the Form1.vb and then use

在 Form1.vb 的开头然后使用

Thread.CurrentThread.CurrentUICulture = New CultureInfo("ru")

to enable Russian localization. But if I put this line into Button_Click event it does not change the language. Is it possible to switch between languages on event like button click or combobox change?

启用俄语本地化。但是,如果我将此行放入 Button_Click 事件中,它不会更改语言。是否可以在按钮单击或组合框更改等事件中切换语言?

Thank you in advance!

先感谢您!

采纳答案by Srinivas

Yes you can implement localization on Button Click event or on a change event. You can set the culture as

是的,您可以在按钮单击事件或更改事件上实施本地化。您可以将文化设置为

Thread.CurrentThread.CurrentUICulture = New CultureInfo("ru-RU")

These links will help you : Globalizing and Localizing Windows Application, Walkthrough: Localizing Windows Forms, Localizing Applications

这些链接将帮助您:全球化和本地化 Windows 应用程序演练:本地化 Windows 窗体本地化应用程序

回答by Ivan Ferrer Villa

here is a possible workaround: https://social.msdn.microsoft.com/Forums/en-US/72f70870-0c0c-4eb1-886b-9db9917d080a/form-support-multilanguage-at-runtime-in-windows-based-application#8c775cc0-5e5e-4551-b5d1-52bb5c1663e8

这是一个可能的解决方法:https: //social.msdn.microsoft.com/Forums/en-US/72f70870-0c0c-4eb1-886b-9db9917d080a/form-support-multilanguage-at-runtime-in-windows-based-应用程序#8c775cc0-5e5e-4551-b5d1-52bb5c1663e8

First change CurrentUICulture, then force apply the resources of the new culture to all controls.

首先更改 CurrentUICulture,然后将新文化的资源强制应用到所有控件。

This code example loops through Me.Controls, but you should loop child containers too (panels, etc).

此代码示例循环遍历Me.Controls,但您也应该循环子容器(面板等)。

Doing this it changes strings, locations, sizes, etc.

这样做会改变字符串、位置、大小等。

        System.Threading.Thread.CurrentThread.CurrentUICulture = New CultureInfo("es-ES")
        Dim res As ComponentResourceManager = New ComponentResourceManager(Me.GetType)
        For Each aControl As Control In Me.Controls
            res.ApplyResources(aControl, aControl.Name)
        Next

EDITED: You can also change thread's default culture using:

编辑:您还可以使用以下方法更改线程的默认文化:

CultureInfo.DefaultThreadCurrentCulture = New CultureInfo("es-ES")

doing this, all new forms you create in runtime will use this new CultureInfo.

这样做,您在运行时创建的所有新表单都将使用这个新的 CultureInfo。