CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Jan 2010
    Location
    .NET v3.5 SP1, VS 2008
    Posts
    18

    [RESOLVED] Custom User Control Issue

    Hey all,

    I created a custom user control in windows forms which simply has a background image and a label. When I add the custom control to one of my forms and set the click event, the event only fires when I click on the background image. It doesn't fire when I click on the label. This doesn't seem right since the label is part of the control and is not separate on the form... Is this normal? If so how do I get the event to fire regardless of what part of the control I click?

    Thanks in advance for the help! It's really annoying me that out of everything, this is what i'm having problems with :/
    Last edited by SixSence; March 30th, 2010 at 06:28 PM. Reason: issue is resolved

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Custom User Control Issue

    Yes, it is normal. The label is still on the usercontrol, so it is indeed still standalone. You'll have to write a comon event handler that handles all the clicks from all the controls on your user control.

  3. #3
    Join Date
    Jan 2010
    Location
    .NET v3.5 SP1, VS 2008
    Posts
    18

    Re: Custom User Control Issue

    i was hoping there was an easier way =/ Where do I set the event handler on the main form? It's viewed as one control from the main form, so I can only set the click event for the control as a whole.

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Custom User Control Issue

    That is the wrong way

    You'll have to incorporate all the necessary events into the userControl's code. So that it works 100% already when placed upon the form.

  5. #5
    Join Date
    Jan 2010
    Location
    .NET v3.5 SP1, VS 2008
    Posts
    18

    Re: Custom User Control Issue

    if i do it that way, I'm only going to be able to call a method within the user control right ? the whole point of the control is to notify the main form when it's clicked, so i can manipulate the main user interface, or am i missing something?

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Custom User Control Issue

    Quote Originally Posted by SixSence View Post
    if i do it that way, I'm only going to be able to call a method within the user control right ?
    No. For the 3rd time

    Quote Originally Posted by SixSence View Post
    the whole point of the control is to notify the main form when it's clicked, so i can manipulate the main user interface,
    Yes, I understand that This is why you have to enable this functionality on the usercontrol itself, and all its child controls.

    Quote Originally Posted by SixSence View Post
    or am i missing something?
    Yes

    The whole point of a usercontrol is to include functionality which you "can't" through standard windows forms. By doing it like you are doing, you are defeating the whole object of a usercontrol. All the methods and properties must be included into the usercontrol's code first. You just interact with the usercontrol through the Form, but all its capabilities must be built in. Full stop.

    Please, please believe me

  7. #7
    Join Date
    Jan 2010
    Location
    .NET v3.5 SP1, VS 2008
    Posts
    18

    Re: Custom User Control Issue

    lol i do believe you, not trying to question your knowledge, just trying to understand it i need it beat into me. Maybe I'm going about this wrong. Let me elaborate what I'm doing. I have 14 pool tables on the main form that represent the layout of a pool hall, the pool table is my custom user control. The reason I made it a custom control is so if I need to make a change to the look of the pool table, I don't have to edit every single table, along with the label i would have to add for each one that shows the table number. I could just edit the one control. Also, I added a couple custom properties to add functionality to it. I want it to notify the main form when a table is clicked. Could you let me know the direction I'm supposed to go in? sorry again for being hard headed.

  8. #8
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Custom User Control Issue

    LOL! I know

    Could you provide us with a screenshot of a pool table, if possible ¿

  9. #9
    Join Date
    Jan 2010
    Location
    .NET v3.5 SP1, VS 2008
    Posts
    18

    Re: Custom User Control Issue


  10. #10
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Custom User Control Issue

    Wow! That looks fantastic!

    OK, this is basically What I'd do. In the UserControl do this :
    Code:
    		private void Click_Handler()
    		{
    			MessageBox.Show("table Clicked");
    		}
    
    		private void Label_Clicked(object sender, System.EventArgs e)
    		{
    			Click_Handler();
    		}
    
    		private void UserControl_Clicked(object sender, System.EventArgs e)
    		{
    			Click_Handler();
    		}
    Here, I added a custome method named Click_Handler, and called it from UserControl_Clicked as well as Label_Clicked. Simple.

    Now, I added this user control to a Windows form, and voila! When I click either the table or the label it shows me a message saying that the table was clicked.

    Does this help ¿

  11. #11
    Join Date
    Jan 2010
    Location
    .NET v3.5 SP1, VS 2008
    Posts
    18

    Re: Custom User Control Issue

    I get what you're saying, but say I wanted to change the right side of the panel i have on the main form when a table is clicked. How do I get this click event to work and at the same time be able to talk back to the main form, without passing a reference of the main form to the user control?

  12. #12
    Join Date
    Jan 2010
    Location
    .NET v3.5 SP1, VS 2008
    Posts
    18

    Re: Custom User Control Issue

    And thanks btw I have been trying to learn C# and work on this thing for nights now ughhh :P

  13. #13
    Join Date
    Jan 2010
    Location
    .NET v3.5 SP1, VS 2008
    Posts
    18

    Smile [RESOLVED] Custom User Control Issue

    I got it to work finally sheesh :/ In the main form's constructor i looped through all of the tables and set a delegate in each table to reference the main form's userControl_Click method, and then inside the user control i called the delegate from its label_Click method.

    Thanks for the help! I learned some new ways to look at things

  14. #14
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: [RESOLVED] Custom User Control Issue

    That is good news! Well done! Sorry I didn't reply last night, had to go home

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured