Winforms自定义加载屏幕文本未加载

时间:2013-11-25 11:36:20

标签: vb.net winforms

我正在使用VB.NET winforms并且有一个特定的表单需要一段时间来加载所以我决定实现一个加载屏幕,让它感觉有点不那么冻结。这是我在冻结表单中使用的代码。

Private Sub HanleyView_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim loaderForm As New Loader
    loaderForm.Show()
    AllOpenOrdersList.FullRowSelect = True
    NeedsAttentionList.FullRowSelect = True
    StockManagementList.FullRowSelect = True
    Dim lowStockCount = HelperMethods.ReviewLowStock()
    ReviewLowStockButton.Text = "Review Low Stock (" & lowStockCount & ")"
    RefreshAllOpenOrdersList()
    RefreshNeedsAttentionList()
    RefreshStockManagementList()
    loaderForm.Close()
End Sub

所以我首先展示加载形式,然后关闭它。

好消息是加载表单出现,但坏消息是“加载...”文本,这是我的加载表单上的标签没有显示,我只是在那里得到一个白色补丁。我尝试了两种方法,上面的方法并调用Loader.Show和Loader.Close。我也尝试过设置loaderForm.Label1.Text =“LOADING ...”,但这没有任何区别。每次加载表单(以及标题加载“加载请等待”),而不是表单上的标签。

我现在也尝试过:

    Dim loaderForm As New Loader
    Dim lbl As New Label
    loaderForm.Controls.Add(lbl)
    lbl.Text = "LOADING..."
    lbl.Location = New System.Drawing.Point(42, 21)
    loaderForm.Show()
    AllOpenOrdersList.FullRowSelect = True
    NeedsAttentionList.FullRowSelect = True
    StockManagementList.FullRowSelect = True
    Dim lowStockCount = HelperMethods.ReviewLowStock()
    ReviewLowStockButton.Text = "Review Low Stock (" & lowStockCount & ")"
    RefreshAllOpenOrdersList()
    RefreshNeedsAttentionList()
    RefreshStockManagementList()
    loaderForm.Close()

但这也没有用。

编辑:我尝试过Varocarbas的代码,但仍然没有成功。表单加载但文本仍为白色补丁

    Dim loaderForm As Form = New Form
    With loaderForm
        .Height = 200
        .Width = 300
        .Location = New System.Drawing.Point(12, 12)
    End With

    Dim label1 As Label = New Label
    loaderForm.Controls.Add(label1)

    With label1
        .Text = "LOADING..."
        .Location = New System.Drawing.Point(12, 45)
    End With

    loaderForm.Show()

No Loading text Showing

编辑2:为清楚起见,现在这是我的代码,它正在使用Franck建议

Private Sub HanleyView_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim loaderForm As New Loader
    loaderForm.Show()
    Application.DoEvents()
    AllOpenOrdersList.FullRowSelect = True
    NeedsAttentionList.FullRowSelect = True
    StockManagementList.FullRowSelect = True
    Dim lowStockCount = HelperMethods.ReviewLowStock()
    ReviewLowStockButton.Text = "Review Low Stock (" & lowStockCount & ")"
    RefreshAllOpenOrdersList()
    RefreshNeedsAttentionList()
    RefreshStockManagementList()
    loaderForm.Close()
End Sub

我保留了原始代码,只是在loaderform.Show下面添加了Application.DoEvents(),现在它可以正常工作。

下面的截图也就是我使用设计器的意思(而不是以编程方式进行):

Loader Form Designer View

1 个答案:

答案 0 :(得分:2)

多线程!

在单独的线程上创建并运行loaderForm。但是你需要注意跨线程操作,所以在你的loaderForm上有一个自我调用的方法,例如:

Public Sub ParseStatus(msg as String)
     If Me.InvokeRequired Then Me.Invoke(New Action(Of String)(AddressOf Me.ParseStatus), msg) Else Me.Label1.Text = msg
End Sub

同样在你的loaderForm中你想要的东西:

Public Sub Finish()
      Me.DialogResult = Windows.Forms.DialogResult.OK
      Me.Close()
End Sub

然后在表格的Load程序中:

Private Sub HanleyView_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim loaderForm As New Loader
    Dim loaderThread As New Threading.Thread(New Threading.ThreadStart(AddressOf loaderForm.ShowDialog))
    loaderThread.Start()

    loaderForm.ParseStatus("Loading ...")

    AllOpenOrdersList.FullRowSelect = True
    NeedsAttentionList.FullRowSelect = True
    StockManagementList.FullRowSelect = True
    Dim lowStockCount = HelperMethods.ReviewLowStock()
    ReviewLowStockButton.Text = "Review Low Stock (" & lowStockCount & ")"

    LoaderForm.ParseStatus("Refreshing open orders ...")
    RefreshAllOpenOrdersList()

    loaderForm.ParseStatus("Refreshing needs attentions?") ' etc
    RefreshNeedsAttentionList()
    RefreshStockManagementList()

    loaderForm.Finish()
End Sub

但Visual Studio在其VB.NET项目模板中有一个称为SplashScreen的简洁东西。如果你还没有......我会使用那个。

编辑:我更正了代码中的语法错误。