如何使图片框在5秒钟后出现

本文关键字:5秒 何使图 | 更新日期: 2023-09-27 18:00:08

我想知道我将使用什么编码来使图片在5秒后出现?

例如

PictureBox1.visible = false

然后我想等待5秒钟,然后图片再次可见

PictureBox1.visible = true

有人能把它放进这个代码里吗?

公共分部类Form1:Form{

    int horiz, vert, step;

    public Form1()
    {
        InitializeComponent();
    }
    private void timer1_Tick_1(object sender, EventArgs e)
    {

        //image is moved at each interval of the timer
        goblin.Left = goblin.Left + (horiz * step);
        goblin.Top = goblin.Top + (vert * step);

        // if goblin has hit the RHS edge, if so change direction left
        if ((goblin.Left + goblin.Width) >= (Form1.ActiveForm.Width - step))
            horiz = -1;
        // if goblin has hit the LHS edge, if so change direction right
        if (goblin.Left <= step)
            horiz = 1;
        // if goblin has hit the bottom edge, if so change direction upwards
        if ((goblin.Top + goblin.Height) >= (Form1.ActiveForm.Height - step))
            vert = -1;
        // if goblin has hit the top edge, if so change direction downwards
        if (goblin.Top < step)
            vert = 1;
    }
    private void Form1_Load_1(object sender, EventArgs e)
    {
        //Soon as the forms loads activate the goblin to start moving 
        //set the intial direction
        horiz = 1;  //start going right
        vert = 1;   //start going down
        step = 5;
        timer1.Enabled = true;
    }
}

}

如何使图片框在5秒钟后出现

您可以使用async / await功能轻松做到这一点。

private async void YourFunction()
{
    PictureBox1.Visible = false;
    await Task.Delay(5000);
    PictureBox1.Visible = true;
}

或者,您也可以使用Timer:

private void YourFunction()
{
    Timer tm = new Timer();
    tm.Interval = 5000;
    tm.Tick += timerTick;
    PictureBox1.Visible = false;
    tm.Enabled = true;
    tm.Start();
}
private void timerTick(object sender, EventArgs e)
{
    PictureBox1.Visible = true;
    ((Timer) sender).Stop();
}
System.Threading.Thread.Sleep(5000);

这应该奏效。

Private WithEvents tim As New System.Timers.Timer
    Public Delegate Sub doSub()
    Private thingToDo As doSub
    Dim tt = New ToolTip()
    Public Sub TimeOut(d As doSub, milliseconds As Integer)
        thingToDo = d
        tim.AutoReset = False
        tim.Interval = milliseconds
        tim.Start()
    End Sub
    Private Sub tim_Elapsed(sender As Object, e As System.Timers.ElapsedEventArgs) Handles tim.Elapsed
        Invoke(thingToDo)
    End Sub

    Private Sub hide()
        picturebox.hide()
    End Sub
    Private Sub show()
        TimeOut(Address of hide, 5000)
    End Sub

至C#http://www.developerfusion.com/tools/convert/csharp-to-vb/

使用后台工作程序,这样您就可以在后台"冻结"线程,从而使应用程序响应。

看看这里:

 Private Sub Execute()
    Dim BGW As New System.ComponentModel.BackgroundWorker
    AddHandler BGW.DoWork, AddressOf WorkerDoWork
    AddHandler BGW.ProgressChanged, AddressOf WorkerProgressChanged
    AddHandler BGW.RunWorkerCompleted, AddressOf WorkerCompleted
    With BGW
        .WorkerReportsProgress = True
        .WorkerSupportsCancellation = True
        .RunWorkerAsync()
    End With
End Sub
Private Sub WorkerDoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs)
    ' Do some work
    Threading.Thread.Sleep(5000)
End Sub
Private Sub WorkerProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs)
    ' I did something!
End Sub
Private Sub WorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs)
    ' Show your picture here 
    Picturebox1.Visible = True
End Sub

这就是你的应用程序不会冻结的全部内容,你的gui也会响应。还可以查看ThreadPools或.net 中的Tasks