From: Mayayana on 19 Jun 2010 21:29 | so | different forms can declare their own settings type and send them to the | same function in the utility module to read and save. Why would you have different instances of the same settings UDT? Then each form would overwrite the last set of settings saved to disk. You can declare these in a .bas: Public Type ProgramSettings Name1 as String '------ other variables here End Type Public Sets as ProgramSettings Then from anywhere in the program you can access that value: Sets.Name1 = "SomeName" Label1.Caption = Sets.Name1 When you read the .dat file at startup you fill the Sets variables. When you shut down you just write Sets to disk. | If i declare the settings type Private in the form, I cant' pass it to a | public module. | If I declare it Public I get the error can't do public type in private | module. | i remember seeing discussions here, but since i wasn't using udts I didn't | pay attention | searching google now but if any one has the solution I"d appreciate it. | Thanks | Mark | |
From: GS on 20 Jun 2010 00:52 mp explained on 6/19/2010 : > "Larry Serflaten" <serflaten(a)gmail.com> wrote in message > news:hvbukn$d7d$1(a)news.eternal-september.org... >> >> "Tony Toews" <ttoews(a)telusplanet.net> wrote >>> >>> > It turns out that a UDT can be read from/written >>> >to disk as a binary file. Despite the string values >>> >being pointers, VB will nevertheless write all data >>> >stored in the UDT in serial fashion! >>> >>> Yowzer, that is an interesting idea. >> >> It been available for a while.... >> (post from 14 Mar 2001) >> http://groups.google.com/group/microsoft.public.vb.general.discussion/msg/8ce436e561783c27?hl=en >> >> <g> >> LFS >> >> > a very nice technique Thanks. > What i'm struggling with now is how to put the function in a utility.bas > module so i can use in all projects, then call it from any form. so different > forms can declare their own settings type and send them to the same function > in the utility module to read and save. > If i declare the settings type Private in the form, I cant' pass it to a > public module. > If I declare it Public I get the error can't do public type in private > module. > i remember seeing discussions here, but since i wasn't using udts I didn't > pay attention > searching google now but if any one has the solution I"d appreciate it. > Thanks > Mark As Mayayana suggests, you should only ever have one instance of a UDT that is declared in a standard module with public scope so its members can be accessed from anywhere in your project. For example, the following two functions are what I actually use in my projects. They're not that different than the code examples I gave you previously, but they handle all my UDTs. The OptionsData is managed by an options dialog. The other two udeDataType members are handled by various procs in standard or class modules, as appropriate. I'm sure these functions could also be modified to pass a ref to any UDT as the 2nd arg, but I chose to use an enum. Function GetDataInFile(FileName As String, FileType As udeDataType) As Boolean Dim bOpen As Boolean If Dir(FileName) = "" Then Exit Function On Error GoTo ErrHandler Open FileName For Binary As #1 bOpen = True Select Case FileType Case udeDataType.StartupInf: Get #1, , StartupStatus Case udeDataType.SettingsData: Get #1, , AppSettings Case udeDataType.OptionsData: Get #1, , AppOptions End Select ErrHandler: If bOpen Then Close #1 GetDataInFile = (Err = 0) End Function Function PutDataInFile(FileName As String, FileType As udeDataType) As Boolean Dim bOpen As Boolean If Dir(FileName) <> "" Then Kill FileName On Error GoTo ErrHandler Open FileName For Binary As #1 bOpen = True Select Case FileType Case udeDataType.StartupInf: Put #1, , StartupStatus Case udeDataType.SettingsData: Put #1, , AppSettings Case udeDataType.OptionsData: Put #1, , AppOptions End Select ErrHandler: If bOpen Then Close #1 PutDataInFile = (Err = 0) End Function HTH -- Garry Free usenet access at http://www.eternal-september.org ClassicVB Users Regroup! comp.lang.basic.visual.misc
From: Larry Serflaten on 20 Jun 2010 07:25 "mp" <nospam(a)Thanks.com> wrote > a very nice technique Thanks. > What i'm struggling with now is how to put the function in a utility.bas > module so i can use in all projects, then call it from any form. so > different forms can declare their own settings type and send them to the > same function in the utility module to read and save. > If i declare the settings type Private in the form, I cant' pass it to a > public module. > If I declare it Public I get the error can't do public type in private > module. > i remember seeing discussions here, but since i wasn't using udts I didn't > pay attention > searching google now but if any one has the solution I"d appreciate it. > Thanks > Mark It seems to me each app would have its own options and user settings, based on the task the program performs. But there are some common values that could be saved (such as window placement) which would be common to many applications. Also realize you can nest UDTs so that you can have multiple groups of options where the options are the same for different objects. Window placement (for example) always uses Left, Top, Width, and Height, so it may be benificial to make that one UDT and nest that in a larger UDT that has other form settings. For a quick example, add a textbox to a new form and set its Index property to 0. Then add a standard module to the project and paste in the code below. When running, note you can change the form size and placement as well as the text shown in the textboxes. Double clicking a textbox changes the text to some random color. All those changes are saved when the last form closes.... LFS ' [ Module1 code ] ------------------- Option Explicit ' Form rectangles Public Type WindowSettings Left As Long Top As Long Width As Long Height As Long End Type ' Other options (textbox options) Public Type TextDisplay Value As String Color As Long End Type ' Form options (each form has placement and 3 textboxes) Public Type FormSettings Init As Boolean Window As WindowSettings Text(0 To 2) As TextDisplay End Type Public Type UserSettings '(the app has 3 forms) UI(0 To 2) As FormSettings End Type Private Enum SettingsIO Read Save End Enum Public UISet As UserSettings Private AppForms As Long Public Property Get FormCount() As Long FormCount = AppForms If AppForms = 0 Then DoSettings Read End Property Public Property Let FormCount(ByVal Value As Long) AppForms = Value If AppForms = 0 Then DoSettings Save End Property Private Sub DoSettings(ByVal Operation As SettingsIO) Dim file As String Dim io As Long ' Quick and dirty temp file file = LCase(Environ("temp") & "\demo.dat") io = FreeFile Open file For Binary As io If Operation = Read Then Get #io, , UISet Else Put #io, , UISet MsgBox file, vbOKOnly, "File Path Information" End If Close End Sub ' [ Form1 code ] --------------- Option Explicit Private MyIndex As Long ' Note FormCount values take care of settings IO Private Sub Form_Initialize() MyIndex = FormCount FormCount = FormCount + 1 End Sub Private Sub Form_Terminate() FormCount = FormCount - 1 End Sub Private Sub Form_Load() Dim t As Long Caption = "FORM #" & CStr(MyIndex) ' Add new textboxes For t = 0 To 2 If t > 0 Then Load Text1(t) Text1(t).Visible = True End If Text1(t).Move 120, t * 420 + 120, 1500, 320 Text1(t).Text = UISet.UI(MyIndex).Text(t).Value Text1(t).ForeColor = UISet.UI(MyIndex).Text(t).Color Next ' Pick up window placement With UISet.UI(MyIndex) If .Init Then Me.Move .Window.Left, .Window.Top, .Window.Width, .Window.Height End If End With ' Show more forms If MyIndex < 2 Then With New Form1 .Show End With End If End Sub Private Sub Form_Unload(Cancel As Integer) Dim i ' Save my settings With UISet.UI(MyIndex) .Init = True .Window.Left = Left .Window.Top = Top .Window.Width = Width .Window.Height = Height For i = 0 To 2 .Text(i).Color = Text1(i).ForeColor .Text(i).Value = Text1(i).Text Next End With ' Kills all forms For Each i In Forms Unload i Next End Sub Private Sub Text1_DblClick(Index As Integer) Do Text1(Index).ForeColor = QBColor(Int(Rnd * 16)) Loop Until Text1(Index).ForeColor <> vbWhite Text1(Index).SelStart = 0 End Sub
From: mp on 20 Jun 2010 11:39 "Mayayana" <mayayana(a)invalid.nospam> wrote in message news:hvjqqn$t7p$1(a)news.eternal-september.org... >| so > | different forms can declare their own settings type and send them to the > | same function in the utility module to read and save. > > Why would you have different instances of the > same settings UDT? because different projects have different forms with different things it might want to save (other than size and position which i realize are common to any form) maybe i'm thinking of this wrong but my idea was to have only one copy of the code that would read and write settings. (code reuse - code in one place and one place only) then any form could define it's own udt with what was needed for that form >Then each form would > overwrite the last set of settings saved to disk. each form would also define the filename to save to so one form would not overwrite settings for another (app specific) then all forms would just call that one universal read/write function in a utility.bas that is common to all projects. guess i can't do it like that....so i have to reproduce the read/write code in all projects that want to use that technique? or am i still missing something? thanks mark > You can declare these in a .bas: > > Public Type ProgramSettings > Name1 as String > '------ other variables here > End Type > > Public Sets as ProgramSettings > > Then from anywhere in the program you can > access that value: > > Sets.Name1 = "SomeName" > > Label1.Caption = Sets.Name1 > > When you read the .dat file at startup you fill > the Sets variables. When you shut down you > just write Sets to disk. > > | If i declare the settings type Private in the form, I cant' pass it to a > | public module. > | If I declare it Public I get the error can't do public type in private > | module. > | i remember seeing discussions here, but since i wasn't using udts I > didn't > | pay attention > | searching google now but if any one has the solution I"d appreciate it. > | Thanks > | Mark > | > | > >
From: mp on 20 Jun 2010 11:48
"GS" <gesansom(a)netscape.net> wrote in message news:hvk6qc$ei1$1(a)news.eternal-september.org... > mp explained on 6/19/2010 : snip >> searching google now but if any one has the solution I"d appreciate it. >> Thanks >> Mark > > As Mayayana suggests, you should only ever have one instance of a UDT that > is declared in a standard module with public scope so its members can be > accessed from anywhere in your project. > see my reply to him...different forms have different data to save - i don't see how to create a universal udt that would apply to all forms in all projects(the ones that want to save state that is) > For example, the following two functions are what I actually use in my > projects. They're not that different than the code examples I gave you > previously, but they handle all my UDTs. The OptionsData is managed by an > options dialog. The other two udeDataType members are handled by various > procs in standard or class modules, as appropriate. > so you have one OptionsData udt that is common to all forms in all projects? my current project is a form with multiple listboxes displaying file names in selected folders- user(me) adds however many listboxes for multiple folder selections. so what this form wants to save(beyond size and location) is a list of foldernames. no other form in my other projects has that need so it's app specific... does that make sense? thanks to everyone who is helping me understand this I really appreciate all your time and effort mark |