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:
Public MyControl As Control For Each MyControl In Me.Controls 'Some code goes here Next
Printable View
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:
Public MyControl As Control For Each MyControl In Me.Controls 'Some code goes here Next
well you could do something like
VB Code:
Dim frm as Form Dim ctrl as Control for each frm in Forms for each ctrl in frm.Controls 'some code goes here next next
but the forms collection only gives you loaded forms.. so this would work if all your forms were loaded...
Bastard. You beat me :)
VB Code:
Dim myControl As Control, myForm As Form For Each myForm In Forms For Each myControl In myForm.Controls MsgBox myForm.Name & "." & myControl.Name Next Next
:D and I even included the disclaimer about the forms being loaded and all :pQuote:
Originally posted by plenderj
Bastard. You beat me :)
VB Code:
Dim myControl As Control, myForm As Form For Each myForm In Forms For Each myControl In myForm.Controls MsgBox myForm.Name & "." & myControl.Name Next Next
But,
I got more proper hungarian notation, AND I got this in too :
VB Code:
MsgBox myForm.Name & "." & myControl.Name
whereas you had this pathetic attempt ;)
VB Code:
'some code goes here
AND AND AND!!!
My code is properly capitalized.
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...Quote:
Originally posted by plenderj
But,
I got more proper hungarian notation, AND I got this in too :
VB Code:
MsgBox myForm.Name & "." & myControl.Name
whereas you had this pathetic attempt ;)
VB Code:
'some code goes here
So :P
so :p
:D :D
Thanks...you all beat me...I was just 10 minutes slower...
First off all thanks...
The
VB Code:
'some code goes here
will be replaced by...
VB Code:
For Each frm In Forms For Each myControl In frm.Controls If TypeOf myControl Is TextBox Then Call TextBoxes End If Next Next 'Sub filling string for Text-write Public Sub TextBoxes() TextInfo = TextInfo & myControl.Parent.Name & "." & myControl.Name If Not myControl.Index = "" Then TextInfo = TextInfo & "(" & myControl.Index & ")" End If TextInfo = TextInfo & "|" & myControl.Text & vbCrLf End Sub
Just thought it wasn't to display the code with my question...as it has nothing do with the question.
ooooo got me there :mad: ;)
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 :DQuote:
Originally posted by Chrissie
Thanks...you all beat me...I was just 10 minutes slower...
First off all thanks...
The
VB Code:
'some code goes here
will be replaced by...
VB Code:
For Each frm In Forms For Each myControl In frm.Controls If TypeOf myControl Is TextBox Then Call TextBoxes End If Next Next 'Sub filling string for Text-write Public Sub TextBoxes() TextInfo = TextInfo & myControl.Parent.Name & "." & myControl.Name If Not myControl.Index = "" Then TextInfo = TextInfo & "(" & myControl.Index & ")" End If TextInfo = TextInfo & "|" & myControl.Text & vbCrLf End Sub
Just thought it wasn't to display the code with my question...as it has nothing do with the question.
That's just because I'm better than kleinma.Quote:
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 :D
Better in every way.
he is :( :(Quote:
Originally posted by plenderj
That's just because I'm better than kleinma.
Better in every way.
*runs away to corner to sob*
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*Quote:
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...
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?
here ya go :)
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...Quote:
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..
as you get closer you hear the cry more clearly...
it says......
DATABASE :D
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
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:
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...
there is a "type of" operator
VB Code:
for each ctrl in me.controls if typeof ctrl is textbox then 'DO THIS elseif typeof ctrl is combobox then 'DO THIS elseif typeof ctrl is Label then 'DO THIS end if next
I'm working on a solution...
How's this ?
I think that should work
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
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:
Private Type minorSettingsType name As String text As String type As Long index As String End Type
Last problem is how to read it in again?!?
VB Code:
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..
This is closer...
VB Code:
Option Explicit Private Enum controlType nTextBox nCommandButton nFrame nLabel End Enum Private Type minorSettingsType name As String text As String type As Long index As Long End Type Private Type globalSettingsType myCtrl() As minorSettingsType End Type Private Sub Command2_Click() Dim myControl As Control '' save the settings '' Dim x As globalSettingsType ReDim x.myCtrl(0) For Each myControl In Controls With x.myCtrl(UBound(x.myCtrl)) .name = myControl.name Select Case TypeName(myControl) Case "TextBox": .type = nTextBox: .text = myControl.text Case "CommandButton": .type = nCommandButton: .text = myControl.Caption Case "Label": .type = nLabel: .text = myControl.Caption Case "Frame": .type = nFrame: .text = myControl.Caption End Select .index = getIndexIfArray(myControl) End With ReDim Preserve x.myCtrl(UBound(x.myCtrl) + 1) Next Open "c:\settings.dat" For Binary As #1 Put #1, , x Close #1 MsgBox "Have saved settings. Now about to clear form", vbInformation '' clear the screen '' For Each myControl In Controls If Not myControl.Tag = "save+load" Then Select Case True Case TypeName(myControl) = "TextBox": myControl.text = "" Case (TypeName(myControl) = "CommandButton") Or _ (TypeName(myControl) = "Label") Or _ (TypeName(myControl) = "Frame"): myControl.Caption = "" End Select End If Next MsgBox "Screen cleared. Now loading information back in", vbInformation '' read back the settings '' Dim y As globalSettingsType, i As Long Open "c:\settings.dat" For Binary As #1 Get #1, , y Close #1 For i = 0 To UBound(y.myCtrl) - 1 For Each myControl In Controls If (myControl.name = y.myCtrl(i).name) Then If Not getIndexIfArray(myControl) = -1 Then If myControl.index = getIndexIfArray(myControl) Then Select Case True Case y.myCtrl(i).type = nTextBox: myControl.text = y.myCtrl(i).text Case (y.myCtrl(i).type = nCommandButton) Or _ (y.myCtrl(i).type = nFrame) Or _ (y.myCtrl(i).type = nLabel): myControl.Caption = y.myCtrl(i).text End Select End If End If End If Next Next MsgBox "Information loaded", vbInformation End Sub Private Function getIndexIfArray(ByVal myObject As Object) As Long On Error Resume Next getIndexIfArray = -1 getIndexIfArray = myObject.index End Function