?
Solved

Panel event C#

Posted on 2005-05-04
14
Medium Priority
?
1,474 Views
Last Modified: 2012-06-22
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
Comment
Question by:madmare
  • 4
  • 4
  • 2
10 Comments
 
LVL 9

Expert Comment

by:cyberdevil67
ID: 13926713
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
 

Author Comment

by:madmare
ID: 13926773
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
 
LVL 9

Accepted Solution

by:
cyberdevil67 earned 1000 total points
ID: 13926849
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 10

Expert Comment

by:smegghead
ID: 13927104
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
 

Author Comment

by:madmare
ID: 13927105
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
 
LVL 10

Assisted Solution

by:smegghead
smegghead earned 1000 total points
ID: 13927140
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
 
LVL 10

Expert Comment

by:smegghead
ID: 14101094
madmare, did my suggestion help you ? if not, can you explain the problem you are still having ?
0
 
LVL 9

Expert Comment

by:cyberdevil67
ID: 14207733
I believe I posted the handler code before smegghead!!
0
 
LVL 10

Expert Comment

by:smegghead
ID: 14211498
Maybe, but my comment illustrated that you can use the same handler for more than one dynamically added control.
0
 
LVL 9

Expert Comment

by:cyberdevil67
ID: 14211769
My comment said that as well
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Loops Section Overview
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question