将对象保存在列表中并使用F#在Windows窗体中绘制它们

时间:2014-01-07 16:39:27

标签: f# c#-to-f#

我目前正在从C#背景学习F#,当我学习C#时我做了类似的项目,这就是为什么我在F#中尝试同样的事情,我想要创建的是一个非常简单的2D图形编辑器使用Windows窗体和F#,我遇到了麻烦。

我这样做的方式(正如我在C#中所做的那样)是通过按菜单按钮创建不同的形状,例如我在菜单中按“square”,我希望程序从预定义创建一个方形对象输入,将其添加到所有其他方形对象的列表中,然后重新绘制表单(无效)。

对于一位经验丰富的F#程序员来说,听起来相当简单,无论如何,这就是我所拥有的。

我有我的广场

type square(x : float, y : float) =
    let length = 10
    let height = 10

我有我的菜单和表格以及所有内容

let form =
    let temp = new Form(Width= 900, Height = 500,Text="Graphics Editor")
    temp.BackColor <- Color.White
    let menu = new MenuStrip()
    let create = new ToolStripDropDownButton("Create")  // Menu
    ignore(menu.Items.Add(create))
    let square = create.DropDownItems.Add("Square")

和一个空的正方形列表

let listOfSquares = []

现在问题。

我想创建一个正方形,然后在单击菜单选项Square时将其添加到空的正方形列表中。像这样的东西

square.Click.Add(fun _ -> let sq = circle(3.0, 4.0), sq :: listOfSquares |> ignore) //I know the syntax here is off but I hope it shows what I'm tryng to acomplish

1 个答案:

答案 0 :(得分:2)

如果您想改变列表,请尝试使用ResizeArray。 F#列表是不可变的。

let listOfSquares = ResizeArray()

square.Click.Add(fun _ -> listOfSquares.Add(circle(3.0, 4.0)))