Author Topic: Trouble with Palettes ...  (Read 10057 times)

0 Members and 1 Guest are viewing this topic.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Trouble with Palettes ...
« on: August 08, 2018, 08:52:50 PM »
I have been considering converting all my app forms into palettes to integrate the app better and make it more user friendly. So to that end, I added the following to my WPF Class project:

Code - C#: [Select]
  1. [CommandMethod("Test")]
  2. public void Test()
  3. {
  4.     PaletteSet ps = new PaletteSet("TestPalette");
  5.     usercontrol tc = new usercontrol();
  6.     ps.Add("Test", tc);
  7.     ps.Visible = true;
  8. }
  9.  
  10. public class usercontrol
  11. {
  12.     public usercontrol()
  13.     {
  14.     }
  15. }

It compiles just fine and loads without issue, however when calling "Test" at the command line, I am getting an error "no parameterless constructor defined for this object"

Obviously I am missing something but I just can't see it.
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: Trouble with Palettes ...
« Reply #1 on: August 08, 2018, 10:03:57 PM »
Well this works .. too bad the examples don't talk about making the calling function static  :tickedoff: :tickedoff:
Code - C#: [Select]
  1. [CommandMethod("Test")]
  2. public static void Test()
  3. {
  4.     PaletteSet ps = new PaletteSet("TestPalette");
  5.     usercontrol tc = new usercontrol();
  6.     ps.Add("Test", tc);
  7.     ps.Visible = true;
  8. }
  9.  
  10. public class usercontrol
  11. {
  12.     public usercontrol()
  13.     {
  14.     }
  15. }
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: Trouble with Palettes ...
« Reply #2 on: August 09, 2018, 01:47:50 AM »
Hi,

You can have a look to this recent message.
Speaking English as a French Frog

n.yuan

  • Bull Frog
  • Posts: 348
Re: Trouble with Palettes ...
« Reply #3 on: August 09, 2018, 09:44:21 AM »
Well this works .. too bad the examples don't talk about making the calling function static  :tickedoff: :tickedoff:
Code - C#: [Select]
  1. [CommandMethod("Test")]
  2. public static void Test()
  3. {
  4.     PaletteSet ps = new PaletteSet("TestPalette");
  5.     usercontrol tc = new usercontrol();
  6.     ps.Add("Test", tc);
  7.     ps.Visible = true;
  8. }
  9.  

CommandMethod CAN BE either static or non-static.

You did not show all code of the class (CommandClass?) where the CommandMethod "Test" is in. However, based on error message showed in your original post, it seems the class has a constructor that requires input parameter. If this is the case, it would be problematic, or potentially bug-prone, to say the least, because in AutoCAD .NET plug-in the CommandClass (or the class that has CommandMethod defined) is not instantiated by your code (so you can choose when to "new" your class; rather it is instantiated by Autocad when the CommandMethod is executed: if the CommandMethod is static, an instance of the class is created in ApplicationConext and if the CommandMethod is non-static, an instance of the class is created per document.

So, having constructor, or having some process logic in the constructor may cause unexpected side-effect. If you need some data at class level, initialize/manipulate them either in IExtensionApplication.Initialize() (if they need to be initialized on loading); or do it in CommandMethod.

« Last Edit: August 09, 2018, 09:47:53 AM by n.yuan »

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Trouble with Palettes ...
« Reply #4 on: August 09, 2018, 11:39:29 AM »
Hi,

You can have a look to this recent message.

I've already managed to port the form to a palette by just copy/paste everything in the user form to my user control (along with all associated code). I did have about 100 items that I had to fix but it is working.

CommandMethod CAN BE either static or non-static.

That's what I thought ..

You did not show all code of the class (CommandClass?) where the CommandMethod "Test" is in. However, based on error message showed in your original post, it seems the class has a constructor that requires input parameter.

I didn't show the rest of the class because the only thing in that class are commands. This is the basic class:

Code - C#: [Select]
  1. namespace TTA
  2. {
  3.     public class Commands
  4.     {
  5.         private Commands()
  6.         {
  7.         }
  8.     }
  9. }

Now that I look at it, perhaps it was the private constructor ... but then again, that's what the wizard built ...
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

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Trouble with Palettes ...
« Reply #5 on: August 09, 2018, 01:01:41 PM »
Make sure your giving your palettes a guid so user's preferences carry from session to session.  I also wrap my WPF palettes in an ElementHost.  That helps with the sizing.

Code - C#: [Select]
  1. var paletteSet = new PaletteSet("My Palette", new Guid("{5624342D-F672-4B0D-957E-D61127B25F76}"))
  2.                 {
  3.                     Style = PaletteSetStyles.ShowAutoHideButton | PaletteSetStyles.ShowCloseButton |
  4.                             PaletteSetStyles.Snappable,
  5.                     MinimumSize = new Size(200, 400),
  6.                     KeepFocus = false
  7.                 };
  8. var host = new ElementHost{ AutoSize = true, Dock = DockStyle.Fill };
  9. paletteSet.Add("My Palette", host);
  10. var palette = new MyPalette();
  11. host.Child = palette;
Revit 2019, AMEP 2019 64bit Win 10

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Trouble with Palettes ...
« Reply #6 on: August 09, 2018, 02:55:35 PM »
I've just ran into an issue that I've not seen previously.

My palette has a button that prompts for the user to select a point on the screen to insert a block. Because the palette is somewhat wide, I have it roll up when the button is clicked.
Anyway, it works fine except if the palette is docked, of course it will not roll up .. so .. using some code I stole from Autodesk University, I patched the event to undock the palette and roll it up.

However ....

Now my code seemingly skips over several lines of code and an error is generated.

The offending code:
Code - C#: [Select]
  1. if (ps.Dock.Equals(DockSides.None))
  2. {
  3.     if (ps.Style.Equals(32))
  4.     {
  5.         ps.Style = 0;
  6.     }
  7.     ps.AutoRollUp = true;
  8.     ps.Visible = false;
  9.     ps.Visible = true;
  10. }
  11. else
  12. {
  13.     ps.Dock = DockSides.None;
  14.     ps.AutoRollUp = true;
  15.     ps.Visible = false;
  16.     ps.Visible = true;
  17. }
  18.  
  19. // ...... <snip>
  20. // the values passed to the BlockJig are validated prior to this call
  21.  
  22. if (caller.Name == "btnInsert")
  23. {
  24.     BlockReference br = insert.BlockJig(bName, attData, rotAngle, rotBehavior, bScale, lay);
  25.     //Set rotation angle for all future insertions after the first one if behavior is set to 0
  26.     if (options.RotBehavior == 0 && x == 0)
  27.     {
  28.         rotAngle = br.Rotation;
  29.     }
  30.     //capture the last rotation angle in case we need to store it
  31.     options.Rotation = br.Rotation; //<-------- Program seemingly jumps to here and br is null if the palette is docked.
  32. }


There is also a timer that fires once and then turns itself off. It does the same thing as above simply setting AutoRollUp to true and cycling the visibility of the palette to effect the changes.
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: Trouble with Palettes ...
« Reply #7 on: August 09, 2018, 04:21:41 PM »
I've added the GUID to the mix and now when the palette comes back up, the tabs are missing until it is minimized and opened back up.
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: Trouble with Palettes ...
« Reply #8 on: August 09, 2018, 06:31:03 PM »
So long as we are discussing palettes … there are some program settings that are store in the registry. I've built a class that retrieves and stores them and it has worked well for many years. Moving to palettes causes a little bit of issue that I'm not sure how to address .. namely, writing updated values back to the registry.


I had been handling it when the user clicked the OK or Apply button in the application and reloading the values when the user accessed the form again (the forms were disposed each time they closed).


Basically the flow was:

    create a new form class
    read the settings during the load event
    do stuff
    save the settings when the user clicks Apply or OK

A palette has a load event, so I can read the settings then, do what I need to do and then what? Needlessly save the settings each time the user changes a value on the palette? Seems a bit redundant and or inefficient.
   
   
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

Atook

  • Swamp Rat
  • Posts: 1027
  • AKA Tim
Re: Trouble with Palettes ...
« Reply #9 on: August 09, 2018, 09:27:20 PM »
For controls that aren't tied directly to a command, I save the values to the drawing/registry OnChange.

I don't have many though, just  checkboxes, listboxes, or comboboxes. I haven't noticed the overhead yet. All actions from the user that interact with the drawing are tied to commands, so I can save then if need be.

It sounds like your OK/Apply button would be tied to a command (or something with a database transaction) that would be a good time to save settings.
« Last Edit: August 09, 2018, 09:30:31 PM by Atook »

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Trouble with Palettes ...
« Reply #10 on: August 09, 2018, 09:33:27 PM »
Yeah, but I've removed the buttons so it feels more like a native tool.
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: Trouble with Palettes ...
« Reply #11 on: August 09, 2018, 11:26:32 PM »
I'm getting a little bit tired of having to figure out how to fix things that basically shouldn't be a problem.

I've discovered what I can only describe as a bug in palette controls behavior.

My palette currently has two controls, one with various textboxes and combo boxes and one with various checkboxes, a combo box and a numeric up/down.


Imagine a palette with two user controls, each one has a combo box on it.

Select control 1
focus the combo box
Select control 2
use the scroll button
combo box on control 1 changes
select control 1
use the scroll button
nothing happens
select control 2
use the scroll button
combo box on control 1 changes

It seems as though the control is maintaining focus even though it is not visible.

The same thing happens if I type. The text goes to the control on the hidden control.

Its infuriating!
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: Trouble with Palettes ...
« Reply #12 on: August 10, 2018, 02:23:16 PM »
I have had to resort to subclassing PaletteSet so I can expose functions that I need to make them behave as they should.

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

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Trouble with Palettes ...
« Reply #13 on: August 14, 2018, 08:59:44 AM »
I have had to resort to subclassing PaletteSet so I can expose functions that I need to make them behave as they should.

What functionality would you need beside Show/Hide? Everything else should be handled by the palette.  I use MVVM so functionality is in the ViewModel.
Revit 2019, AMEP 2019 64bit Win 10

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Trouble with Palettes ...
« Reply #14 on: August 14, 2018, 01:31:58 PM »
What functionality would you need beside Show/Hide? Everything else should be handled by the palette.  I use MVVM so functionality is in the ViewModel.

There is no good way to access to the underlying user control exposed functions and properties. For my purposes, this is very important.
I suppose I could hack together dozens of different global objects that represent each user control but I'd still have to handle the paletteset events to figure out which ones I need to work on at any given time.

In a paletteset you can activate a palette only by index ... if you don't know what that is (you can't really know that if your paletteset is created dynamically) then the best option is to activate the palette by name. I can do that now.

In a paletteset you cannot get the active palette. I can do that now.

Oh, and the fact that the palettes really like to maintain focus, despite losing focus on the palette itself, the controls never seem to lose focus. Microsoft says that's because the palette itself is in a different focus set than the paletteset, so technically, if you have 5 palettes in a paletteset, you could have 5 focused controls, and amazingly all or none of them may process user input at any given time. That is what initially prompted me to subclass .. my class forces the control with focus to lose focus when it is deactivated.

I've actually looked at a number of OOB software programs that utilize palettes and what I've found is they all subclass them to add functionality.

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