• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1505
  • Last Modified:

Panel event C#

hi,
 i have a program that at running time i am adding a Panels controls, how do i make a MouseMove event for these panels,
each time MouseMove event occur on any Panel, the same event function will be called.

i am adding in this code two lines

Panel p1 = new Panel();
TablePicture.Controls.Add(p1);


thank you
0
madmare
Asked:
madmare
  • 4
  • 4
  • 2
2 Solutions
 
cyberdevil67Commented:
Hi madmare,

 You will need to write an event for each Panel, or you could create a seperate class that inherits the panel with the code.

 But your code should have something like this


        this.panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MousemoveEvent);

        private void MousemoveEvent(object sender, System.Windows.Forms.MouseEventArgs e)
        {
           Your code here
        }

Cheers!
0
 
madmareAuthor Commented:
i can't write for each panel because all have the same name "p1" and i don't know there number, how do i create a seperate class that inherits the panel with the code, please explain.

thank you
0
 
cyberdevil67Commented:
Its a very complicated process, but for code reuses you would do something like this

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace WindowsApplication1
{
      public class UserControl1 : System.Windows.Forms.Panel
      {
            private System.ComponentModel.Container components = null;

            public UserControl1()
            {
                  InitializeComponent();
            }
                }
}

Its more complicated that that, but anytime you want to share the same features you would look at creating a custom control so that you can place that on the form, thus using the mousevent that I showed you earlier.

I will tkae it that the panels (p1) is on multiple forms otherwise you can have more than one on a form with the same name.

I would recommend going to msdn.microsoft.com and look at doing a search for UserControls, or custom controls.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
smeggheadCommented:
Why can't you just do this ??

Panel p1 = new Panel();
p1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MousemoveEvent);
TablePicture.Controls.Add(p1);
p1=new Panel();
p1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MousemoveEvent);
TablePicture.Controls.Add(p1);


private void MousemoveEvent(object sender, System.Windows.Forms.MouseEventArgs e)
{
   // Deal with MouseMove
}


This would add two panels to your TablePicture control, with both of them calling the same code if the mouse is moved over them. You can determine which control it was dragged over by casting 'sender' back to 'Panel'

Smg.
0
 
madmareAuthor Commented:
i think i hadn't explained my self in the best way.

in my program each time the user press on "button1", what happen:
  Panel p1 = new Panel();
  p1.Top = left_dise_click_Y(e.Y);
  p1.Left = left_dise_click_X(e.X)+1;
  p1.Width = Convert.ToInt16(ReticalY)*DisesX-1;
  p1.Height =Convert.ToInt16(ReticalX)*DisesY-1;
  p1.BackColor = Color.Yellow;
  TablePicture.Controls.Add(p1);

the user press many time , so i don't know the number of my panels

i want to make an Event function that each time the mouse pointer move over one of these panels the same event function will be called, i want the same event function because all the panels in this form are the same name.

thank you
0
 
smeggheadCommented:
as my comment above suggests.. just add the line...

 Panel p1 = new Panel();
     p1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MousemoveEvent);
  p1.Top = left_dise_click_Y(e.Y);
  p1.Left = left_dise_click_X(e.X)+1;
  p1.Width = Convert.ToInt16(ReticalY)*DisesX-1;
  p1.Height =Convert.ToInt16(ReticalX)*DisesY-1;
  p1.BackColor = Color.Yellow;
  TablePicture.Controls.Add(p1);

The event isn't tied to 'p1', it's tied to the object that it's pointing to at the time, so each time you create a new panel, the event is attached to it. (calling the same event code)
0
 
smeggheadCommented:
madmare, did my suggestion help you ? if not, can you explain the problem you are still having ?
0
 
cyberdevil67Commented:
I believe I posted the handler code before smegghead!!
0
 
smeggheadCommented:
Maybe, but my comment illustrated that you can use the same handler for more than one dynamically added control.
0
 
cyberdevil67Commented:
My comment said that as well
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now