Results 1 to 12 of 12

Thread: Event when Controls in a PictureBox get Focus

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Brooklyn NY USA
    Posts
    1,258

    Event when Controls in a PictureBox get Focus

    Is there any event that triggers when any control in a picturebox receives focus.

    I have around 10 Controls in a picturebox, and i want to do something if any of does receive focus (Like setting a command buttom as default depending which group of controls have focus.)

    So is there a easy way or i need to code it in each control?

  2. #2
    Member
    Join Date
    Aug 2005
    Posts
    35

    Re: Event when Controls in a PictureBox get Focus

    Hi,
    I think you do not need to code everysingle ctrl..because you can create an array of ctrls when you copy and paste them (VB creates a new button called cmdbutton(1)..cmdbutton(2) and then you can use their index to recognize them as follows:

    sub myctrl_GotFocus(index as.....)
    dosomething
    end sub

    sub dosomething(indx....)
    msgbox indx
    ...
    ...
    end sub

    I'm leaving..bye
    Cheers

  3. #3
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575

    Re: Event when Controls in a PictureBox get Focus

    What kind of controls are in the container - each control will get its own gotfocus event.
    My secretary hopes that I will pay her, her landlord hopes that she will produce some rent, the Electricity Board hopes that he will settle their bill, and so on. I find it a wonderfully optimistic way of life. [Dirk Gently]

  4. #4
    Fanatic Member BrianHawley's Avatar
    Join Date
    Aug 2001
    Location
    Saudi Arabia
    Posts
    796

    Re: Event when Controls in a PictureBox get Focus

    You can group controls of the same type in control arrays, so there is only a single got focus event for each group.
    Brian
    (Fighting with the RightToLeft bugs in VS 2005)

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Brooklyn NY USA
    Posts
    1,258

    Re: Event when Controls in a PictureBox get Focus

    But its a few type of controls. Combo's text, datepickers etc etc.

    Does anyone know of a easy way?

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Brooklyn NY USA
    Posts
    1,258

    Re: Event when Controls in a PictureBox get Focus

    But its a few type of controls. Combo's text, datepickers etc etc.

    Does anyone know of a easy way?

    The same way the Option Button knows when its in a single picturbox.

  7. #7
    Fanatic Member BrianHawley's Avatar
    Join Date
    Aug 2001
    Location
    Saudi Arabia
    Posts
    796

    Re: Event when Controls in a PictureBox get Focus

    No easy way.

    You could in theory intercept the Windows message queue, but it would probably be less work to check each control's got focus event. How many controls are we talking about here?

    Could you put the picture box on its own form and check the activate event instead?
    Brian
    (Fighting with the RightToLeft bugs in VS 2005)

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Brooklyn NY USA
    Posts
    1,258

    Re: Event when Controls in a PictureBox get Focus

    in this form how do the option boxes know if they are in the same picturebox?

    VERSION 5.00
    Begin VB.Form Form1
    Caption = "Form1"
    ClientHeight = 7635
    ClientLeft = 60
    ClientTop = 450
    ClientWidth = 10110
    LinkTopic = "Form1"
    ScaleHeight = 7635
    ScaleWidth = 10110
    StartUpPosition = 3 'Windows Default
    Begin VB.PictureBox Picture2
    Height = 2970
    Left = 5685
    ScaleHeight = 2910
    ScaleWidth = 3630
    TabIndex = 4
    Top = 1785
    Width = 3690
    Begin VB.OptionButton Option6
    Caption = "Option6"
    Height = 270
    Left = 1185
    TabIndex = 7
    Top = 1965
    Width = 1275
    End
    Begin VB.OptionButton Option5
    Caption = "Option5"
    Height = 420
    Left = 1035
    TabIndex = 6
    Top = 1320
    Width = 1785
    End
    Begin VB.OptionButton Option4
    Caption = "Option4"
    Height = 465
    Left = 975
    TabIndex = 5
    Top = 495
    Width = 1965
    End
    End
    Begin VB.PictureBox Picture1
    Height = 3045
    Left = 1155
    ScaleHeight = 2985
    ScaleWidth = 3210
    TabIndex = 0
    Top = 1815
    Width = 3270
    Begin VB.OptionButton Option3
    Caption = "Option3"
    Height = 480
    Left = 330
    TabIndex = 3
    Top = 2085
    Width = 2070
    End
    Begin VB.OptionButton Option2
    Caption = "Option2"
    Height = 525
    Left = 450
    TabIndex = 2
    Top = 1260
    Width = 1590
    End
    Begin VB.OptionButton Option1
    Caption = "Option1"
    Height = 510
    Left = 540
    TabIndex = 1
    Top = 675
    Width = 1410
    End
    End
    End
    Attribute VB_Name = "Form1"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = False
    Attribute VB_PredeclaredId = True
    Attribute VB_Exposed = False

  9. #9
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Event when Controls in a PictureBox get Focus

    Because the underlying form code shows the option buttons in a heirarchy under their respective pictureboxes. BTW, it would be easier for us if you attached the form rather than the underlying code.

  10. #10
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Event when Controls in a PictureBox get Focus

    If you are willing to use a timer then here is a way to dectect when a control in a particular picturebox gets focus.

    VB Code:
    1. Private Sub Timer1_Timer()
    2.     Static strOld As String
    3.    
    4.     If Screen.ActiveControl.Container.Name <> strOld Then
    5.         Select Case Screen.ActiveControl.Container.Name
    6.             Case "Picture1"
    7.                 MsgBox "A control in Picture1 has focus"
    8.             Case "Picture2"
    9.                 MsgBox "A control in Picture2 has focus"
    10.         End Select
    11.         strOld = Screen.ActiveControl.Container.Name
    12.     End If
    13. End Sub

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Brooklyn NY USA
    Posts
    1,258

    Re: Event when Controls in a PictureBox get Focus

    Thanks MartinLiss Maybe i will use this option.

    Do you the OptionBox internly also uses the same mechanism?

  12. #12
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Event when Controls in a PictureBox get Focus

    With an option box control when any one of them is clicked it enumerates through all others in its container and sets their value to False.

    To answer your question, the easy way is to use a control array and then you can just have one event handler for each control in the array. However seeing as you have different types of controls you wouldn't be able to do that.

    There is probably a way of doing it using an array of classes and an interface. If you are willing to use that sort of coding then I will see if I can knock up an example (that's if it works).

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