I’ve sub-classed a listbox which when supplied correct parameters will be the one and only listbox that I use for my application. One of the setup procedures involves passing through a column width value. In most instances I will allow the user to resize the column width and I would prefer that the resized column width be saved in a preferences file somewhere.
I haven’t dealt with setting and retrieving these types of settings before. Is there a recommended way to accomplish this? Is it merely a text file, I’m familiar with the old INI files from years ago or should I employ SQLite database? Is there another approach? I know I’ve referenced a listbox as an example but other settings I’m assuming could be window positions and sizes etc. and so I reckon there could be quite a lot of such settings for an entire application.
There’s a bunch of ways you can do this. For large volumes of data, an SQLdatabase might be more appropriate, for things like window size and location, I wouldn’t imagine that it would be a benefit.
I’ve seen many people simply write out their variables in text files (one of my apps uses this method), if you developing x-plat then you may want to consider using your own format. Binary files can be a little more difficult to start with.
If you’re doing Mac only, then look at using NSDictionary and NSArray, these make it simple to store most data types in a structured manor and format independent (it could be a variety of formats, but you don’t need to worry as the OS deals with it for you). You then use them just like a Dictionary or Array object.
I recommend plist files, and use MacCrafters’ “pList Class for Realbasic” which sadly doesn’t seem to be supported anymore, but it’s easy to support one’s self as it’s all unencrypted and open. I use plists not only for prefs but for application data files; the human-readable XML text is a plus, as is the ability to use third-party readers for debugging. Being based on dictionaries, plists make for very readable code as well.
Here’s a useful thread about prefs on the old forum:
Maybe what Markus was referring to is the fact that Dave’s URL’s are not appearing as live links in the forum; I’m not sure why that is happening (but I don’t think it has anything to do with the slashes, lol).
I usually save app preferences to a plain text file, in JSON format. The JSONItem class works a lot like the Dictionary class, with the added ability to be converted to and from strings.
e.g.
Dim prefs As New JSONItem
prefs.Value("Col1Width") = 100
prefs.Value("Col1Head") = "Column One"
prefs.Value("Col1BGColor") = &cC0C0C0
Dim s As String = prefs.ToString() 'this string can be written to a file
prefs = New JSONItem(s) 'constructing a JSONItem from a string
JSON is cross-platform, built-in to Xojo, and has a lot of 3rd party editors.
Thinking about it, I would actually suggest that you use each platforms preferred preference mechanism. This would reduce the likelihood of a new version breaking your application.
For instance Apple advise against reading & writing files directly the Preferences Folder, instead they recommend usage of NSUserDefaults to store preference values. Document Data should be stored however suits you, but it should be in a location of the users choosing.
I’m not sure what Windows uses at the moment, back in early naughties, there seemed to be a push to move to XML files in a special location, but most people were still using the Windows Registry for storing preference values.