2

i have made a winform1 dll in C# and try to use it in F# beacause the visual F# doesn't have the visual designer. What i want to do is like this

//F# pesudo code
let obj1 = new winform1()
obj1.show()// 
let mutable i = 1
while true do
    thread.sleep 1000
    i<i+1
    obj1.shownumber i

in other words, i am simulating to show something in a form over time. Can you hint me something. Thank you in advance!

Constantine
  • 164
  • 9
  • And what exactly doesn't work? - that code looks OK at first glance. – John Palmer Jan 28 '14 at 05:04
  • @JohnPalmer it's not setting up a proper message loop so it won't be able to handle any events – JaredPar Jan 28 '14 at 05:13
  • @JaredPar - I kind of guessed that - I just wanted the OP to actually explain his problem so there was an actual question. – John Palmer Jan 28 '14 at 05:15
  • john and jaredPar , thanks for your reply. THe problem is , if i use obj.showdialog(),which is some kind of "modal form", the program will be stoped there and don't contiunue. And if i use obj.show(),which is "modaless form" or "non-modal form", it the form will just flash and disappear at once. \ – Constantine Jan 28 '14 at 07:22

1 Answers1

5

To actually run the initial form for a WinForms application you need to setup a a proper message loop. The best way to do this is with the Application.Run method.

open System.Windows.Forms

let obj1 = new winform1()
Application.Run(obj1)

Note that the standard C# WinForms application adds 2 other statements. While I don't know their exact purpose I would include them as well

Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(false)
Application.Run(obj1)
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • For `SetCompatibleTextRenderingDefault` see: http://msdn.microsoft.com/de-de/library/system.windows.forms.application.setcompatibletextrenderingdefault(v=vs.110).aspx For `EnableVisualStyles` see: http://stackoverflow.com/questions/6764100/does-application-enablevisualstyles-do-anything – Andy Jan 28 '14 at 07:27
  • Thanks for your answer first. I am tried what your method. And it has the same result with the ShowDialog(), i mean the code will just stop here and can't update the form over time. May be my question is not clear , i open a new thread here .http://stackoverflow.com/questions/21410402/update-the-form-after-application-run – Constantine Jan 28 '14 at 15:58