I am translating a C# project into F#. While the logic part is easy, I am confused with the GUI part:
public partial class GomokuGUI : Form {
private void GomokuGUI_Load(object sender, EventArgs e)
{
this.Width = 500;
this.Height = 550;
...
this.Paint += new PaintEventHandler(GomokuGUI_Paint);
Graphics gp = this.CreateGraphics();
DrawChessbord(gp);
}
private void GomokuGUI_Paint(object sender, PaintEventArgs e)
{
Graphics gp = e.Graphics;
DrawChessbord(gp);
}
void DrawChessbord(Graphics gp)
{
float w, h;
SolidBrush br = new SolidBrush(linecolor);
Pen p = new Pen(br, frame);
gp.DrawLine(p, 20, 45, this.Width - 25, 45);
...
}
private void Form1_Click(object sender, EventArgs e) {
Graphics gp = this.CreateGraphics();
DrawChess(gp);
...
}
}
Problem: How to write above C# code in F#…Thanks
Note that F# doesn’t have any WinForms designer, so if you have some controls in the Form, you’ll need to create them manually (alternatively you can design the form in C#, compile it and reference it from F#). You could start with something like this:
It uses a couple of interesting things:
WidthandHeightin the constructor when calling the base class constructor.as thisin the class declaration, so that you can refer to the class in thedocode (which is run during construction)Addto register event handler and you can either give it a named function (e.g.patinGui) or a lambda function if you need to do only some simple call (e.g. handling ofClick)