Passing a control array to a method


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Passing a control array to a method

  1. #1
    Willy Van den Driessche Guest

    Passing a control array to a method


    Is it possible to pass a control array as a paremeter to a method. In particular,
    is it possible to handle the events from a control array inside a (controller)
    class ?

    Willy.

  2. #2
    Chris Lucas Guest

    Re: Passing a control array to a method


    Willy,

    I believe you can get where you want to go by allowing your control class
    to create its own controls from a control array. Since the class owns them,
    it should receive the control array's event messages. Make sure to declare
    the control array WithEvents. Someone told me this is called 'vapor code'.
    Dunno about that, but it should work, unless I've misinterpreted what you
    were asking.

    "Willy Van den Driessche" <Willy.van.den.driessche@skynet.be> wrote:
    >
    >Is it possible to pass a control array as a paremeter to a method. In particular,
    >is it possible to handle the events from a control array inside a (controller)
    >class ?
    >
    >Willy.



  3. #3
    Willy Van den Driessche Guest

    Re: Passing a control array to a method

    Chris,

    thank for the reply.

    > I believe you can get where you want to go by allowing your control

    class
    > to create its own controls from a control array. Since the class owns

    them,
    > it should receive the control array's event messages. Make sure to

    declare
    > the control array WithEvents. Someone told me this is called 'vapor

    code'.
    > Dunno about that, but it should work, unless I've misinterpreted what you
    > were asking.


    I don't see how that works. For instance, how do your declare a class
    member variable to be "a control array of textboxes". And how do you
    declare that withevents ?

    --
    Van den Driessche Willy
    For a work in progress :
    http://users.skynet.be/wvdd2/index.html



  4. #4
    Chris Lucas Guest

    Re: Passing a control array to a method


    Willy,

    What follows is not mine but Colin McGuigan's. He fielded a similar
    question for me sometime ago.

    Colin wrote...

    It's more difficult than you might think. I believe Matthew Curland's book
    goes into this in some detail, but the basic gist is you need to have a
    Class Module that declares that control type WithEvents, and then, when it
    receives those events, passes it on to your global handler. Something like
    this (vapor code):

    'Class CTextBox
    Option Explicit

    Private WithEvents txt As TextBox

    Public Property Get TextBoxControl() As TextBox
    Set TextBoxControl = txt
    End Property

    Public Property Set TextBoxControl(newTextBoxControl As TextBox)
    Set txt = newTextBoxControl
    End Property

    Private Sub txt_Change()
    FMain.TextBoxChange txt
    End Sub

    'Form FMain
    Private m_lngCount As Long
    Private m_colControls As Collection

    Private Sub Form_Load()
    Set m_colControls = New Collection
    End Sub

    Private Sub cmdAdd_Click()
    Dim clsTextBox As CTextBox
    Dim ctl As Control

    m_lngCount = m_lngCount + 1
    Set ctl = Controls.Add("VB.TextBox", "txt" & m_lngCount)
    ctl.Visible = True

    Set clsTextBox = New CTextBox
    Set clsTextBox.TextBoxControl = ctl
    m_colControls.Add clsTextBox
    End Sub

    Public Sub TextBoxChange(ctlText As TextBox)
    MsgBox ctlText.Name & " fired the change event."
    End Sub

    The thing to watch out for, though, is that since you're keeping references
    to the controls on the form, the form won't unload until you get rid of
    those references. You can do that by 'Set m_colControls = Nothing' before
    you unload the form (or in the QueryUnload event)

    --Colin McGuigan

    I hope this helps you, I know it did help me. I know this isn't exactly
    what you wanted but hopefully it will get you close enough.

    Chris


    "Willy Van den Driessche" <Willy.Van.denDriessche@skynet.be> wrote:
    >Chris,
    >
    >thank for the reply.
    >
    >> I believe you can get where you want to go by allowing your control

    >class
    >> to create its own controls from a control array. Since the class owns

    >them,
    >> it should receive the control array's event messages. Make sure to

    >declare
    >> the control array WithEvents. Someone told me this is called 'vapor

    >code'.
    >> Dunno about that, but it should work, unless I've misinterpreted what

    you
    >> were asking.

    >
    >I don't see how that works. For instance, how do your declare a class
    >member variable to be "a control array of textboxes". And how do you
    >declare that withevents ?
    >
    >--
    >Van den Driessche Willy
    >For a work in progress :
    >http://users.skynet.be/wvdd2/index.html



  5. #5
    Willy Van den Driessche Guest

    Re: Passing a control array to a method

    Chris, thanks again for the reply.
    what you show is essentially a wrapper. There is one class instance per
    control instance. Each class instance deals with the events of each own
    observed control. I was aware of that technique.
    I will do my best to explain my problem again. I have a form with a fixed
    control array on it. So the dynamic creation of controls is not my main
    problem. My real problem is this. This is a huge form containing all kinds
    of data about an international (swift payment). Now since the form is
    already complex enough by itself, I've decide to take the 'logical parts'
    out of it. This nearly reduces the code in the form to initialization and
    termination code. There are momentarily 12 classes dealing with the
    specific parts on the screen. Some of these control combinations of
    comboboxes and textboxes, others control listboxes, etc..
    Now I wanted to have such a class to control several (fixed) control arrays.
    Hence my question.
    I could create a usercontrol for this doesn't work for the simple reason
    that the layout is not fixed. The control logic is, but the layout depends
    on the position of the moon, the current heat of the sun and other relevant
    marketing parameters. And it's different for the screens where the controls
    occur. (which I think of as ABT (a bad thing) but I guess marketing logic is
    way beyond me). I currently solved this my passing all controls to my
    controller class but I was just curious if it could be done in VB by just
    passing the control array at once.

    Many thanks
    --
    Van den Driessche Willy
    For a work in progress :
    http://users.skynet.be/wvdd2/index.html
    "Chris Lucas" <cdl1051@earthlink.net> wrote in message
    news:3baeb4e5$1@news.devx.com...
    >
    > Willy,
    >
    > What follows is not mine but Colin McGuigan's. He fielded a similar
    > question for me sometime ago.
    >
    > Colin wrote...
    >
    > It's more difficult than you might think. I believe Matthew Curland's

    book
    > goes into this in some detail, but the basic gist is you need to have a
    > Class Module that declares that control type WithEvents, and then, when it
    > receives those events, passes it on to your global handler. Something

    like
    > this (vapor code):
    >
    > 'Class CTextBox
    > Option Explicit
    >
    > Private WithEvents txt As TextBox
    >
    > Public Property Get TextBoxControl() As TextBox
    > Set TextBoxControl = txt
    > End Property
    >
    > Public Property Set TextBoxControl(newTextBoxControl As TextBox)
    > Set txt = newTextBoxControl
    > End Property
    >
    > Private Sub txt_Change()
    > FMain.TextBoxChange txt
    > End Sub
    >
    > 'Form FMain
    > Private m_lngCount As Long
    > Private m_colControls As Collection
    >
    > Private Sub Form_Load()
    > Set m_colControls = New Collection
    > End Sub
    >
    > Private Sub cmdAdd_Click()
    > Dim clsTextBox As CTextBox
    > Dim ctl As Control
    >
    > m_lngCount = m_lngCount + 1
    > Set ctl = Controls.Add("VB.TextBox", "txt" & m_lngCount)
    > ctl.Visible = True
    >
    > Set clsTextBox = New CTextBox
    > Set clsTextBox.TextBoxControl = ctl
    > m_colControls.Add clsTextBox
    > End Sub
    >
    > Public Sub TextBoxChange(ctlText As TextBox)
    > MsgBox ctlText.Name & " fired the change event."
    > End Sub
    >
    > The thing to watch out for, though, is that since you're keeping

    references
    > to the controls on the form, the form won't unload until you get rid of
    > those references. You can do that by 'Set m_colControls = Nothing' before
    > you unload the form (or in the QueryUnload event)
    >
    > --Colin McGuigan
    >
    > I hope this helps you, I know it did help me. I know this isn't

    exactly
    > what you wanted but hopefully it will get you close enough.
    >
    > Chris
    >
    >
    > "Willy Van den Driessche" <Willy.Van.denDriessche@skynet.be> wrote:
    > >Chris,
    > >
    > >thank for the reply.
    > >
    > >> I believe you can get where you want to go by allowing your control

    > >class
    > >> to create its own controls from a control array. Since the class owns

    > >them,
    > >> it should receive the control array's event messages. Make sure to

    > >declare
    > >> the control array WithEvents. Someone told me this is called 'vapor

    > >code'.
    > >> Dunno about that, but it should work, unless I've misinterpreted what

    > you
    > >> were asking.

    > >
    > >I don't see how that works. For instance, how do your declare a class
    > >member variable to be "a control array of textboxes". And how do you
    > >declare that withevents ?
    > >
    > >--
    > >Van den Driessche Willy
    > >For a work in progress :
    > >http://users.skynet.be/wvdd2/index.html

    >




  6. #6
    Chris Lucas Guest

    Re: Passing a control array to a method


    Willy,

    Sorry, I don't know of a way to pass a control array in one fell swoop.

    Chris Lucas

    "Willy Van den Driessche" <Willy.Van.denDriessche@skynet.be> wrote:
    >Chris, thanks again for the reply.
    >what you show is essentially a wrapper. There is one class instance per
    >control instance. Each class instance deals with the events of each own
    >observed control. I was aware of that technique.
    >I will do my best to explain my problem again. I have a form with a fixed
    >control array on it. So the dynamic creation of controls is not my main
    >problem. My real problem is this. This is a huge form containing all kinds
    >of data about an international (swift payment). Now since the form is
    >already complex enough by itself, I've decide to take the 'logical parts'
    >out of it. This nearly reduces the code in the form to initialization and
    >termination code. There are momentarily 12 classes dealing with the
    >specific parts on the screen. Some of these control combinations of
    >comboboxes and textboxes, others control listboxes, etc..
    >Now I wanted to have such a class to control several (fixed) control arrays.
    >Hence my question.
    >I could create a usercontrol for this doesn't work for the simple reason
    >that the layout is not fixed. The control logic is, but the layout depends
    >on the position of the moon, the current heat of the sun and other relevant
    >marketing parameters. And it's different for the screens where the controls
    >occur. (which I think of as ABT (a bad thing) but I guess marketing logic

    is
    >way beyond me). I currently solved this my passing all controls to my
    >controller class but I was just curious if it could be done in VB by just
    >passing the control array at once.
    >
    >Many thanks
    >--
    >Van den Driessche Willy
    >For a work in progress :
    >http://users.skynet.be/wvdd2/index.html
    >"Chris Lucas" <cdl1051@earthlink.net> wrote in message
    >news:3baeb4e5$1@news.devx.com...
    >>
    >> Willy,
    >>
    >> What follows is not mine but Colin McGuigan's. He fielded a similar
    >> question for me sometime ago.
    >>
    >> Colin wrote...
    >>
    >> It's more difficult than you might think. I believe Matthew Curland's

    >book
    >> goes into this in some detail, but the basic gist is you need to have

    a
    >> Class Module that declares that control type WithEvents, and then, when

    it
    >> receives those events, passes it on to your global handler. Something

    >like
    >> this (vapor code):
    >>
    >> 'Class CTextBox
    >> Option Explicit
    >>
    >> Private WithEvents txt As TextBox
    >>
    >> Public Property Get TextBoxControl() As TextBox
    >> Set TextBoxControl = txt
    >> End Property
    >>
    >> Public Property Set TextBoxControl(newTextBoxControl As TextBox)
    >> Set txt = newTextBoxControl
    >> End Property
    >>
    >> Private Sub txt_Change()
    >> FMain.TextBoxChange txt
    >> End Sub
    >>
    >> 'Form FMain
    >> Private m_lngCount As Long
    >> Private m_colControls As Collection
    >>
    >> Private Sub Form_Load()
    >> Set m_colControls = New Collection
    >> End Sub
    >>
    >> Private Sub cmdAdd_Click()
    >> Dim clsTextBox As CTextBox
    >> Dim ctl As Control
    >>
    >> m_lngCount = m_lngCount + 1
    >> Set ctl = Controls.Add("VB.TextBox", "txt" & m_lngCount)
    >> ctl.Visible = True
    >>
    >> Set clsTextBox = New CTextBox
    >> Set clsTextBox.TextBoxControl = ctl
    >> m_colControls.Add clsTextBox
    >> End Sub
    >>
    >> Public Sub TextBoxChange(ctlText As TextBox)
    >> MsgBox ctlText.Name & " fired the change event."
    >> End Sub
    >>
    >> The thing to watch out for, though, is that since you're keeping

    >references
    >> to the controls on the form, the form won't unload until you get rid of
    >> those references. You can do that by 'Set m_colControls = Nothing' before
    >> you unload the form (or in the QueryUnload event)
    >>
    >> --Colin McGuigan
    >>
    >> I hope this helps you, I know it did help me. I know this isn't

    >exactly
    >> what you wanted but hopefully it will get you close enough.
    >>
    >> Chris
    >>
    >>
    >> "Willy Van den Driessche" <Willy.Van.denDriessche@skynet.be> wrote:
    >> >Chris,
    >> >
    >> >thank for the reply.
    >> >
    >> >> I believe you can get where you want to go by allowing your control
    >> >class
    >> >> to create its own controls from a control array. Since the class owns
    >> >them,
    >> >> it should receive the control array's event messages. Make sure to
    >> >declare
    >> >> the control array WithEvents. Someone told me this is called 'vapor
    >> >code'.
    >> >> Dunno about that, but it should work, unless I've misinterpreted what

    >> you
    >> >> were asking.
    >> >
    >> >I don't see how that works. For instance, how do your declare a class
    >> >member variable to be "a control array of textboxes". And how do you
    >> >declare that withevents ?
    >> >
    >> >--
    >> >Van den Driessche Willy
    >> >For a work in progress :
    >> >http://users.skynet.be/wvdd2/index.html

    >>

    >
    >



  7. #7
    Willy Van den Driessche Guest

    Re: Passing a control array to a method

    > Sorry, I don't know of a way to pass a control array in one fell
    swoop.

    That's OK, Chris, thanks for replying.
    --
    Van den Driessche Willy
    For a work in progress :
    http://users.skynet.be/wvdd2/index.html



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center