Click here to Skip to main content
15,908,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a class named Resistor. Yes, its about electronics. But I am experimenting, still. Inside it, in a Paint event,I add a image of a rezistor.png and some e.Graphics.FillRectangle colors and text.
In this class, I add "public event EventHandler MouseEnter;"
And from Form1 I can call it fine:
resistorx1.MouseEnter += new EventHandler(resistorx1_MouseEnter);

void resistorx1_MouseEnter(object sender, EventArgs e)
{
resistorx1.text1 = "gugu";
}
But noting is happening after I run with my mouse over it. No text changing!

I suppose some boundaries must be set to be able to "feel" when the mouse is inside ? How can I do that? - an example would help immensely - thank you.

What I have tried:

a lot of work ___________________
Posted
Updated 9-Dec-18 19:59pm
v2

1 solution

You can't just add an event and expect it to be automatically hooked up - if you want to respond to system events then the class needs to be one that knows about system events: a UserControl.

I quickly constructed one, and it works. Add a new UserControl, Call it "Resistor" and set it's CS code to this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GeneralTesting
    {
    public partial class Resistor : UserControl
        {
        public Resistor()
            {
            InitializeComponent();
            Text = "100R";
            }

        private void Resistor_Paint(object sender, PaintEventArgs e)
            {
            e.Graphics.FillRectangle(Brushes.Pink, e.ClipRectangle);
            e.Graphics.DrawString(Text, Font, Brushes.Black, e.ClipRectangle);
            }

        private void Resistor_MouseEnter(object sender, EventArgs e)
            {
            Text = "Hello";
            Invalidate();
            }

        private void Resistor_MouseLeave(object sender, EventArgs e)
            {
            Text = "100R";
            Invalidate();
            }

        }
    }
Drop one on my form in design mode, and run the app. when the mouse enters, it changes text, when the mouse leaves, it goes back.
 
Share this answer
 
Comments
_Q12_ 10-Dec-18 3:07am    
Thank you mister OriginalGriff ! You give the best answer !
But... I want something special this time(if possible). I want to maintain it as a class only! (not transform it into UserControl). Why, you ask me? Different motifs. The essential one is to keep transparency after the control drawing (no background). That's the whole reason why I Paint my items in a class and refer to that paint, outside in my Form1. I understand I must make the whole event mechanism from scratch, but it is what it is, so I will do it this way. My question is How? It's a hard way to do it, but it's the only way my poor soul will be happy with the results.
Thank you !
OriginalGriff 10-Dec-18 3:27am    
You can't. Classes are not "displayable items" in themselves, they do not - and cannot, because of the way Windows works - recieve or respond to Windows actions, including but not limited to mouse operations. Only items which are derived from the Control class (which includes the Form class) can be displayed, and can receive mouse actions.

To do what you want with a "standard class" would mean a huge amount of work in the "containing form": handling the mouse move for the container, working out that it's over an area your class is "in" and translating the messages and raising the events.
It's a huge investment, most of which is duplicating code which is already in .NET! Not to mention which but it's also a massive OOPs failure, because it breaks the independence of your new class in that the form becomes responsible for knowing where it is and drawing it!
And when you have a circuit diagram containing 40 resistors, 60 caps, a dozen IC's, uncounted diodes, and a few inductors your form has to be responsible for all of them.

Do it "the proper way" and they all become responsible for themselves, and the container becomes responsible for the links between them - which is a lot more of an accurate mapping to a real-world PCB and it's components!

Trust me on this: you are setting yourself up for a huge input of effort to get what you could have got with a single line of code ... :laugh:
_Q12_ 10-Dec-18 7:38am    
edited:
I am testing your code now.
i dont think I actually tried this method that you present here... intriguing. Though I run over similar ones many times(in my searches).
_Q12_ 12-Dec-18 22:11pm    
InitializeComponent(); does not work!
Error 1 The name 'InitializeComponent' does not exist in the current context.
OriginalGriff 13-Dec-18 3:38am    
Let me guess ... You added ": UserControl" to the end of your existing class definition?

You don't create a UserControl like that, any more than adding ": Form" to a class will turn it into a working Form. Or if you like, adding a Ferrari logo to a bicycle will make it a race car! Controls have Designer.cs files which construct the control, in the same way the Forms do (and for the same reason, Forms are Controls!).

Add a new UserControl to your project, and copy your code into it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900