|
|
|
|
|
Home | Forums | Visual Basic 6.0 | Games | Web Directory | Software | HTML | Wiki | Blog | Contact |
|
|
|
ActiveX ControlThe Configuration File ActiveX control is another one that was written by me. Its main purpose is to store data either in memory or on disk that can be accessed easily. Data is retrieved and edited by a reference to the name of the data. I.e. if one item of data is called 'Name1', which holds the string "Barney", using the argument "Name1" in the function that gets the data will return the string "Barney". Another good thing about this control is the way you can group items together in sets; see below for details. This control can be very useful as a way of storing your program's data. You can download the control here (12.6 KB). An example project demonstrating some of the Methods of this control can be downloaded here:
One of the most useful aspects of this control is Sets, which allow you to manipulate related data. See the Sets box for more information. The term 'ConfigFile space' mentioned throughout this article refers to the space in memory that contains all the ConfigFile entries for the control you are working on. Unlike INI files, ConfigFile loads entries from a file and then stores them in memory until you are ready to save them. The examples in the article assume you are working with the following data (except for Sets): People=4 Person1=Charlie Person2=José Person3=Victoria Person4=José MethodsConfigLoadFileName As String Loads data from a file into the ConfigFile space. The file must be in this format: Data1=Value1 Data2=Value2 Etc... The equals (=) sign must be used to separate data with its reference (key). The entries to not need to be in any particular order. The file can have empty lines. ConfigSaveFileName As String Creates a file containing the contents of the ConfigFile space. Unfortunately this process can take quite a while when there are a huge number of entries. DelAllRemoves all data from the ConfigFile space. DelKeyKeyName As String Deletes a key and its associated data e.g. if 'DelKey "Person2"' is executed, the ConfigFile space would end up as: People=4 Person1=Charlie Person3=Victoria Person4=José GetAllReturns all the data in the ConfigFile space as a string. This might be useful if you want to save the data in a different way to how ConfigSave does. GetNameKeyValue As String Searches through the ConfigFile space and returns the KeyName associated with the data (value) specified as a string. The problem with this is that although two KeyNames cannot be the same, values can. This means that only the first match encountered will return the KeyName. E.g.
GetName should not be generally be used unless there are exceptional circumstances. If the KeyValue is not found, the string "[NOT FOUND]" is returned. GetValueKeyName As String, [DefValue As String] This method returns the value associated with the specified KeyName as a string. E.g.:
DefValue refers to the default value to be returned if the KeyName is not found. If DefValue is not used and the KeyName is still not found, the string "[NOT FOUND]" is returned. WriteAllNewConfig As String Replaces the data in the ConfigFile space with the NewConfig string. The new data must be in this format: KeyName1=Value KeyName2=Value etc... WriteValueKeyName As String, KeyValue As String Writes data (the KeyValue) to a KeyName. If the KeyName already exists, it will be overwritten with the new data otherwise a new KeyName is created.
WriteValueNoCheckKeyName As String, KeyValue As String Works in the same way as WriteValue but does not check to see if the KeyName already exists. This method should only be used when you are writing a lot of KeyNames at a time (e.g. when saving files) and you know that the KeyName is unique. As no check needs to be made, the writing of KeyNames is takes less time.
SetCountAddSetName As String When adding a new record to a set (by using SetItemAdd), you need to use this method to add to the count, before adding the new record. If this method is not used, the new record will overwrite the last record in the set. E.g. to add a new person to the 'People' set:
SetCountSubSetName As String When deleting a record from a set (by using SetItemDel), you need to use this method, before deleting the record. If this method is not used, SetItemDel will act in the normal way except that set will contain an extra record at the end with '[NOT FOUND]' as the values for all the KeyNames.
SetDelSetName As String, KeyName As String Deletes all KeyNames (the one specified) in a set. If you want to delete a whole set, you would use SetDel to delete all KeyNames and then use DelKey to delete the SetName. E.g.:
SetDel will not work if the set name is deleted first. SetItemAddSetName As String, KeyName As String, KeyValue As String This method adds a record to a set. Before adding a new record, you must use SetCountAdd to increase the number of records in the set (as described above). You must then use SetItemAdd for each new KeyName in the new record. E.g.:
SetItemDelSetName As String, KeyName As String, Pos As Long This method is used to delete a record from a set. You must first use SetCountSub to decrease the number of records (as described above). You must then use SetItemDel for each KeyName in the record specifying the record's position.
SetItemDownKeyName As String, Pos As Long This method swaps the value of the KeyName in any set with the data in the next position i.e. moving it down. E.g.:
In the above example, the data in position 2 would move to position 3 and the data that was in position 3 would move to position 2. See also SetItemUp. SetItemEditKeyName As String, Pos As Long, KeyValue As String Allows you to edit a record in a set.
Another way to do this is to use the WriteValue method i.e.:
SetItemUpKeyName As String, Pos As Long This method swaps the value of the KeyName in any set with the data in the previous position i.e. moving it up. E.g.:
In the above example, the data in position 2 would move to position 1 and the data that was in position 1 would move to position 2. See also SetItemDown. SetJoinSetName As String, KeyName As String Returns a string made up of all the records specified by the KeyName e.g.:
This method is most useful to join KeyNames together that were split using the SetSplit method. SetSplitSetName As String, KeyName As String, Text As String, Length As Integer Creates a set where the KeyNames include a single piece of data. If you have a very long record, you might want to split it using this method. The length of each record is determined with the Length method. You should not use this method to add data to an existing set. E.g.: the following example will use this text: This is an example of using the SetSplit method. This string will be split into a set with records, which are ten (10) characters in length. It can even handle carriage returns e.g.: This text appears two lines on.
The data will appear like this in the ConfigFile space: Example=22 Line1=This is an Line2= example o Line3=f using th Line4=e SetSplit Line5= method. Line6=This strin Line7=g will be Line8=split into Line9= a set wit Line10=h records, Line11= which are Line12= ten (10) Line13=characters Line14= in length Line15=. It can Line16=even handl Line17=e carriage Line18= returns e Line19=.g.:&ç;&ç;Th Line20=is text ap Line21=pears two Line22=lines on. Use the SetJoin method to join the text back together. blog comments powered by Disqus Martin Allen 1999 - 2011. Last updated Wednesday 10 August 2011 07:44:06 PM +0100. |