Sign in to follow this  

[.net] help on events - C# noob

This topic is 2833 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi guys, I'm very new to C# and I have a problem i don't understand. I just built an interface with visual studio express, where i simply draw a filled circle on a canvas. However, i would like to move the circle with the mouse. For now, to simplify, i'm just trying to have that circle display a popup when i click on it. I implemented my circle class, and try to add an event. It compiles, but doesn't react at all.....Where did i go wrong ? Here's my Circle class (in fact it is called Module). Module is an abstract class and P3 derives from it (it just render the Shape).
namespace Modules
{
  
    public struct Coord
    {
        public Coord(int _x, int _y) { x = _x; y = _y; }
        public int x;
        public int y;
    }

    abstract class Module : System.Windows.Forms.Form
    {
       public Module() 
       {
//here i register the event
this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.updatePosition);
       }

        public void updatePosition(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            MessageBox.Show("updatePosition");
        }
        // =============================================
        public Brush color;
        public Coord coords;
        abstract public Shape getShape();
    }

    class P3 : Module
    {
        public P3() { base.color = Brushes.PaleGoldenrod; coords = new Coord(); }
        public P3(int x, int y) {
            base.color = Brushes.PaleGoldenrod;
            coords = new Coord(x,y);
        }
        public override Shape getShape()
        {
            //this method gets the shape, and i use it to add it the the canvas with canvas.Add()

            Ellipse el = new Ellipse();
            el.Width = 15;
            el.Height = 15;
            el.Stroke = Brushes.Black;
            el.Fill = color;

            return el;
        }
    }
}




thanks in advance .... I really don't understand, i'm sure it's pretty easy though :/

Share this post


Link to post
Share on other sites
Works fine here: if I click on the form, a message box shows up displaying "updatePosition".

I used your code, added some fake implementation for the missing classes Shape and Ellipse and used the following code to start the app:

using System;
using System.Windows.Forms;

namespace ClickTestApp
{
internal static class Program
{
[STAThread]
private static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new P3());
}
}
}

Share this post


Link to post
Share on other sites
hello,

thank you for your reply! That's really strange. Maybe it's when i instanciate the module then ? Here is what i do : there's a button on my interface which creates the circle, and it has a click event associated to it. Inside this function, i instanciate the circle. Is that what i am supposed to do ?


private void button3_Click(object sender, RoutedEventArgs e)
{

string val = comboBox1.Text;
Module obj = null;
switch (val)
{
case "P3":
obj = new P3();
break;
case "P10":
obj = new P10();
break;

}
if (obj != null)
{
Shape s = obj.getShape();
Coord coords = obj.coords;
Canvas.SetTop (s, coords.y);
Canvas.SetLeft (s, coords.x);

canvas1.Children.Add(s);
}
}



thank you again

Share this post


Link to post
Share on other sites
hello,

i kind of figurated the problem...In fact, i want to draw elements inside a canvas and be able to move them. Should i add an event to the canvas ? I still don't know how i should do this!!

thank you in advance

[Edited by - carmellose on March 11, 2010 7:04:49 AM]

Share this post


Link to post
Share on other sites

This topic is 2833 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this