Results 1 to 14 of 14

Thread: Custom Contol Problem

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Posts
    16

    Custom Contol Problem

    I am working on a custom control and i have it working for the most part, but what i want to do now is, on the form that i put my custom control, i want to fire an event when my custom control gets focus. so what i did was just added an event

    Code:
    Private Sub CustCont1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles CustCont1.GotFocus
    the problem is, is that when i put a breakpoint on this line i never hit the breakpoint. is there something special i need to code in my customcontrol to fire the gotfocus and lostfocus events?

    BTW this is in the Compact Framework 3.5

    thanks
    doug
    Last edited by dougg; May 7th, 2009 at 02:05 PM.

  2. #2
    Junior Member
    Join Date
    May 2009
    Posts
    21

    Re: Custom Contol Problem

    You need to override the events from the base class.

    vb Code:
    1. Protected Overrides Sub OnGotFocus(ByVal e As System.EventArgs)
    2.     MyBase.OnGotFocus(e)
    3.     'insert code here
    4. End Sub

    *edit*

    I just realized I misinterpreted what you wrote. Although you could try overriding it from within the control to see if it gets fired. What type of class are you inheriting the custom control from?
    Last edited by insomnicoder; May 7th, 2009 at 02:04 PM. Reason: misread

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Posts
    16

    Re: Custom Contol Problem

    Adding that code to my custom control did not make any difference.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Posts
    16

    Re: Custom Contol Problem

    i am not inheriting from any from anything.

    all my custom control is is a panel on the custom control. want a control that i can draw on.

  5. #5
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: Custom Contol Problem

    Whenever a control on your custom control gets focus, you need that control's GotFocus event to fire your custom controls GotFocus Event.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Posts
    16

    Re: Custom Contol Problem

    Quote Originally Posted by Campion View Post
    Whenever a control on your custom control gets focus, you need that control's GotFocus event to fire your custom controls GotFocus Event.
    the only control on my custom control is a panel and that never gets a gotfocus event fired.

  7. #7
    Junior Member
    Join Date
    May 2009
    Posts
    21

    Re: Custom Contol Problem

    You're right. I just tried the panel and even though it has the got focus event it does not fire. It's not typically a control that receives input directly though.

    If you create a class that just inherits from the UserControl it seems to work and you can draw on it. You could even do your drawing from within the FocusPanel class if you wanted.

    vb Code:
    1. Public Class FocusPanel
    2.     Inherits UserControl
    3. End Class

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Posts
    16

    Re: Custom Contol Problem

    insomicoder, i don't quite follow. i added the focus panel class and added that to my form but i still dont get the events when i click in the control to start drawing. if i have a textbox on the form as well as the custom control, and i put the focus to the textbox and get the cursor for typing, even when i draw on my custom control, the cursor stays in the text box.
    if i add a button control to my custom control i can get the got focus event to fire, but i dont want any other controls on the customcontrol besides the panel.

  9. #9
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Custom Contol Problem

    Please read the MSDN on occasions like this. It says the following:
    Quote Originally Posted by MSDN
    Note The GotFocus and LostFocus events are low-level focus events that are tied to the WM_KILLFOCUS and WM_SETFOCUS Windows messages. Typically, the GotFocus and LostFocus events are only used when updating UICues or when writing custom controls. Instead the Enter and Leave events should be used for all controls except the Form class, which uses the Activated and Deactivate events.
    http://msdn.microsoft.com/en-us/libr....gotfocus.aspx


    So try using the Enter event, see if that works.

  10. #10
    Junior Member
    Join Date
    May 2009
    Posts
    21

    Re: Custom Contol Problem

    The focus event of the user control only seems to fire when you hit tab. But you can set it's focus when it is clicked.

    Code:
    Private Sub FocusPanel1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles FocusPanel1.Click
        If Not FocusPanel1.Focused Then FocusPanel1.Focus()
    End Sub

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Posts
    16

    Re: Custom Contol Problem

    NickThissen,
    the compact framework only has gotfocus and lost focus, and it has none of the rest.

  12. #12
    Junior Member
    Join Date
    May 2009
    Posts
    21

    Re: Custom Contol Problem

    Quote Originally Posted by insomnicoder View Post
    The focus event of the user control only seems to fire when you hit tab. But you can set it's focus when it is clicked.

    Code:
    Private Sub FocusPanel1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles FocusPanel1.Click
        If Not FocusPanel1.Focused Then FocusPanel1.Focus()
    End Sub
    I think that will also work for just the regular panel too.

  13. #13
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Custom Contol Problem

    Quote Originally Posted by dougg View Post
    NickThissen,
    the compact framework only has gotfocus and lost focus, and it has none of the rest.
    Sorry, I did not see you were referring to the compact framework. Maybe you should have made that more evident in your first post instead of a small 'btw' if it's as important as now

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Posts
    16

    Re: Custom Contol Problem

    i think i may have somehting working here. i am going to roll it out of my test project and into my live app and test. if i have anything else i will resume this thread.
    thanks again

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