vb.net Visual Basic 2010 检查表单加载时文件是否存在

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

Visual basic 2010 Check if file exists on form load

.netvb.netvisual-studio-2010if-statement

提问by user1955680

I would like my Visual Basic app to run a splash screen with a progress bar and after that to check if file exists, but I have a problem since as soon as I start and the splash screen shows up the file checker fires up, but I would like it to check when form1loads and not splash screen. Here is my code, I could use an advice:

我希望我的 Visual Basic 应用程序运行带有进度条的启动画面,然后检查文件是否存在,但是我遇到了一个问题,因为一旦我启动并且启动画面显示文件检查器就会启动,但是我希望它检查何时form1加载而不是启动屏幕。这是我的代码,我可以使用一个建议:

Splash screen :

启动画面:

Public NotInheritable Class SplashScreen1

    'TODO: This form can easily be set as the splash screen for the application by going to the "Application" tab
    '  of the Project Designer ("Properties" under the "Project" menu).       

    Private Sub Splashscreen1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim Dte As DateTime = DateTime.Now
        Label2.Text = FormatDateTime(Dte, DateFormat.LongDate)

        Timer1.Start()
        Timer2.Start()

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If ProgressBar1.Value = ProgressBar1.Maximum Then
            Form1.Show()
            Me.Close()
        End If

    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        ProgressBar1.PerformStep()
    End Sub
End Class

Form 1 :

表格一:

Imports System.Net
Imports System.IO
Public Class Form1
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If My.Computer.FileSystem.FileExists(Application.StartupPath & "/read me.txt") Then
            MsgBox("file found")
        Else
            MsgBox("not found")
        End If

    End Sub
End Class

回答by Jason Tyler

Try moving your Form1 code from the Load event to the Shown event. Load is run before the form is visible to the user, which is not what you want from what I understand.

尝试将您的 Form1 代码从 Load 事件移动到 Shown 事件。Load 在表单对用户可见之前运行,根据我的理解,这不是您想要的。

回答by Donny McCoy

Your timer1_tick event is causing this issue. Timer1 is enabled at splashscreen load and depending on the timer_interval it may never display before form1 loads. Do you want the user to see something during this time?

您的 timer1_tick 事件导致了此问题。Timer1 在启动画面加载时启用,并且根据 timer_interval 它可能永远不会在 form1 加载之前显示。你想让用户在这段时间内看到什么吗?

The progress bar implies an action or set of actions is taking place. What do you have taking place that requires the progress bar? If you want the illusion of things happening, make sure you timer1 interval is greater than your timer2 interval since timer1 controls how long the splashscreen is visible before form1 is loaded.

进度条暗示正在发生一个动作或一组动作。你有什么事情需要进度条?如果您想要事情发生的错觉,请确保 timer1 间隔大于 timer2 间隔,因为 timer1 控制在加载 form1 之前启动画面可见的时间。

回答by s3ktonic

If ProgressBar1.Value = ProgressBar1.Maximum Then
      Form1.Show()
      Me.Close()
End If

change to:

改成:

If ProgressBar1.Value = ProgressBar1.Maximum Then
      Form1.Visible = True
      Me.Visible = False
End If