如何在F#或C#中更新Windows窗体?

时间:2018-12-07 21:38:34

标签: c# winforms f#

比方说,我希望表格的大小为100x100,然后在1秒后为200x200,我该怎么做?运行表单后,我是否要更新大小?设置大小后再使用Thread.Sleep(),然后设置新大小吗?我真的很困惑如何做到这一点。最终,我希望在屏幕上有一个位图,该位图将由箭头键控制。但是只是为了让我更好地了解如何使下面的代码正常工作?

open System
open System.Drawing
open System.Windows.Forms
open System.Threading

let form = new Form()

form.Size <- new Size(100, 100)

Thread.Sleep(1000)

form.Size <- new Size(200, 200) // ???

do Application.Run form

第二次尝试

open System
open System.Drawing
open System.Windows.Forms

let form = new Form()

form.Size <- new Size(100, 100)

do Application.Run form

let timer = new Timer()
timer.Interval <- 1000
timer.Start()

form.Size <- new Size(200, 200)

1 个答案:

答案 0 :(得分:0)

这是一种实现方法:

open System
open System.Drawing
open System.Windows.Forms
open System.Threading

let form = new Form()

form.Size <- new Size(100, 100)

async {
    do! Async.Sleep 1000
    form.Size <- new Size(200, 200)
} |> Async.Start

do Application.Run form