Results 1 to 25 of 25

Thread: All Controls in Application

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    The Netherlands
    Posts
    96

    All Controls in Application

    Is their a way to retrieve all Controls in an Application?
    With the following code I get all controls in the current Form..but how to get them all?

    VB Code:
    1. Public MyControl As Control
    2.         For Each MyControl In Me.Controls
    3. 'Some code goes here
    4.         Next

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    well you could do something like
    VB Code:
    1. Dim frm as Form
    2. Dim ctrl as Control
    3.  
    4. for each frm in Forms
    5.     for each ctrl in frm.Controls
    6.         'some code goes here
    7.     next
    8. next

    but the forms collection only gives you loaded forms.. so this would work if all your forms were loaded...

  3. #3
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Bastard. You beat me

    VB Code:
    1. Dim myControl As Control, myForm As Form
    2.     For Each myForm In Forms
    3.         For Each myControl In myForm.Controls
    4.             MsgBox myForm.Name & "." & myControl.Name
    5.         Next
    6.     Next

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    Originally posted by plenderj
    Bastard. You beat me

    VB Code:
    1. Dim myControl As Control, myForm As Form
    2.     For Each myForm In Forms
    3.         For Each myControl In myForm.Controls
    4.             MsgBox myForm.Name & "." & myControl.Name
    5.         Next
    6.     Next
    and I even included the disclaimer about the forms being loaded and all

  5. #5
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    But,

    I got more proper hungarian notation, AND I got this in too :

    VB Code:
    1. MsgBox myForm.Name & "." & myControl.Name

    whereas you had this pathetic attempt
    VB Code:
    1. 'some code goes here



    AND AND AND!!!
    My code is properly capitalized.


    So :P

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    Originally posted by plenderj
    But,

    I got more proper hungarian notation, AND I got this in too :

    VB Code:
    1. MsgBox myForm.Name & "." & myControl.Name

    whereas you had this pathetic attempt
    VB Code:
    1. 'some code goes here

    So :P
    i took that from the posters original code which to me implied that they already had code or an idea for code that they wanted there... thus making my code much more understandable to the person that asked the question...

    so


  7. #7

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    The Netherlands
    Posts
    96

    Thanks..

    Thanks...you all beat me...I was just 10 minutes slower...
    First off all thanks...

    The

    VB Code:
    1. 'some code goes here

    will be replaced by...

    VB Code:
    1. For Each frm In Forms
    2.     For Each myControl In frm.Controls
    3.         If TypeOf myControl Is TextBox Then
    4.             Call TextBoxes
    5.         End If
    6.     Next
    7. Next
    8.  
    9. 'Sub filling string for Text-write
    10. Public Sub TextBoxes()
    11.         TextInfo = TextInfo & myControl.Parent.Name & "." & myControl.Name
    12.                    
    13.         If Not myControl.Index = "" Then
    14.             TextInfo = TextInfo & "(" & myControl.Index & ")"
    15.         End If
    16.        
    17.         TextInfo = TextInfo & "|" & myControl.Text & vbCrLf
    18. End Sub

    Just thought it wasn't to display the code with my question...as it has nothing do with the question.

  8. #8
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    ooooo got me there

  9. #9
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Thanks..

    Originally posted by Chrissie
    Thanks...you all beat me...I was just 10 minutes slower...
    First off all thanks...

    The

    VB Code:
    1. 'some code goes here

    will be replaced by...

    VB Code:
    1. For Each frm In Forms
    2.     For Each myControl In frm.Controls
    3.         If TypeOf myControl Is TextBox Then
    4.             Call TextBoxes
    5.         End If
    6.     Next
    7. Next
    8.  
    9. 'Sub filling string for Text-write
    10. Public Sub TextBoxes()
    11.         TextInfo = TextInfo & myControl.Parent.Name & "." & myControl.Name
    12.                    
    13.         If Not myControl.Index = "" Then
    14.             TextInfo = TextInfo & "(" & myControl.Index & ")"
    15.         End If
    16.        
    17.         TextInfo = TextInfo & "|" & myControl.Text & vbCrLf
    18. End Sub

    Just thought it wasn't to display the code with my question...as it has nothing do with the question.
    glad to hear you got it working.. me and plender were just messin with eachother... besides he has a habit of usually beating me to the post

  10. #10
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    Re: Re: Thanks..

    Originally posted by kleinma
    glad to hear you got it working.. me and plender were just messin with eachother... besides he has a habit of usually beating me to the post
    That's just because I'm better than kleinma.
    Better in every way.

  11. #11
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Re: Re: Thanks..

    Originally posted by plenderj
    That's just because I'm better than kleinma.
    Better in every way.
    he is

    *runs away to corner to sob*

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    The Netherlands
    Posts
    96

    Don't runaway (yet)

    Well...you might have some suggestions or code for me.

    I'm trying to create an option in my App to Save all values of the Textboxes, ListBoxes, CheckBoxes, OptionBoxes etc. to a file...and read in again...

    It was all working fine..but when I started using some Control Arrays it didn't work out anymore. As it's more difficult to store the stuff in the file...

    How can I store...besides
    frmInput.txtBla|TextValue
    also store the type; frmInput.txtBla|TextValue|TextBox

    The hardest part to my opinion is controls in Control Arrays do have an index other's don't (343 Error)...

    This gives me a headache aswell..when loading...
    txtBox.Text
    checkBox.Value
    cboBox.Index

    Might sound a little confusing..that's to make it more challenging :P
    See who's the fastest...

  13. #13
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Don't runaway (yet)

    Originally posted by Chrissie
    Well...you might have some suggestions or code for me.

    I'm trying to create an option in my App to Save all values of the Textboxes, ListBoxes, CheckBoxes, OptionBoxes etc. to a file...and read in again...

    It was all working fine..but when I started using some Control Arrays it didn't work out anymore. As it's more difficult to store the stuff in the file...

    How can I store...besides
    frmInput.txtBla|TextValue
    also store the type; frmInput.txtBla|TextValue|TextBox

    The hardest part to my opinion is controls in Control Arrays do have an index other's don't (343 Error)...

    This gives me a headache aswell..when loading...
    txtBox.Text
    checkBox.Value
    cboBox.Index

    Might sound a little confusing..that's to make it more challenging :P
    See who's the fastest...
    *runs back to help*

    well i would use a database if i were doing this.. because it is easier to store the info...

    but lets say you have 2 textboxes and a combobox

    you have 3 fields in a database

    CustomerName
    CustomerAccount
    ReasonForCall

    your controls on the form are
    txtCustName
    txtAcctNum
    cmboCallRsn

    you can easily store these values in the cooresponding fields in the database and read them back in the form just as easy because you will know what goes with what...

    make sense?

  14. #14
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    here ya go
    Attached Files Attached Files

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    The Netherlands
    Posts
    96

    Yeah...

    I have some code which did read in and write everything to a textfile...

    Problem is I want to automatically add an extra thingie to the file which says cboBla|0|ComboBox, txtBla|text|Textbox

    As cbo will be .ListIndex, Textbox will be .Text, CheckBoxes will be .Value

    That's the whole problem..

  16. #16
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Yeah...

    Originally posted by Chrissie
    I have some code which did read in and write everything to a textfile...

    Problem is I want to automatically add an extra thingie to the file which says cboBla|0|ComboBox, txtBla|text|Textbox

    As cbo will be .ListIndex, Textbox will be .Text, CheckBoxes will be .Value

    That's the whole problem..
    off in the distance.. you hear this faint cry...

    as you get closer you hear the cry more clearly...

    it says......


    DATABASE

  17. #17
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    You could write a setting that states what sort of object this piece of text is going into.
    And then when reading check what sort of object its going into, and then use .Text, .Value or .Caption accordingly

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    The Netherlands
    Posts
    96

    Database / Flatfile

    Database or Flatfile isn't the problem...
    When reading out of the flatfile I put it in an array...so would I do when using a database....
    cboBla|0|ControlBox

    Problem is the .value .text .listindex
    and how to get the Control Type...
    Control.Type or something like this...

    VB Code:
    1. elements = Split(data, "|")

    If someone has a suggestions thanks...If not this topic can be closed..

    The text file is meant to be used as an Config file...like an INI file...So user can read in their settings again...

  19. #19
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Database / Flatfile

    there is a "type of" operator
    VB Code:
    1. for each ctrl in me.controls
    2.     if typeof ctrl is textbox then
    3.         'DO THIS
    4.     elseif typeof ctrl is combobox then
    5.         'DO THIS
    6.     elseif typeof ctrl is Label then
    7.         'DO THIS
    8.     end if
    9. next

  20. #20
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    I'm working on a solution...

  21. #21
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    How's this ?
    Attached Files Attached Files

  22. #22
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    I think that should work

  23. #23

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    The Netherlands
    Posts
    96

    Almost.....Textbox Arrays

    Almost working...just try out to enter different values into the TextBox Arrays...

    Text1(0) and Text1(1)

    That doesn't work....we're close though

  24. #24

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    The Netherlands
    Posts
    96

    Almost

    I almost have it working they way I want....

    I added an Index to the array...I'm able to read the Index of the Control when it's in a Control Array...

    VB Code:
    1. Private Type minorSettingsType
    2.     name As String
    3.     text As String
    4.     type As Long
    5.     index As String
    6. End Type

    Last problem is how to read it in again?!?

    VB Code:
    1. Case (y.myCtrl(i).type = nTextBox): myControl(y.MyCtrl(i).index).text = y.myCtrl(i).text

    It should do something like Control(0) = text....
    But I can't seem to get it working..

  25. #25
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    This is closer...

    VB Code:
    1. Option Explicit
    2.  
    3. Private Enum controlType
    4.     nTextBox
    5.     nCommandButton
    6.     nFrame
    7.     nLabel
    8. End Enum
    9.  
    10. Private Type minorSettingsType
    11.     name As String
    12.     text As String
    13.     type As Long
    14.     index As Long
    15. End Type
    16. Private Type globalSettingsType
    17.     myCtrl() As minorSettingsType
    18. End Type
    19.  
    20.  
    21.  
    22. Private Sub Command2_Click()
    23.    
    24.     Dim myControl As Control
    25.    
    26.    
    27.    
    28.     '' save the settings
    29.     ''
    30.     Dim x As globalSettingsType
    31.     ReDim x.myCtrl(0)
    32.     For Each myControl In Controls
    33.         With x.myCtrl(UBound(x.myCtrl))
    34.             .name = myControl.name
    35.             Select Case TypeName(myControl)
    36.                 Case "TextBox":       .type = nTextBox:       .text = myControl.text
    37.                 Case "CommandButton": .type = nCommandButton: .text = myControl.Caption
    38.                 Case "Label":         .type = nLabel:         .text = myControl.Caption
    39.                 Case "Frame":         .type = nFrame:         .text = myControl.Caption
    40.             End Select
    41.             .index = getIndexIfArray(myControl)
    42.         End With
    43.         ReDim Preserve x.myCtrl(UBound(x.myCtrl) + 1)
    44.     Next
    45.     Open "c:\settings.dat" For Binary As #1
    46.         Put #1, , x
    47.     Close #1
    48.     MsgBox "Have saved settings. Now about to clear form", vbInformation
    49.    
    50.     '' clear the screen
    51.     ''
    52.     For Each myControl In Controls
    53.         If Not myControl.Tag = "save+load" Then
    54.             Select Case True
    55.                 Case TypeName(myControl) = "TextBox": myControl.text = ""
    56.                 Case (TypeName(myControl) = "CommandButton") Or _
    57.                     (TypeName(myControl) = "Label") Or _
    58.                     (TypeName(myControl) = "Frame"): myControl.Caption = ""
    59.             End Select
    60.         End If
    61.     Next
    62.     MsgBox "Screen cleared. Now loading information back in", vbInformation
    63.    
    64.     '' read back the settings
    65.     ''
    66.     Dim y As globalSettingsType, i As Long
    67.     Open "c:\settings.dat" For Binary As #1
    68.         Get #1, , y
    69.     Close #1
    70.     For i = 0 To UBound(y.myCtrl) - 1
    71.         For Each myControl In Controls
    72.             If (myControl.name = y.myCtrl(i).name) Then
    73.                 If Not getIndexIfArray(myControl) = -1 Then
    74.                     If myControl.index = getIndexIfArray(myControl) Then
    75.                         Select Case True
    76.                             Case y.myCtrl(i).type = nTextBox: myControl.text = y.myCtrl(i).text
    77.                             Case (y.myCtrl(i).type = nCommandButton) Or _
    78.                                  (y.myCtrl(i).type = nFrame) Or _
    79.                                  (y.myCtrl(i).type = nLabel): myControl.Caption = y.myCtrl(i).text
    80.                         End Select
    81.                     End If
    82.                 End If
    83.             End If
    84.         Next
    85.     Next
    86.     MsgBox "Information loaded", vbInformation
    87.    
    88. End Sub
    89.  
    90. Private Function getIndexIfArray(ByVal myObject As Object) As Long
    91.     On Error Resume Next
    92.     getIndexIfArray = -1
    93.     getIndexIfArray = myObject.index
    94. End Function

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