vb.net 使用 VB .NET 4+ 从另一个窗体重新加载 Windows 窗体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16619677/
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 13:38:11  来源:igfitidea点击:

Reload a Windows Form from another Form with VB .NET 4+

vb.netwinformsvisual-studio-2010

提问by Tearstar

Using Visual Studio 2010, WinForms, and VB.Net, I am trying to use one form as a "User Settings" page (Form2) that contains a "Theme" variable for Form1. Basically after a user enters a different theme name (which will eventually be a dropdown list) into the user settings and clicks the save button, I want Form1 to be redrawn or reloaded with the new theme settings.

使用 Visual Studio 2010、WinForms 和 VB.Net,我尝试将一个表单用作包含 Form1 的“主题”变量的“用户设置”页面 (Form2)。基本上,在用户在用户设置中输入不同的主题名称(最终将是一个下拉列表)并单击保存按钮后,我希望使用新的主题设置重新绘制或重新加载 Form1。

I have tried to use refresh, hide/show Form1, recall the form_load event, and many other things. I have looked online for a method to redraw or unload/reload the form1 with the new theme setting, but I haven't found anything that works and will change the color settings of form1 once the user saves the user settings on form2.

我曾尝试使用刷新、隐藏/显示 Form1、调用 form_load 事件以及许多其他功能。我在网上寻找一种使用新主题设置重绘或卸载/重新加载 form1 的方法,但我没有找到任何有效的方法,一旦用户将用户设置保存在 form2 上,就会更改 form1 的颜色设置。

According to the logic and the documents I have read, one of these options should work, but none of them change the background color of the form1. I have tried calling the ColorChange on load event, on shown event, etc.

根据我阅读的逻辑和文档,这些选项之一应该有效,但它们都不会更改 form1 的背景颜色。我曾尝试在加载事件、显示事件等上调用 ColorChange。

I don't want to use the Color Dialog because I don't want the user to select individual colors. I want them to choose from a selection of pre-designed themes that are saved in the user settings. This is only a test project so you can let me know the good and bad about doing it this way while suggesting other ways to do it, but I would much rather understand the reason that none of these options work and figure out a way to make it work if it is possible.

我不想使用颜色对话框,因为我不想让用户选择单个颜色。我希望他们从保存在用户设置中的一系列预先设计的主题中进行选择。这只是一个测试项目,所以你可以让我知道这样做的好处和坏处,同时建议其他方法,但我更愿意理解这些选项都不起作用的原因,并找出一种方法如果可能,它会起作用。

Form1

表格1

Dim selectedTheme As String = My.Settings.Theme

Sub ColorChange()
    Select Case selectedTheme
        Case "Desert"
            Me.BackColor = Color.Brown
        Case "Default"
            Me.BackColor = Color.Black
            lblErrorMsgs.ForeColor = Color.Red
    End Select

End Sub

Form2

表格2

Private Sub btnSaveSettings_Click(sender As Object, e As EventArgs) Handles btnSaveSettings.Click
    My.Settings.Theme = txtTheme.Text
    My.Settings.Save()

    Form1.Refresh()
    Form1.Show()

    Me.Hide()

End Sub

回答by rene

The problem is that with calling Refreshor Shownone of the code you have shown here in your Form1 is going to be executed. So your variable selectedThemestill holds the previous value and your colorchange is not invoked.

问题是调用RefreshShow没有您在 Form1 中显示的代码将被执行。因此,您的变量selectedTheme仍保留先前的值,并且不会调用您的 colorchange。

Fortunately the My.Settingsinstance derives from ApplicationBaseSettingswhich provides the PropertyChangedevent.

幸运的是,该My.Settings实例派生自提供事件的ApplicationBaseSettingsPropertyChanged

By adding an event handler to your form load event you can use that feature:

通过向表单加载事件添加事件处理程序,您可以使用该功能:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    AddHandler My.Settings.PropertyChanged, AddressOf HandlePropChanged
End Sub

Now we need a method that will process the that event:

现在我们需要一个方法来处理那个事件:

'Imports System.ComponentModel
Private Sub HandlePropChanged(sender As Object, e As PropertyChangedEventArgs)
    If e.PropertyName = "Theme" Then
        selectedTheme = My.Settings.Theme
        ColorChange()
    End If
End Sub

As you can see the PropertyChangedEventArgshas a PropertyNamethat tells you which property actually changed.

正如您所看到的,PropertyChangedEventArgs有一个PropertyName告诉您实际更改了哪个属性。

You can now change the My.Setting properties anywhere in your app without having to call Refresh or Show after doing so.

您现在可以在应用程序的任何位置更改 My.Setting 属性,而无需在更改后调用 Refresh 或 Show。

Your code in Form2 gets reduced to:

您在 Form2 中的代码简化为:

Private Sub btnSaveSettings_Click(sender As Object, e As EventArgs) Handles btnSaveSettings.Click
    My.Settings.Theme = txtTheme.Text
    My.Settings.Save()

    Me.Hide()

End Sub