I believe .Location.Y and .Top properties are equivalent, so you could just use Top instead. Also you might want to use F# property initalizer syntax:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#light
open System.Windows.Forms

let form = new Form(Text="form")

let txtLog = new RichTextBox(Dock = DockStyle.Bottom, 
                             ReadOnly = true, 
                             Height = 150)

let lblOutput = new Label(Text = "Output:", Top = 150)

let txtEditor = new RichTextBox(Dock = DockStyle.Top,
                                Height = (form.Height-250))
let dc c = c :> Control
do form.Controls.AddRange([|dc txtLog; dc lblOutput; dc txtEditor|])
By on 2/15/2008 8:38 AM ()

I believe .Location.Y and .Top properties are equivalent, so you could just use Top instead. Also you might want to use F# property initalizer syntax:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#light
open System.Windows.Forms

let form = new Form(Text="form")

let txtLog = new RichTextBox(Dock = DockStyle.Bottom, 
                             ReadOnly = true, 
                             Height = 150)

let lblOutput = new Label(Text = "Output:", Top = 150)

let txtEditor = new RichTextBox(Dock = DockStyle.Top,
                                Height = (form.Height-250))
let dc c = c :> Control
do form.Controls.AddRange([|dc txtLog; dc lblOutput; dc txtEditor|])

What does the following mean?

1
2
let dc c = c :> Control
do form.Controls.AddRange([|dc txtLog; dc lblOutput; dc txtEditor|])

Also, my reasoning behind this is I'm building sort of a very simple custom compiler. Currently it's just two textboxes with a label between. The problem is that when I would resize the window the bottom box would push up over the label and the grey space between the two boxes. If I would drag it the grey area would get larger.

This leads to my next question about this, I've written the following to control the size of the texts but it doesn't seem to work, any reason why?

1
    do txtLog.Height <- (form.Height * (1/4))

It just doesn't show the box when I have that but if I substitute it for a static number it works fine.

By on 2/15/2008 9:37 AM ()
1
2
let dc c = c :> Control
do form.Controls.AddRange([|dc txtLog; dc lblOutput; dc txtEditor|])

"dc" is function that downcasts to Control. We're then using the controls AddRange Method to add an array of controls.We need the downcast so type inference will be able to work out the correct type for the array. Thats one of the small problems with type inference, it makes implicit downcast necessary occasionally because the compiler has less type information to work with.

Your positioning problems are related to the Docking layout system. In winforms controls controls are static unless they are docked or anchored. The 2 RichTextBoxes are docked so will resize automatically the label is not so it won't. The docking system isn't that great if you have more that two controls as it gives you little control over how controls resize themselves, so you need to use anchoring:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#light
open System.Windows.Forms

let form = new Form(Text="form")

let txtLog = new RichTextBox(Anchor = (AnchorStyles.Left ||| AnchorStyles.Right ||| AnchorStyles.Bottom), 
                             Width = form.Width,
                             ReadOnly = true, 
                             Height = 150,
                             Top = form.ClientRectangle.Height - 150)

let lblOutput = new Label(Anchor = (AnchorStyles.Left ||| AnchorStyles.Right ||| AnchorStyles.Bottom),
                          Text = "Output:", Top = txtLog.Top - 16)

let txtEditor = new RichTextBox(Anchor = (AnchorStyles.Left ||| AnchorStyles.Right ||| AnchorStyles.Top),
                                Width = form.Width,
                                Height = (form.Height-250))
let dc c = c :> Control
do form.Controls.AddRange([|dc txtLog; dc lblOutput; dc txtEditor|])
By on 2/15/2008 11:27 AM ()

Thanks, that exactly what I was looking for. Just so I understand, is the operator '|||' like an 'AND'?

By on 2/18/2008 9:05 AM ()

The '|||' operator is actually a bitwise-OR. It is used to combine multiple options like AnchorStyles.Left and AnchorStyles.Top. This is a popular technique to combine options since it allows easy extraction using a bitmask and a bitwise-AND operator.

Basically, each option sets a single, different bit of an integer. Then, when the options are ORed, all the bits that are set represent options the user has selected. To find out if the user has selected a particular option, you AND the options integer with a mask that represents the desired option. You can find a more thorough explanation at [link:en.wikipedia.org] and [link:www.mvps.org].

Regards,

z.

By on 2/18/2008 1:26 PM ()
1
do lblOutput.Location.Y <- 150

Even if F# let you do this, it wouldn't do want you want it to. Location is a property on Label and is of type Point. Point is a value type. So when you access lblOutput.Location you are actually creating a local copy of the label's location because value types like Point are passed by value not reference. So then if you assigned Location.Y <- 150 you would actually be mutating the local copy of Location and not the label's actual location. So the label wouldn't move. Instead you need to do this

1
do lblOutput.Location <- Point(lblOutput.Location.X, 150)

Now you are setting a property, rather than mutating a field on a value type returned from getting a property. If that makes sense.

By on 2/15/2008 12:24 AM ()

Thanks, that fixed it. I had tried something similar where I tried to create a new Point and set the point properties, then set the location to the point but I didn't get far in that.

By on 2/15/2008 8:16 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper