Results 1 to 16 of 16

Thread: [RESOLVED] Passing controls to a class

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Location
    Lexington, SC
    Posts
    586

    Resolved [RESOLVED] Passing controls to a class

    I need help with passing a control to a class.

    I'm trying to create a control to handle properties and stuff for an image on a user control (I'm essentially turning the image into the button and storing data in the class).

    Well I want to pass the actual image control to the class so I can set the properties of the class.

    This works fine.. I can do like imgBox.visible from within the class just fine.

    The problem is I cannot handle the events of the image anywhere... If I delcare the imgBox variable withevents in the class then it tells me the object isn't declared when I try to pass the image control to it. If I don't use events in the class the control itself dosn't catch it's own events.

    It's actually a control array on the usercontrol and all the indexes that I don't pass to the class still handle properly, but the ones that I pass to the class control do not fire their MouseMove Down and Up events that I need...

    Any ideas?

    EDIT:
    Oh I did try another way to, I thought about trying to scratch passing the image and just create one from within the class.. but that didn't work either. I couldn't find anyway to create a dynamic image control on the usercontrol from within the class.
    Last edited by StevenHickerson; Sep 14th, 2005 at 01:01 PM.

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Passing controls to a class

    In the class module declare an image variables with the WithEvents keyword.
    VB Code:
    1. Dim WithEvents MyImage As Image
    So you will be able to set all events of MyImage.

    Then somewhere in teh code set it to your desired image control.
    VB Code:
    1. Set MyImage = Form1.Image1

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3
    New Member wspguy's Avatar
    Join Date
    Sep 2005
    Location
    South Carolina
    Posts
    13

    Re: Passing controls to a class

    Are you calling
    VB Code:
    1. DoEvents
    from within the class after you call a method on the Image Control?

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Location
    Lexington, SC
    Posts
    586

    Re: Passing controls to a class

    As I said I tried that.. the name of my control is TitleBar... I've tried in the class setting it with Set imgBox = TitleBar.Image1 I've tried UserControl.xxx but it will not let me access these outside sources from within the class. Tells me user type variable not defined. And if I try passing the image through a sub it tells me Object not declared on my image.
    Last edited by StevenHickerson; Sep 14th, 2005 at 01:29 PM.

  5. #5
    Hyperactive Member
    Join Date
    Jun 2004
    Posts
    468

    Re: Passing controls to a class

    Perhaps you didn't expose the Image control from the UserControl. This set-up seems to work:


    VB Code:
    1. 'Code for Form1...
    2.  
    3. Option Explicit
    4.  
    5. Private c As Class1
    6.  
    7. Private Sub Form_Load()
    8.   Set c = New Class1
    9.   c.Init UserControl11(1).Image
    10. End Sub
    11.  
    12. Private Sub Form_Unload(Cancel As Integer)
    13.   Set c = Nothing
    14. End Sub

    VB Code:
    1. 'Code for UserControl1
    2.  
    3. Option Explicit
    4.  
    5. Public Property Get Image() As Image
    6.   Set Image = UserControl.Image1
    7. End Property


    VB Code:
    1. 'Code for Class1
    2.  
    3. Option Explicit
    4.  
    5. Private WithEvents Image As Image
    6.  
    7. Private Sub Class_Terminate()
    8.   Set Image = Nothing
    9. End Sub
    10.  
    11. Public Sub Init(ByRef ImageCtrl As Image)
    12.   Set Image = ImageCtrl
    13. End Sub
    14.  
    15. Private Sub Image_Click()
    16.   Debug.Print "Click"
    17. End Sub

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Location
    Lexington, SC
    Posts
    586

    Re: Passing controls to a class

    Well bpd..that may work but your not set up the same as me.

    Se I have a usercontrol.. the class is part of the usercontrol not part of the project.

    I'm trying to keep the handling of the image and all out of sight from the form that the usercontrol is placed on. Essentially what I want to be able to do for example is this

    VB Code:
    1. 'User Control
    2.  
    3. Public c As New Class1
    4.  
    5. Private Sub Class_Initialize()
    6.      Load imgArray(1)
    7.      c.setImg = imgArray(1)
    8. End Sub

    VB Code:
    1. 'Class code
    2. Public ImgBox As Image
    3.  
    4. Public Property Let Visible(vSet As Boolean)
    5.     imgBox.Visible = vSet
    6. End Property
    7.  
    8. Friend Property Let setImg(imgSet As Image)
    9.      Set imgBox = imgSet
    10. End Property

    VB Code:
    1. 'Form Code
    2. Private Sub Form_Load()
    3.      UserControl1.C.Visible = True
    4. End Sub

    Now I can get this much to work.. it's just when I try to handle the events.. they do not get fired anywhere.. not even on the UserControl, it's as if I've done something to cause the image to be eventless.

  7. #7
    Hyperactive Member
    Join Date
    Jun 2004
    Posts
    468

    Re: Passing controls to a class

    I think I understand... You want to catch events from a dynamically loaded control array. I don't think you can do that. But you can catch events from dynamically created controls - so long as they're not part of a control array.

    VB Code:
    1. Private Sub UserControl_Initialize()
    2.   'Load imgArray(1)
    3.   Set c.setImg = Controls.Add("VB.Image", "imgArray1")
    4. End Sub

    And to "fake" a control array, you can use something like:

    VB Code:
    1. Private Property Get imgArray(ByVal Index As Integer) As Image
    2.   Set imgArray = Controls("imgArray" & CStr(Index))
    3. End Property

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Location
    Lexington, SC
    Posts
    586

    Re: Passing controls to a class

    I even tried something simliar to that bpd.. and it dosn't work because the class cannot see Controls()... says Sub or Function not defined and highlights the word Controls when I try something like that.

  9. #9
    Hyperactive Member
    Join Date
    Jun 2004
    Posts
    468

    Re: Passing controls to a class

    Quote Originally Posted by StevenHickerson
    I even tried something simliar to that bpd.. and it dosn't work because the class cannot see Controls()... says Sub or Function not defined and highlights the word Controls when I try something like that.
    Are you saying the the control creation happens within the Class and not the UserControl?

    If you want the controls created by the Class, you'll have to either pass a reference of the UserControl into the class (to gain access to UserControl.Controls) or have the Class call a public method of the UserControl to do the actual control creation.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Location
    Lexington, SC
    Posts
    586

    Re: Passing controls to a class

    No the control is created on the usercontrol I've just tried setting it by using the controls() function.

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Location
    Lexington, SC
    Posts
    586

    Re: Passing controls to a class

    Ok I've decided to upload my entire project so maybe someone can look at it and figure out what Im doing wrong or at least figure out what I'm trying to do and suggest a better way to do it that works.
    Attached Files Attached Files

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Location
    Lexington, SC
    Posts
    586

    Re: Passing controls to a class

    So I guess no one can figure this out? See 3 people downloaded the project but no more replies.. I'm just stuck on this project until I can figure this out because I have to have some way of changing the imgbox when the settings are changed and some way of changing the image on mouseevents to do this like I wanted...

  13. #13
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Passing controls to a class

    The problem is I cannot handle the events of the image anywhere...
    In the UserControl_Initialize event, the Label is resized to the size of the entire user control. Since the Label has a higher Z-Order than the Image control it is getting all the mouse events. After loading a new image control make sure its z-order is higher than the label control.

    Add this line at the end of the UserControl_Initialize event.

    VB Code:
    1. capLabel.ZOrder vbSendToBack
    2.  
    3. End Sub

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Location
    Lexington, SC
    Posts
    586

    Re: Passing controls to a class

    Thanks brucevde!

    Now the problem I'mhaving though is since I have the label set to transparent and at the back, I was using it to detect when the mouse wasn't on the button, but it will only register if I pass the mouse over some text on the label if its transparent, and the form does not receive the mouse events even though the label is transparent.

    So now my problem is detecting when the mouse moves off the image ... lol

  15. #15
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Passing controls to a class

    If the mouse is over a "Transparent" area of a control, the Mouse events of the first container control which is not transparent are raised.

    Since your UserControl is also transparent, moving the mouse over the Label where there is no text, raises the Form's MouseMove events.

    If you place a Frame on the Form and the UserControl onto the Frame, the Frame's MouseMove Event will be raised.

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Location
    Lexington, SC
    Posts
    586

    Re: Passing controls to a class

    bleh, forgot I had the control set to transparent lol.. didn't even realize that setting was there. But it's corrected now. Thanks brucevde, now I can continue the project

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