Page 1 of 1

Issues getting OpenFileDialog to work

PostPosted: Sun Jan 24, 2010 12:07 pm
by torial
When I compile and run the following code, and select the "..." button, it gives a message box (my test to confirm the event fired) but then hangs when trying to show the dialog.

Can you pass on any pointers as to what I'm doing wrong or missing? The original goal was to try and learn Cobra by implementing http://cobra-language.com/trac/cobra/wiki/PortingC ... but I didn't get far :-/

Thanks,

Sean
Code: Select all

use System.Windows.Forms from "System.Windows.Forms"

class CSharp2Cobra inherits Form
   var _txtFile as TextBox
   var _lblFile as Label
   var _btnBrowse as Button
   var _btnConvert as Button
   var _panel as FlowLayoutPanel

   cue init
       base.init
       .text = 'C# to Cobra Converter'
       _panel = FlowLayoutPanel(parent=this, dock=DockStyle.Fill, flowDirection=FlowDirection.TopDown)
       _lblFile = Label(parent=_panel, autoSize=true,text='File Name:')
       _txtFile = TextBox(parent=_panel, width=300, text='')
       _btnBrowse = Button(parent=_panel, autoSize=true, text='...', tag=1)
       listen _btnBrowse.click, ref .browseFile
       _btnConvert = Button(parent=_panel, autoSize=true, text='Two', tag=2)
       listen _btnConvert.click, ref .handleClick

   def browseFile(sender, args as EventArgs)
       MessageBox.show(this, 'You clicked [sender.text]/[sender.tag]', 'Click')
       dlg = OpenFileDialog()
       #dlg.filter = "C# files|*.cs"
       #dlg.filterIndex = 1
       result = dlg.showDialog
       #if dlg.showDialog == DialogResult.OK
       #    _txtFile.text = dlg.fileName


   def handleClick(sender, args as EventArgs)
       MessageBox.show(this, 'You clicked [sender.text]/[sender.tag]', 'Click')

class Program

   def main is shared
       Application.run(CSharp2Cobra())

Re: Issues getting OpenFileDialog to work

PostPosted: Mon Jan 25, 2010 7:49 pm
by Charles
It works on my system. I click OK on the message box and then the open file dialog appears which I cancel out of.

I'm using the latest Cobra from source.

My system is Windows XP Pro SP 3.

What version of Cobra are you using? Does the C# code work? What if you compile with -turbo ?

Re: Issues getting OpenFileDialog to work

PostPosted: Mon Jan 25, 2010 8:47 pm
by torial
Chuck wrote:It works on my system. I click OK on the message box and then the open file dialog appears which I cancel out of.

I'm using the latest Cobra from source.

My system is Windows XP Pro SP 3.

What version of Cobra are you using? Does the C# code work? What if you compile with -turbo ?


Thanks for checking it out. I will look into a C# variant to see if it works.

I used the latest source repository (the unzipped folder was Cobra-Informal-Release-2009-06-09), and ran the install-from-workspace.bat.

Possible variants in my environment: I've got VS 2008 installed, .Net 3.5 SP1, and I'm not sure what version of .Net 2.0 I have. The OS is Win2008 Server.

Oh -- I just thought of something to try as well -- permissions! I will look into that and report back. It may be a few days though.

Re: Issues getting OpenFileDialog to work

PostPosted: Mon Jan 25, 2010 8:48 pm
by torial
torial wrote:
Chuck wrote:Oh -- I just thought of something to try as well -- permissions! I will look into that and report back. It may be a few days though.


Permissions were not the issue :-/ I ran the program as administrator and did it still hung. I'll try the C# implementation in the next few days.

Re: Issues getting OpenFileDialog to work

PostPosted: Mon Jan 25, 2010 11:48 pm
by Charles
Cobra-Informal-Release-2009-06-09 is the last informal release. The latest source is retrieved with subversion. See:

http://cobra-language.com/trac/cobra/wiki/HowToInstallFromSource

I also have VS 2008 and .NET 3.5 installed.

It will be very informative if a C# version works and if it works if you mark it as targeting .NET 2.0 vs 3.5.

Re: Issues getting OpenFileDialog to work

PostPosted: Tue Jan 26, 2010 11:14 am
by torial
Chuck wrote:Cobra-Informal-Release-2009-06-09 is the last informal release. The latest source is retrieved with subversion. See:

http://cobra-language.com/trac/cobra/wiki/HowToInstallFromSource

I also have VS 2008 and .NET 3.5 installed.

It will be very informative if a C# version works and if it works if you mark it as targeting .NET 2.0 vs 3.5.


I haven't tried it with the latest from the repository -- but I did run into an issue (regardless of 2.0/3.5) with C#. I will have to look into this further (didn't try the turbo, since C# replicates it).


ERROR reported in Visual Studio: Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process.

(NOTE, I get this irrespective of running in Debug or Release w/ VS). I will look at what I need, perhaps the STA attribute?

Re: Issues getting OpenFileDialog to work

PostPosted: Tue Jan 26, 2010 12:55 pm
by torial
What is the syntax for applying an attribute to a function? Is it VB inspired, C#, of Python?

Re: Issues getting OpenFileDialog to work

PostPosted: Tue Jan 26, 2010 2:48 pm
by Charles
class Foo
def main is shared
has STAThread
Program().run

"... has ATTR"

See http://cobra-language.com/how-to/WPF/ for an example.

Re: Issues getting OpenFileDialog to work

PostPosted: Tue Jan 26, 2010 3:13 pm
by torial
Chuck wrote:
class Foo
def main is shared
has STAThread
Program().run

"... has ATTR"

See http://cobra-language.com/how-to/WPF/ for an example.


That attribute fixed it, thanks!

Re: Issues getting OpenFileDialog to work

PostPosted: Tue Jan 26, 2010 5:58 pm
by Charles
Excellent.