Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8074153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:33:46+00:00 2026-06-05T14:33:46+00:00

I’ve been trying to get my head around creating a custom event to handle

  • 0

I’ve been trying to get my head around creating a custom event to handle mouse controls and such, but i keep running into certain errors that none of the tutorials seem to address. I’m pretty sure my logic of creation / assigning is correct, but maybe theres something fundamentally wrong that I’m missing here.

First off i create my delegate with the same signature as the method;

public delegate void MouseDown(object sender, MouseEventArgs e, Control control);

And then i assign it my event;

public event MouseDown OnMouseDown;

and then finally i try and subscribe to the event;

public static void Init(Control control, Control container, Direction direction)
{
    control.MouseDown += OnMouseDown;

}
//ignore the parameters I'm passing, these are out of the scope of my problem.

However on this section I’m getting the error “an ohject reference is required for the non-static field, method, or propery “blah blah.OnMouseDown””

Finally heres my method I’m trying to subscribe to on mouseDown;

public void MouseDownEvent(object sender, MouseEventArgs e, Control control)
{
    Dragging = true;
    DragStart = new Point(e.X, e.Y);
    control.Capture = true;
}

It probably doesn’t help that I’m trying to modify a helper class i found somewhere. If any further information is needed feel free to ask.

Note: The prime objective of this class is to allow controls to be moved at runtime.

Edit:

I believe the first two have worked, but to move i need to use the following method;

public void MouseMoveEvent(object sender, MouseEventArgs e, Control control, Control container, Direction direction)
{
     if (Dragging)
    {
        if (direction != Direction.Vertical)
            container.Left = Math.Max(0, e.X + container.Left - DragStart.X);
        if (direction != Direction.Horizontal)
            container.Top = Math.Max(0, e.Y + container.Top - DragStart.Y);
    }
}

so i need to send Direction direction, which i can’t send as sender. The reason I’m making these changes to the whole system is, i had it working before using anonymous delegates, but these proved tough to unsubscribe from when i wanted to re-lock a control in place.

Edit 2:
scratch that, the mouseDown and mouseUp won’t work if i don’t pass the correct control, at least subscribing the way i was doing it before. i could try your method but, the way i was doing it i was just calling one method which subscribed to all 3 MouseControls. it looks like either i can subscribe in sepearate methods as suggest, or i need to pass the right control correctly, i.e. not sender as control. any ideas?

Currently I’m subscribing by running this method from anywhere;

helper.Init(this.Controls["btn" + i]);

and then it runs through these methods before subscribing the button to my mouseup, down and move.

public static void Init(Control control)
{
    Init(control, Direction.Any);
}

public static void Init(Control control, Direction direction)
{
    Init(control, control, direction);
}

public static void Init(Control control, Control container, Direction direction)
{
    control.MouseDown += new MouseEventHandler(FireOnMouseDown);
    control.MouseUp += new MouseEventHandler(FireOnMouseUp);
    control.MouseMove += delegate(object sender, MouseEventArgs e)
    {
        if (Dragging)
        {
            if (direction != Direction.Vertical)
                container.Left = Math.Max(0, e.X + container.Left - DragStart.X);
            if (direction != Direction.Horizontal)
                container.Top = Math.Max(0, e.Y + container.Top - DragStart.Y);
        }
    };
}

Note: the third subscription is how they were before (anon delegates). I believe i need to pass the correct control in the events though. Does this give more clarity to my problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:33:47+00:00Added an answer on June 5, 2026 at 2:33 pm

    Answer to this question:

    However on this section i’m getting the error “an ohject reference is
    required for the non-static field, method, or propery “blah
    blah.OnMouseDown””

    Init method is static which means that any non local variable used inside it must be static. In your code, the public event MouseDown OnMouseDown; must be static.

    Just do this and it will work fine (without the need for a delegate):

    EDIT
    Please see code below to see how to get the control that has been clicked.

    public static void Init(Control control, Control container, Direction direction)
    {
       control.MouseDown += new System.Windows.Forms.MouseEventHandler(On_MouseDown);;
    }
    private static void On_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)       
    {     
       Control control = sender as Control;
       if(control != null){
           // Here we go, use the control to do whatever you want with it 
       } 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.