Author Topic: Single instance application and/or form  (Read 1872 times)

0 Members and 1 Guest are viewing this topic.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Single instance application and/or form
« 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.



Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Single instance application and/or form
« Reply #1 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
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Single instance application and/or form
« Reply #2 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. }
Speaking English as a French Frog

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Single instance application and/or form
« Reply #3 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
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

n.yuan

  • Bull Frog
  • Posts: 348
Re: Single instance application and/or form
« Reply #4 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