Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 132,612 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 933 people online right now. Registration is fast and FREE... Join Now!




User Control Events

 
Reply to this topicStart new topic

User Control Events

majorpain1588
post 13 Jul, 2008 - 07:42 AM
Post #1


New D.I.C Head

*
Joined: 13 Jul, 2008
Posts: 4

Hello everyone. This is my first posting here.

I have a user control that consists of a panel, 3 labels and an image box. I dynamically add the user controls to my Form based on the number of records in my database.

The Problem:
I need to add an mouse click event to the user control so that when I click anywhere on the user control it fires. Currently when ever I add events to the control they never fire. The events that fire are those of the child controls in the user control (ie. the panel and lables).

Currently I do have 1 non-dynamically added version of my user control on my Form and it acts the same way.

The child controls fire events inside my user control class. I need to be able to know what user control I just clicked on in my Form class.

I need to have an event where the "object sender" is the panel control so that I can use that to change its location and such.

If this is confusing just say so and I can try to explain it again another way.
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 13 Jul, 2008 - 09:19 AM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,062



Thanked 175 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Well it is hard to say what is going on with your code without actually seeing it. Sounds like you just have your events crossed. When you trigger your click event delegate for the control, you are passing an instance of the class itself, not an instance of the child control as sender right?

It would be better to see what you are doing to tell you where you are going wrong. smile.gif
User is offlineProfile CardPM

Go to the top of the page

majorpain1588
post 13 Jul, 2008 - 09:40 AM
Post #3


New D.I.C Head

*
Joined: 13 Jul, 2008
Posts: 4

CODE

public partial class Form1 : Form
{
        //......(other code)
    private void createPanels()
   {
            for (int i = 0; i < caseNum; i++)
            {
                this.casePanels[i] = new PanelControl(cases[i].manufacturer, cases[i].name, cases[i].price, cases[i].imageName);
                this.tabPage1.Controls.Add(this.casePanels[i]);
                this.casePanels[i].Click += new System.EventHandler(this.panelControl_Click);
                this.casePanels[i].Name = "panelControlA";
                this.casePanels[i].Parent = panel1;
                this.casePanels[i].BringToFront();
                Point p = new Point();
                p.X = panel1.Location.X;
                p.Y = panel1.Location.Y + (100 * i);
                this.casePanels[i].Location = p;                
            }
   }
   private void panelControl_Click(object sender, EventArgs e)
   {
                PanelControl pc = (PanelControl)sender;
                pc.Location = new Point(500, 500);

   }
}

The Relevant code from my Form.
This is the event I want to fire. The panelControl is my User Control that contains a panel, 3 labels, and a pic box. They fire their individual events, but I want the overall control to fire its own event. If I put a regular button on the form it will fire its own events. I want to put my user control on and have it fire its own events.

CODE

public partial class PanelControl : UserControl
    {
        private String imageDir;
        private bool isSelected;
      
        public PanelControl()
        {
            InitializeComponent();
        }
        public PanelControl(string mnf, string nm, double prc, string img)
        {
            InitializeComponent();
            imageDir = Directory.GetCurrentDirectory() + "\\" + "Images" + "\\";
            label1.Text = mnf;
            label2.Text = nm;
            label3.Text = prc.ToString();
            imageBox.ImageLocation = imageDir + "img";        
        }        
        private void panelControl_MouseLeave(object sender, EventArgs e)
        {
            a1Panel1.GradientEndColor= Color.DodgerBlue;
        }

        private void panelControl_MouseEnter(object sender, EventArgs e)
        {
            a1Panel1.GradientEndColor = Color.Black;
        }

This is the code for my User Control. All the child controls that make up my User control fire these events.
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 13 Jul, 2008 - 09:56 AM
Post #4


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,062



Thanked 175 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Do you have actual events (which uses the "event" keyword) and delegates defined yet? where are you defining events for the control? We will need to see those and any functions/child control events which call them.

smile.gif

This post has been edited by Martyr2: 13 Jul, 2008 - 09:56 AM
User is offlineProfile CardPM

Go to the top of the page

majorpain1588
post 13 Jul, 2008 - 10:16 AM
Post #5


New D.I.C Head

*
Joined: 13 Jul, 2008
Posts: 4

Im not exactly sure what you mean by defining events and delegates.

By define do you mean lik this:

this.casePanels[i].Click += new System.EventHandler(this.panelControl_Click);
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 13 Jul, 2008 - 10:26 AM
Post #6


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,062



Thanked 175 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Well this might be part of the problem then. user controls can define their own events using the "event" keyword. You would trigger these new events using a delegate (which has the "delegate" keyword). When a child element is touched, its event fires, but then calls the user control's event. Your labels, panels, buttons click events will call a general event for the entire control.

This site is a nice look at how you can do this. In the example pay attention to how clicking the button in the control calls its event which calls the "SubmitClicked" event. This event is defined using the "event" keyword and returns the type of "SubmitClickedHandler"... our defined delegate. Later down in the code we can then respond to this event.

Custom Controls in Visual C#.NET - Publishing and Subscribing Events

Hope this helps. smile.gif

This post has been edited by Martyr2: 13 Jul, 2008 - 10:27 AM
User is offlineProfile CardPM

Go to the top of the page

majorpain1588
post 13 Jul, 2008 - 11:31 AM
Post #7


New D.I.C Head

*
Joined: 13 Jul, 2008
Posts: 4

Thanks a whole bunch smile.gif

That was exactly what I needed.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 02:38AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month