I still haven't figured this out and will look at it tonight but in case it helps, here's my full code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
    open System
    open System.IO
    open System.Windows.Forms
    open System.Drawing
    open System.Diagnostics
    open System.Collections.Generic
    
    let form = new Form(Text = "SCompiler", Height = 768, Width = 1024) 
    
    type document =
    {
        filename: string;
        text: string;
    }

    let documentList = new List<document>()
    let newDocument = {filename="New Document"; text="";}
    do documentList.Add(newDocument)
    
    (* Menu bar, menus *)
    let mMain = form.Menu <- new MainMenu()
    let mFile = form.Menu.MenuItems.Add("&File")
    let miNew = new MenuItem("New")
    do  mFile.MenuItems.Add(miNew) |> ignore
    let miOpen = new MenuItem("Open...")
    do  mFile.MenuItems.Add(miOpen) |> ignore
    let miSave = new MenuItem("Save", ShowShortcut = true, Shortcut = Shortcut.CtrlS)
    do  mFile.MenuItems.Add(miSave) |> ignore
    let miSaveAs = new MenuItem("Save As...")
    do  mFile.MenuItems.Add(miSaveAs) |> ignore
    let miQuit  = new MenuItem("E&xit")
    do  mFile.MenuItems.Add(miQuit) |> ignore

    let mCompile = form.Menu.MenuItems.Add("Compile")
    let miCompileDocument = new MenuItem("Compile Document", ShowShortcut = true, Shortcut = Shortcut.CtrlShiftB)
    do  mCompile.MenuItems.Add(miCompileDocument) |> ignore
    let miCompileSolution = new MenuItem("Compile All Documents", ShowShortcut = true, Shortcut = Shortcut.CtrlShiftP)
    do  mCompile.MenuItems.Add(miCompileSolution) |> ignore

    let mHelp = form.Menu.MenuItems.Add("&Help")
    let miAbout = new MenuItem("&About")
    do  mHelp.MenuItems.Add(miAbout) |> ignore
    let miOnlineHelp = new MenuItem("Online Help", ShowShortcut = true, Shortcut = Shortcut.F1)
    do  mHelp.MenuItems.Add(miOnlineHelp) |> ignore

    (* Form Controls *)
    let txtLog = new RichTextBox(Text = "", Height = 150, Width = form.Width-40, Location = Point(20, form.Height-200),
        Anchor = (AnchorStyles.Bottom ||| AnchorStyles.Left ||| AnchorStyles.Right), ReadOnly = true)
    do form.Controls.Add(txtLog)
    
    let txtEditor = new TextBox(Text = "", Height = 500, Width = form.Width - 220, Location = Point(20, 40), 
        Anchor = (AnchorStyles.Left ||| AnchorStyles.Top ||| AnchorStyles.Right ||| AnchorStyles.Bottom), AcceptsTab = true,
        TabIndex = 0, Multiline = true, ScrollBars = ScrollBars.Vertical)
    do form.Controls.Add(txtEditor)
    
    let lblOutput = new Label(Text = "Output Log:", Location = Point(20, form.Height-220), Anchor = (AnchorStyles.Left ||| AnchorStyles.Bottom))
    do form.Controls.Add(lblOutput)

    let lblProject = new Label(Text = "Project Files:", Location = Point(form.Width-190, 20), Anchor = (AnchorStyles.Top ||| AnchorStyles.Right))
    do form.Controls.Add(lblProject)
    
    let lstProject = new ListBox(Height = form.Height - 290, Width = 180, Location = Point(form.Width-190, 55), 
        Anchor = (AnchorStyles.Right ||| AnchorStyles.Top ||| AnchorStyles.Bottom))
    let temp = documentList.Count-1
    do for i = 0 to temp do
        newDocument = documentList.Item(i) |> ignore;
        lstProject.Items.Add(newDocument.filename) |> ignore;
    done;
    do lstProject.SelectedIndex <- 0
    do form.Controls.Add(lstProject)
    
    let lblCurrDoc = new Label(Text = "Current Document:", Location = Point(20, 20), Anchor = (AnchorStyles.Top ||| AnchorStyles.Left))
    do lblCurrDoc.Text <- lblCurrDoc.Text + " " + lstProject.SelectedItem.ToString()
    do lblCurrDoc.AutoSize <- true
    do form.Controls.Add(lblCurrDoc)

    (* callbacks *)
    let opExitForm sender args = form.Close ()
    do miQuit .add_Click (new EventHandler(opExitForm))
    
    let opInternetHelp sender args =  
        let ieProcess = new Process() in
        ieProcess.StartInfo.UseShellExecute <- true;
        ieProcess.StartInfo.FileName <- "http://matt.insidegamer.org/scompiler/";
        ieProcess.Start() |> ignore;;

    do miOnlineHelp .add_Click (new EventHandler(opInternetHelp))
    
    let opNewFile sender args =
        documentList.Add({filename="New Document"; text="";});
            let updateDoc = documentList.Item(lstProject.SelectedIndex) in
                documentList.RemoveAt(lstProject.SelectedIndex);
                documentList.Insert(lstProject.SelectedIndex, updateDoc);
                lstProject.Items.Clear() |> ignore;
                temp = documentList.Count |> ignore;
                for i = 0 to temp do
                    newDocument = documentList.Item(i) |> ignore;
                    lstProject.Items.Add(newDocument.filename) |> ignore;
                done;
                lstProject.SelectedIndex <- lstProject.Items.Count;
    
    do miNew .add_Click (new EventHandler(opNewFile))
    
    let opOpenFile sender args =
        let ofdDialog = new OpenFileDialog(Filter = "SCompiler Files (*.scpt)|*.scpt", Title = "Open Script File") in
            if ofdDialog.ShowDialog() = DialogResult.OK then
                match ofdDialog.OpenFile() with
                | null -> txtLog.AppendText("File Open Failed")
                | stream ->
                    let r = new StreamReader(stream) in
                        documentList.Add({filename=ofdDialog.FileName.Remove(0, ofdDialog.FileName.LastIndexOf("\\") + 1); text=r.ReadToEnd();});
                        //lstProject.Items.Add(ofdDialog.FileName.Remove(0, ofdDialog.FileName.LastIndexOf("\\") + 1)) |> ignore;
                        lstProject.Items.Clear() |> ignore;
                        temp = documentList.Count |> ignore;
                        for i = 0 to temp do
                            newDocument = documentList.Item(i) |> ignore;
                            lstProject.Items.Add(newDocument.filename) |> ignore;
                        done;
                        txtEditor.Text <- r.ReadToEnd();
                        txtLog.AppendText("\nFile Loaded: " + ofdDialog.FileName.Remove(0, ofdDialog.FileName.LastIndexOf("\\") + 1));;
                        
    do miOpen .add_Click (new EventHandler(opOpenFile))
    
    let opSaveFile sender args =
        let ofdDialog = new SaveFileDialog(Filter = "SCompiler Files (*.scpt)|*.scpt|All Types|*.*", Title = "Save Script As...") in
            if ofdDialog.ShowDialog() = DialogResult.OK then
                match ofdDialog.OpenFile() with
                | null -> txtLog.AppendText("File Save Failed")
                | stream ->
                    let r = new StreamWriter(stream) in
                    r.Write(txtEditor.Text);
                    r.Close();;
                    
    do miSave .add_Click (new EventHandler(opSaveFile))
    
    let opSaveAsFile sender args =
        let ofdDialog = new SaveFileDialog(Filter = "SCompiler Files (*.scpt)|*.scpt|All Types|*.*", Title = "Save Script As...") in
            if ofdDialog.ShowDialog() = DialogResult.OK then
                match ofdDialog.OpenFile() with
                | null -> txtLog.AppendText("File Save As Failed")
                | stream ->
                    let r = new StreamWriter(stream) in
                    r.Write(txtEditor.Text);
                    r.Close();;
                    
    do miSaveAs .add_Click (new EventHandler(opSaveAsFile))
    
    let opAbout sender args =
        MessageBox.Show("Created by Matt Christian\n\nProgrammed with Microsoft F#\n\nVer 1.0.0","About", MessageBoxButtons.OK, MessageBoxIcon.Question) |> ignore;;
    
    do miAbout .add_Click (new EventHandler(opAbout))        
    
    (* run! *)
    [<STAThread()>]  //Necessary to open file dialogs
    do Application.Run(form)
By on 2/27/2008 4:25 PM ()

Having the full code really helped. I'd been looking at the documentList.Add, which is completely correct, the mistake is a little further on.

The problem is your missing using the = operator. When not used with a let binding equals is an equality test, so in the line "temp = documentList.Count |> ignore" your actually creating a boolean then throwing it way, there were quite a few places where you'd done this. Here's a corrected version:

1
2
3
4
5
6
7
8
9
10
11
12
13
    let opNewFile sender args =
        documentList.Add({filename="New Document"; text="";});
            let updateDoc = documentList.Item(lstProject.SelectedIndex) in
                documentList.RemoveAt(lstProject.SelectedIndex);
                documentList.Insert(lstProject.SelectedIndex, updateDoc);
                lstProject.Items.Clear();
                for i = 0 to documentList.Count - 1 do
                    let newDocument = documentList.Item(i) in
                    lstProject.Items.Add(newDocument.filename) |> ignore;
                done;
                lstProject.SelectedIndex <- lstProject.Items.Count - 1;
    
    do miNew .add_Click (new EventHandler(opNewFile))

However I think you could really benerfit from using databinding. This means the document list would "bound" to the project list so there's no need to update it manually. You can no longer use the lstProjets.Items property, but your almost always better off as you just work directly with documentList instead. I've put the entire revised code below, as there's changes in a few differnt places need. Also I may not have removed every use of "lstProjets.Items", but adding a new file and opening a file seem to work okay.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
    open System
    open System.IO
    open System.Windows.Forms
    open System.Drawing
    open System.Diagnostics
    open System.Collections.Generic
    open System.ComponentModel
    
    let form = new Form(Text = "SCompiler", Height = 768, Width = 1024) 
    
    type document =
    {
        filename: string;
        text: string;
    }
    with
        override x.ToString() = x.filename
    end
        
    let documentList = new BindingList<document>()
    //let documentList = new List<document>()
    let newDocument = {filename="New Document"; text="";}
    do documentList.Add(newDocument)
    
    (* Menu bar, menus *)
    let mMain = form.Menu <- new MainMenu()
    let mFile = form.Menu.MenuItems.Add("&File")
    let miNew = new MenuItem("New")
    do  mFile.MenuItems.Add(miNew) |> ignore
    let miOpen = new MenuItem("Open...")
    do  mFile.MenuItems.Add(miOpen) |> ignore
    let miSave = new MenuItem("Save", ShowShortcut = true, Shortcut = Shortcut.CtrlS)
    do  mFile.MenuItems.Add(miSave) |> ignore
    let miSaveAs = new MenuItem("Save As...")
    do  mFile.MenuItems.Add(miSaveAs) |> ignore
    let miQuit  = new MenuItem("E&xit")
    do  mFile.MenuItems.Add(miQuit) |> ignore
    let mCompile = form.Menu.MenuItems.Add("Compile")
    let miCompileDocument = new MenuItem("Compile Document", ShowShortcut = true, Shortcut = Shortcut.CtrlShiftB)
    do  mCompile.MenuItems.Add(miCompileDocument) |> ignore
    let miCompileSolution = new MenuItem("Compile All Documents", ShowShortcut = true, Shortcut = Shortcut.CtrlShiftP)
    do  mCompile.MenuItems.Add(miCompileSolution) |> ignore
    let mHelp = form.Menu.MenuItems.Add("&Help")
    let miAbout = new MenuItem("&About")
    do  mHelp.MenuItems.Add(miAbout) |> ignore
    let miOnlineHelp = new MenuItem("Online Help", ShowShortcut = true, Shortcut = Shortcut.F1)
    do  mHelp.MenuItems.Add(miOnlineHelp) |> ignore
    (* Form Controls *)
    let txtLog = new RichTextBox(Text = "", Height = 150, Width = form.Width-40, Location = Point(20, form.Height-200),
        Anchor = (AnchorStyles.Bottom ||| AnchorStyles.Left ||| AnchorStyles.Right), ReadOnly = true)
    do form.Controls.Add(txtLog)
    
    let txtEditor = new TextBox(Text = "", Height = 500, Width = form.Width - 220, Location = Point(20, 40), 
        Anchor = (AnchorStyles.Left ||| AnchorStyles.Top ||| AnchorStyles.Right ||| AnchorStyles.Bottom), AcceptsTab = true,
        TabIndex = 0, Multiline = true, ScrollBars = ScrollBars.Vertical)
    do form.Controls.Add(txtEditor)
    
    let lblOutput = new Label(Text = "Output Log:", Location = Point(20, form.Height-220), Anchor = (AnchorStyles.Left ||| AnchorStyles.Bottom))
    do form.Controls.Add(lblOutput)
    let lblProject = new Label(Text = "Project Files:", Location = Point(form.Width-190, 20), Anchor = (AnchorStyles.Top ||| AnchorStyles.Right))
    do form.Controls.Add(lblProject)
    
    let lstProject = new ListBox(Height = form.Height - 290, Width = 180, Location = Point(form.Width-190, 55), 
        Anchor = (AnchorStyles.Right ||| AnchorStyles.Top ||| AnchorStyles.Bottom))
    let temp = documentList.Count-1
    do lstProject.DataSource <- documentList;
    do form.Controls.Add(lstProject)
    
    let lblCurrDoc = new Label(Text = "Current Document:", Location = Point(20, 20), Anchor = (AnchorStyles.Top ||| AnchorStyles.Left))
    
    do lblCurrDoc.AutoSize <- true
    do form.Controls.Add(lblCurrDoc)
    
    (* callbacks *)

    do form.Load.Add(fun _ -> 
        lstProject.SelectedIndex <- 0;
        lblCurrDoc.Text <- (lblCurrDoc.Text + " " + lstProject.SelectedItem.ToString()))
    
    let opExitForm sender args = form.Close ()
    do miQuit .add_Click (new EventHandler(opExitForm))
    
    let opInternetHelp sender args =  
        let ieProcess = new Process() in
        ieProcess.StartInfo.UseShellExecute <- true;
        ieProcess.StartInfo.FileName <- "<A href="http://matt.insidegamer.org/scompiler/">http://matt.insidegamer.org/scompiler/</A>";
        ieProcess.Start() |> ignore;;
    do miOnlineHelp .add_Click (new EventHandler(opInternetHelp))
    
    do miNew.Click.Add (fun _ -> 
        documentList.Add({filename="New Document"; text="";});
        lstProject.SelectedIndex <- documentList.Count - 1)
    
    let opOpenFile _ =
        let ofdDialog = new OpenFileDialog(Filter = "SCompiler Files (*.scpt)|*.scpt", Title = "Open Script File") in
            if ofdDialog.ShowDialog() = DialogResult.OK then
                match ofdDialog.OpenFile() with
                | null -> txtLog.AppendText("File Open Failed")
                | stream ->
                    let r = new StreamReader(stream) in
                        documentList.Add({filename=ofdDialog.FileName.Remove(0, ofdDialog.FileName.LastIndexOf("\\") + 1); text=r.ReadToEnd();});
                        txtEditor.Text <- r.ReadToEnd();
                        txtLog.AppendText("\nFile Loaded: " + ofdDialog.FileName.Remove(0, ofdDialog.FileName.LastIndexOf("\\") + 1));;
                        
    do miOpen.Click.Add(opOpenFile)
    
    let opSaveFile sender args =
        let ofdDialog = new SaveFileDialog(Filter = "SCompiler Files (*.scpt)|*.scpt|All Types|*.*", Title = "Save Script As...") in
            if ofdDialog.ShowDialog() = DialogResult.OK then
                match ofdDialog.OpenFile() with
                | null -> txtLog.AppendText("File Save Failed")
                | stream ->
                    let r = new StreamWriter(stream) in
                    r.Write(txtEditor.Text);
                    r.Close();;
                    
    do miSave .add_Click (new EventHandler(opSaveFile))
    
    let opSaveAsFile sender args =
        let ofdDialog = new SaveFileDialog(Filter = "SCompiler Files (*.scpt)|*.scpt|All Types|*.*", Title = "Save Script As...") in
            if ofdDialog.ShowDialog() = DialogResult.OK then
                match ofdDialog.OpenFile() with
                | null -> txtLog.AppendText("File Save As Failed")
                | stream ->
                    let r = new StreamWriter(stream) in
                    r.Write(txtEditor.Text);
                    r.Close();;
                    
    do miSaveAs .add_Click (new EventHandler(opSaveAsFile))
    
    let opAbout sender args =
        MessageBox.Show("Created by Matt Christian\n\nProgrammed with Microsoft F#\n\nVer 1.0.0","About", MessageBoxButtons.OK, MessageBoxIcon.Question) |> ignore;;
    
    do miAbout .add_Click (new EventHandler(opAbout))        
    
    (* run! *)
    [<STAThread()>]  //Necessary to open file dialogs
    do Application.Run(form)
By on 2/27/2008 9:55 PM ()

Thanks! That worked great. I also like your suggestion about the BindingList and have implemented that like you had mentioned.

Just for some clarification, you said it was creating a bool and then instantly tossing it out basically, is the because of the |>ignore attachment?

By on 2/28/2008 9:51 AM ()

Yes, the ignore function is for throwing away values, so you should use it sparingly. Generally it is only needed when you call a function that has a side effect and also returns a value.

Cheers,
Rob

By on 2/29/2008 12:34 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