TheSwamp

Code Red => .NET => Topic started by: Keith™ on July 27, 2018, 12:27:54 PM

Title: Single instance application and/or form
Post by: Keith™ on July 27, 2018, 12:27:54 PM
I'm working on an application for AutoCAD and I need to make it a single instance application (modeless) and have forms that display modeless such that if the user enters the command a second time, the existing form is displayed and not create a new one.



Title: Re: Single instance application and/or form
Post by: Keith™ on July 27, 2018, 01:31:40 PM
I've devised a workaround until I find a better solution. its a bit verbose, but it seems to work.

Code - vb.net: [Select]
  1. Public NotInheritable Class MyCommands
  2.  
  3.     Shared MainDlg As MainForm
  4.  
  5.     Private Sub New()
  6.     End Sub
  7.  
  8.     <CommandMethod("MySettings", CommandFlags.Modal)>
  9.     Public Shared Sub MySettings()
  10.         If (MainDlg Is Nothing) Then
  11.             MainDlg = New MainForm
  12.             Application.ShowModelessDialog(MainDlg)
  13.         ElseIf (MainDlg.IsDisposed) Then
  14.             MainDlg = New MainForm
  15.             Application.ShowModelessDialog(MainDlg)
  16.         Else
  17.             MainDlg.BringToFront()
  18.         End If
  19.     End Sub
  20.  
  21. End Class
Title: Re: Single instance application and/or form
Post by: gile on July 28, 2018, 03:11:23 AM
Hi,

If you need a modeless UI, what about using one which inherits from PaletteSet?
Typically, you simply create an instance of the palette if it's null (Nothing) and set its Visible property.

Code - C#: [Select]
  1. static CustomPalette palette;  // CustomPalette type is derived from PaletteSet
  2.  
  3. [CommandMethod("MySettings")]
  4. public void MySettings()
  5. {
  6.     if (palette == null)
  7.         palette = new CustomPalette(}
  8.     palette.Visible = true;
  9. }
Title: Re: Single instance application and/or form
Post by: Keith™ on July 28, 2018, 11:47:51 AM
Yeah, it would be nice to do, but this is much too big to change now .. perhaps a future revision
Title: Re: Single instance application and/or form
Post by: n.yuan on July 28, 2018, 09:58:55 PM
You can easily make the modeless form behave like PaletteSet: once instantiated, it remains in AutoCAD session until AutoCAD stops; only change its Visible property to show/hide it, so you do not need to repopulate the form with data each time it shows.

Simply handle the form's FormClosing event and add this code in the handler:

e.Cancel=True
Me.Visible=False

This way, if user closes the form by the code Me.Close(), or clicks the "x" on the form, the form simply becomes invisible, exactly behaves like the PaletteSet.

One post in my blog may be helpful, a bit:

http://drive-cad-with-code.blogspot.com/2014/02/showing-and-closing-modeless-formdialog.html (http://drive-cad-with-code.blogspot.com/2014/02/showing-and-closing-modeless-formdialog.html)