Adding User conrol into Palette with C#

Adding User conrol into Palette with C#

deusextechnic
Advocate Advocate
3,564 Views
8 Replies
Message 1 of 9

Adding User conrol into Palette with C#

deusextechnic
Advocate
Advocate

Hi, 

 

I am trying to add a user control to a palette. I used the below method sucessfully with VB.net but it is not working with C#.  the mainPalette.add() method is throwing up : Error 5 The best overloaded method match for 'Autodesk.AutoCAD.Windows.PaletteSet.Add(string, System.Uri)' has some invalid arguments 

 

public static PaletteSet mainPalette = null;


[CommandMethod("POCFind")]
public static void pocfind()
{
try
{

if (mainPalette == null)
{

POCForm mainForm = new POCForm();
mainPalette = new PaletteSet("POC ADD");
mainPalette.Add("TEST", mainForm); 

}

mainPalette.Visible = true;

// populateDatabaseFromCSVAsync();

}

 

Thanks in advance. 

0 Likes
Accepted solutions (1)
3,565 Views
8 Replies
Replies (8)
Message 2 of 9

Jeff_M
Consultant
Consultant
Is the POCForm a UserControl?
Jeff_M, also a frequent Swamper
EESignature
0 Likes
Message 3 of 9

deusextechnic
Advocate
Advocate

Yes. Judging by the error message, it seems to be trying to use it as an Uri. I can't see any reason it would do this. Like I said, I simply translated a snip of VB.net that i succesfully used previously to C#.   

0 Likes
Message 4 of 9

_gile
Consultant
Consultant
Accepted solution

Hi,

 

The PaletteSet.Add() method is to be used with UserControl deriving from System.Windows.Forms.UserControl (i.e. Winform UserControl).

 

If he POCform class derives  from System.Windows.Controls.(i.e. WPF UserControl), you have to use PaletteSet.AddVisual() method.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 9

deusextechnic
Advocate
Advocate
That was it. Thanks.
0 Likes
Message 6 of 9

jtibbetts73
Observer
Observer

How can you dynamically remove a user control from the paletteset? I don't see a remove method in the object browser

0 Likes
Message 7 of 9

_gile
Consultant
Consultant

@jtibbetts73 

You can use the PaletteSet.Remove(int index) method.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 8 of 9

jtibbetts73
Observer
Observer

But how do you get the index value of a specific palette if they are reordered whenever one is activated? Is there a way to get the index by name or something? I dont see any methods in the object model.

say you have 4 palettes in the set:

 

var multiplePaletteSet = new PaletteSet();

 

var multiplePaletteSet .Palette1 = multiplePaletteSet .AddVisual("Palette1", wpfForm1);

var multiplePaletteSet .Palette2 = multiplePaletteSet .AddVisual("Palette1", wpfForm2);

var multiplePaletteSet .Palette3 = multiplePaletteSet .AddVisual("Palette1", wpfForm3);

var multiplePaletteSet .Palette4 = multiplePaletteSet .AddVisual("Palette1", wpfForm4);

so now user clicks around between the palette tabs, effectively changing the active palette dynamically, but on a specific user action in one of the palettes, I need to remove a different palette from the paletteset. How do I find/select/identify a palette if it is not the active one?

 

Remove(2);

 

 

0 Likes
Message 9 of 9

norman.yuan
Mentor
Mentor

Firstly, you really should not directly instantiate a PaletteSet object. Instead, always derive your custom one from PaletteSet class. In the derived class, you can track which Palette is currently the active one, and do the Palette adding/removing.

 

To tracking which Palette is the currently active one, you handle PaletteSet.PaletteActivated event.

For example, with that event handler, you would have a class member "currentPalatteName". Then, when you remove Palette, you do:

for (int i=0; i<this.Count; i++)

{

  var name=this[i].Name;

  if (name!=currentPaletteName && name==[PaletteNameToRemove])

  {

    this.RemoveAt(i);

    break;

  }

}

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes