vb.net 在 Sub 运行时显示沙漏或请稍候消息

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

Display hourglass or Please Wait message while Sub runs

vb.net

提问by Jose M.

I have the following sub running on my form. It can take a while for the sub to run so I want to change the cursor to an hourglass and display a Please Wait message while the code is running. Here is my procedure:

我的表单上运行了以下子程序。子程序可能需要一段时间才能运行,因此我想将光标更改为沙漏并在代码运行时显示请稍候消息。这是我的程序:

Public Sub GoToSheets(sheetName As String)

'This sub is used to open the workbook on the selected sheet.
'This checks to see if Excel workbook is opened, if not it
'opens Excel, the workbook and then the selected sheet. If the workbook is
'opened, it goes to the selected sheet.

'@param sheetName, sheet to be displayed

Try
    'get an existing excel.application object
    xlApp = CType(GetObject(, "Excel.Application"), Application)
Catch ex As Exception
    'no existing excel.application object - create a new one

    xlApp = New Excel.Application

End Try

Dim xlWBName As String = "2011.1004.Compensation Template"
Dim xlBookPath As String = Path.Combine(Directory.GetCurrentDirectory())

xlApp.Visible = True

Try
    'get the opened workbook
    xlBook = xlApp.Workbooks(xlWBName & ".xlsx")
Catch ex As Exception
    'open it
    xlBook = xlApp.Workbooks.Open(xlBookPath & "\" & xlWBName & ".xlsx")
End Try

Try

    xlSheet = CType(CType(xlBook.Sheets("summarySheet"), Excel.Worksheet), Excel.Worksheet)
    Dim strChckRange As String = xlSheet.Range("A2").Value

    If strChckRange Is Nothing Then

        Dim frmClientInfo As New frmClientInformation
        frmClientInfo.ShowDialog()

        closeXLApp()

    Else


        xlSheet = CType(CType(xlBook.Sheets(sheetName), Excel.Worksheet), Excel.Worksheet)


        'close the navigation instance on the welcome page
        frmNavigation.Close()
        'activate requested sheet
        xlSheet.Activate()
        'display as dashboard
        DashboardView()

        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)

        GC.Collect()
        GC.WaitForPendingFinalizers()
        GC.Collect()
        GC.WaitForPendingFinalizers()

        frmWelcomePage.Hide()
        chkForm()

    End If

Catch ex As Exception

End Try

End Sub

结束子

I have done some research on this, but so far nothing for Visual Basic.

我对此进行了一些研究,但到目前为止对 Visual Basic 没有任何研究。

采纳答案by Karl Anderson

I suggest creating a PleaseWaitFormthat has the label of Please Wait...message on it and then have the form shown as mode-less, change the cursor to hourglass, do Excel work, change cursor back to default and hide the PleaseWaitForm.

我建议创建一个上面PleaseWaitFormPlease Wait...消息标签的表单,然后将表单显示为无模式,将光标更改为沙漏,执行 Excel 工作,将光标更改回默认值并隐藏PleaseWaitForm.

Dim pleaseWait As New PleaseWaitForm
pleaseWait.Show()

' Set cursor as hourglass
Cursor.Current = Cursors.WaitCursor

Application.DoEvents

' Execute your GoToSheets method here

' Set cursor as default arrow
Cursor.Current = Cursors.Default

' Hide the please wait form
pleaseWait.Hide()