【问题标题】:Using a VB.NET variable between different forms在不同表单之间使用 VB.NET 变量
【发布时间】:2016-05-23 15:40:46
【问题描述】:

我正在尝试使用 VB.NET 程序设置在每次加载表单时为每个表单加载背景图像。到目前为止,我已经设法让程序以一种形式设置背景,并且改变了其他所有形式的背景。但是,当每个窗体在程序运行时关闭并重新打开时,背景会变回默认设置。我需要以某种方式更改背景一次并在每次打开表单时加载它,以便在程序运行时每次重新打开表单时它都不会切换回来。我认为有一些方法可以使用 VB.NET 中的 My.Settings 来执行此操作,但我不确定。

这是更改每个表单的背景的代码:

Me.BackgroundImage = PreviewBackgroundBox.Image
MainForm.BackgroundImage = PreviewBackgroundBox.Image
LogInForm.BackgroundImage = PreviewBackgroundBox.Image

PreviewBackgroundBox 用于在用户应用之前向用户显示图像,然后当他们单击应用时,图像将从PreviewBackgroundBox 中获取并设置为所有表单的背景。

有人可以帮我解决这个问题吗?

谢谢!

【问题讨论】:

  • PreviewBackgroundBox 是表单类名吗?
  • PreviewBackgroundBox 是表单上的一个图片框

标签: vb.net forms


【解决方案1】:

基本上,您需要一个字典来记住要为每个表单显示的图像。您可以将这样的字典与处理相关逻辑的方法一起存储在模块中

Private imageDict As New Dictionary(Of String, Image)

Public Sub SetImage(ByVal formName As String, ByVal img As Image)
    imageDict(formName) = img
End Sub

Public Function GetImage(ByVal formName As String) As Image
    Dim img As Image
    If imageDict.TryGetValue(formName, img) Then
        Return img
    End If
    Return Nothing 'Or return a default image
End Function

注意:字典存储一些数据并将其与用于检索此数据的键相关联。在这里,我将使用表单的名称作为 Key。您也可以使用表单的类型 GetType(Form1)Me.GetType() 并改用 Dictionary(Of Type, Image)

每当用户选择另一个图像时调用SetImage 以记住它。打开表单时调用GetImage 以获取记住的图像。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-25
    • 2013-12-09
    • 1970-01-01
    • 2014-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多