mousehover on toolpalette... almost there but what am i doing wrong?

mousehover on toolpalette... almost there but what am i doing wrong?

stefanveurink68AXD
Advocate Advocate
588 Views
5 Replies
Message 1 of 6

mousehover on toolpalette... almost there but what am i doing wrong?

stefanveurink68AXD
Advocate
Advocate

Dear experts, 

I've been working on some code (mostly copy & paste form multiple sites) and almost got it to work the way i want it, or better said got it in a way that I understand how to add things myself and got it working the way I want, only one thing that I can't get to work (hope it's some dumb mistake cause it took me lots of time to understand the pasted codes enough to get it work for as far as it does now). So in other words please don't tell me I have to set everything up on a total different way 🙏

 

anyhow this is the code i got: 

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Windows;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Reflection;
using WinForms = System.Windows.Forms;

namespace ModelessDialogs
{

    public class Commands
    {
        //zodat die niet crasht als je palletcommand doet terwijl pallet er al is?
        private static PaletteSet _ps = null;
        
        //palet laden
        [CommandMethod("PC")]
        public void PaletteCcommands()
        {
            if (_ps == null)
            {
                var doc = Application.DocumentManager.MdiActiveDocument;
                if (doc == null) return;
                var ed = doc.Editor;

                var bs = new List<WinForms.Button>();
                
                // buttons
                WinForms.Button b = new WinForms.Button();
                b.Click += new EventHandler(b_Click);
                b.Text = "testttest";
                b.SetBounds(50, 50, 50, 50);
                b.MouseHover += b_MouseHover;
                bs.Add(b);

                WinForms.Button c = new WinForms.Button();
                c.Click += new EventHandler(b_Click);
                c.Text = "testttest";
                c.SetBounds(50, 150, 50, 50);
                c.MouseHover += b_MouseHover;
                bs.Add(c);

                // Create a user control 
                var uc = new WinForms.UserControl();
                uc.Controls.AddRange(bs.ToArray());

                //paletteset maken 
                _ps = new PaletteSet("PC", new Guid("87374E16-C0DB-4F3F-9271-7A71ED921566"));
                _ps.Add("CMDPAL", uc);
                _ps.DockEnabled = (DockSides)(DockSides.Left | DockSides.Right);
                _ps.Size = new Size(400, 500);

            }
            _ps.Visible = true;
        }


        private void b_Click(object sender, EventArgs e)
        {
            DisplayMessage("dit was de test hij doet het");
        }

        // Helper function to display a message and post a command prompt
        // (if there's an active document available)

        private static void DisplayMessage(string str, bool postPrompt = true)
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            if (doc == null) return;
            doc.Editor.WriteMessage("\n{0}\n", str);
            if (postPrompt)
                doc.Editor.PostCommandPrompt();
        }

        // mousehovers

      
        private void b_MouseHover(object sender, EventArgs e)
        {
            System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip();
            ToolTip1.SetToolTip(b, "text voor mousehover button");
        }

    }
}

the part that isn't working is the mousehover. in the last line, the b stays 'unknown'. how can i possibly solve this?

 

Next to that I would really appreciate some explanation about why i have to add the buttons to a button list before inserting it to the palette. Because, if I don't do this, the buttons don't show up the way they should (not taking the given size for example but filling up the full pallette, or starting new tabs). 

 

And I would also like to know why the background color of the pallet doesn't automatically adjust to the colors of the autocadenvironment (for example the x-ref manager or properties manager does do that). 

 

Thanks for any thought about any of these questions!

0 Likes
589 Views
5 Replies
Replies (5)
Message 2 of 6

stefanveurink68AXD
Advocate
Advocate

GUYS please help me out i'm pulling al my hairs out. what the * is going wrong here?

 

why doesn't:

        private void b_MouseHover(object sender, EventArgs e)
        {
            System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip();
            ToolTip1.SetToolTip(b, "text voor mousehover button");
        }

recognize the button b from 

        public void PaletteCcommands()
        {
        .
        WinForms.Button b = new WinForms.Button();
        .
        }

??????????????????????

 

is there some wrong in the structure? I've tried changing privates in publics and vice versa but doesn't work. please help me guys. 

0 Likes
Message 3 of 6

fieldguy
Advisor
Advisor

put a breakpoint at >>System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip();<< and check the "sender" object.

 

check >>this link<< for more information.

 

your "b_MouseHover" code section does not know what "b" (in this case "sender") is. 

Message 4 of 6

fieldguy
Advisor
Advisor

You don't need a specific event for the tooltip to display.  the "hover" part is built in to the class.

check >>here<<

Message 5 of 6

norman.yuan
Mentor
Mentor

Even you said "...please don't tell me I have to set everything up on a total different way...", here you go, I still want to say, or hope, that the code you show here is only meant to show the issue you have; you did not actually build the PaletteSet's Win Form UserControl entirely by code in the CommandClass, did you? If this is what you are actually doing, you'd better "set everything up" in totally different way, a much easier way: add an UserControl item to your project and use Visual Studio's designer to set up controls in the UserControl. If you prefer build the UserControl purely with code (in order to better understand how these UI components work), you should separate the UserControl builing code from the CommandClass.

 

Now back to your issue with the code:

        private void b_MouseHover(object sender, EventArgs e)
        {
            System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip();
            ToolTip1.SetToolTip(b, "text voor mousehover button");
        }

where variable "b" is obviously unknown in the scope of this event handler procedure, because "b"  was declared as Button in other procedure (PaletteCommand()) LOCALLY. If you decleard "b" and "c" as Button at class level, you would not have this issue, BUT DOING THAT WOULD BE VERY BAD PRACTICE!

 

The quick fix to you issue at hand, without change the way you do things, is like this:

 

private void b_MouseHover(object sender, EventArgs e)
        {
Control b = (Control)sender; System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip(); ToolTip1.SetToolTip(b, "text voor mousehover button"); }

However, I must pointed out: this line of code:

 

System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip();

 

is very much problematic. It might work, but it is wrong to keep creating a new ToolTip object whenever the mouse hovers on a control, which would result in hundreds/thousands of ToolTip objects being created in memory (and eventually crash AutoCAD, if one keep moving the mouse on top of your buttons. Well, if the computer has a lot memory, it may afford this mouse hovering for hours, but you get the point: it is wrong to do this). 

 

The correct way is to only create one ToolTip per UI component (UserControl, or a Form) with an ComponentModel.IContainer argument passed in its constructor, so that the ToolTip object would be properly disposed with the UI component. I am not sure if you know what I am saying here. But do yourself a favor, use Visual Studio's designer to build the UserControl (i.e. "set everything up on a total different way"!).

 

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 6 of 6

stefanveurink68AXD
Advocate
Advocate

Well at first, thanks both for your help. However I haven't got a working code yet, is says no errors, but when i load it in autocad and type the pc command I get the error below, when clicking continue nothing happens. 
error.PNGerror2.PNG

this goes for both sollutions. so for having a

 

 private void b_MouseHover(object sender, EventArgs e)
{
WinForms.Control b = (WinForms.Button)sender; 
WinForms.ToolTip ToolTip1 = new WinForms.ToolTip();
ToolTip1.SetToolTip(b, "text voor mousehover button");
}

as well as the first link fieldguy gave me as wel las the second link which results into: 

 

 

namespace ModelessDialogs
{
....
                bs.Add(c);

                WinForms.ToolTip tooltip1 = new WinForms.ToolTip();
                tooltip1.ShowAlways = true;

                tooltip1.SetToolTip(b, "dit is button b");

                bs.Add(b);
...
}

About the rest of your answer. 

1) I suspected the code to be awkard. It's my first .net code. And made with the intention to get it working asap the way i want so the rest of my company can access some vba-codes i made on an easy way (there's reasons to not do this by new button menu's but it's a little distracting to start explaining this so trust me to say adding a pallet is the most easy way). Anyhow I'm very thankfull for your opinions on it because this is giving me input to do more research on how to write a code that is 'nicely written'. 

2) Your forlast paragraph is concerning to me cause i don't want it to crash offcourse. I expect the second way might prevent this?

3) In your last paragraph indeed i've got no idea what your saying but i'll research this. I can explain you however why I have chosen to build a regular windows form (you're allowed to laugh): it's because I don't know how to convert a windows userform into a palette. 

4) about usercontrols in the command class don't attach to much significance to the name, i didn't built this code but more likely stripped some codes i found on the internet down to the stuff I needed. When I do know how to make a right 'project structure' I'll certrainly do this, but for now the most important thing for me is get some working palette which gives acces to some commands so my company can use the stuff i made (and don't have people asking at my desk all the time to do it for them). When I've got that working, I can take all the time I want to improve it / set things up again. 

 

Anyways... wonder why I got the error in both cases while visual studio itself builds it with no errors. People who got any idea on this???

 

 

0 Likes