Click here to Skip to main content
13,141,747 members (58,086 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I wonder if anyone can help me figure this out?

I have a Marquee which scrolls text on TV screens and is working ok. Thing is that I need the end user to be able to change certain aspects of it through a Windows Form. I've been trying to use the Visual Studio Settings (which save to an AppConfig.XML file) to save the users' settings.

The text, font name and font size all save and restore perfectly, but for some reason the font colour, which is a System.Drawing.Color type, is always wrong.

When I open the AppConfig.XML or AppConfig.EXE.XML files in an external editor, the colour variable is correct, but the program seems to be getting its data from somewhere else for only this one object....

This is the code I'm using on the form:

        Dim FontName As String = My.Settings.ScrollFontName
        Dim FontSize As Integer = My.Settings.ScrollFontSize
        Dim FS As New Font(FontName, CInt(FontSize / 2), FontStyle.Regular)
        Dim FontColour As System.Drawing.Color = My.Settings.ScrollColour
        ScrollerSetupLbl.ForeColor = FontColour
        ScrollerSetupLbl.Font = FS
        ScrollerSetupLbl.ScrollLeftToRight = False
        ScrollerSetupLbl.MarqueeText = My.Settings.ScrollText

(In case you're wondering about why I'm dividing the font size in half, the scrollbar on the setup form is half the height of the scrollbar on the main screen.)

This is the AppConfig XML file:

<?xml version="1.0" encoding="utf-8"?>
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="Secure_Kiosk.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
      <setting name="ScrollFontName" serializeAs="String">
      <setting name="ScrollFontSize" serializeAs="String">
      <setting name="ScrollColour" serializeAs="String">
      <setting name="ScrollText" serializeAs="String">
        <value>Test text here</value>

Note that the "ScrollColour" value is "DarkBlue". I've also tried "Black", "Gold" and "White", all of which are recognised colours for the System.Drawing.Color type, but it always just displays "Red", which was the first colour I tried when I was testing it. For some reason it seems "stuck" on that one regardless.

I've also tried saving a String type variable in the settings, and converting to a System.Drawing.Color type in the form code and got exactly the same thing. The first colour I set "sticks" and won't change. Even manually editing the config files doesn't work.

Any ideas?

Posted 8-Oct-12 10:05am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

In my opinion, introduction of Data Contract rendered application settings morally obsolete, but many did not notice the value of Data Contract approach.

Data Contract is just one method of Serialization, but most robust, non-intrusive and easy-to-use. Using the Data Contract based XML files is much better compared to application settings: no limitations, provisions for versioning and backup compatibility. Please see:[^].

Please see also my past answers:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^],
deseralize a json string array[^].

djdynamix 8-Oct-12 17:16pm
Thank you for your suggestions, Sergey. I'm relatively new to Object Oriented programming, so I'm finding some of the terminology a bit confusing. Could you possibly point me to something a bit more "for dummies" about Data Contracts and Serialization? Is there something like a simple "how to" guide on how to create and read those kinds of XML files, without having to wade through MSDN? Thanks.
I see no need for something more "for dummies". If you need "for dummies", you would better need "OOP for dummies" and ".NET for dummies", and then you would cope with regular way of getting to Data Contract. I don't know "how to", but this is not the field where "how to" is good for you. In general, avoid "how to" approach. You better need to understand.
Espen Harlinn 8-Oct-12 18:11pm
Good idea :-D
Thank you, Espen.
djdynamix 8-Oct-12 20:27pm
Sorry, both of you, but you're not being terribly helpful with comments like that. I'm not a complete noobie. On the contrary, I've completed several advanced projects in VB.Net, including multi-threaded apps and Windows Services. I am, however, a bit "behind the times" when it comes to stuff like XML. (I always relied on INI files, which are apparently now obsolete. I don't need "beginners' guides to programming", I just need to know, in terminology I can understand, a) What is a Data Contract? b) Why is Serialization an issue in XML, and c) How does any of this help me to store my users'`preferences? I'm afraid the MSDN documentation is way too over-complicated and seems to rely on the reader being familiar with half-a-dozen other long and complex documents relating to other parts of the XML language which I don't need to know and will never use.

All I need to do is save 4 user preferences and a text string in some sort of settings file in a way which is not due to become obsolete in the next couple of incarnations of the .Net Framework!
I provided you a complete solution which you can apply to any data model, settings or anything else. You will get all answers if you read the referenced pages. If you think that the crystal-clear (in this part) MSDN documentation is over-complicated... helping you would be a bit difficult.

How come you did not understand that I advise you to avoid using XML directly, whatsoever? The serializer will simply store any object graph to a stream/file and later restore it the way it was before. You don't need to take care about anything except your data (in memory). All you do is adding attributes to the types and members you want to persist, and use DataContractSerializer. This thing is difficult in implementation and easy in use; and it's much easier then application settings.

djdynamix 9-Oct-12 8:32am
I'll go through those pages again, Sergey. I guess I'm being a bit impatient because I'm on a deadline. It's also a little exasperating, given that I already have a solution which works for everything except that one colour setting, and being told, effectively, that I need to start again "from the ground up" to accomodate it.

Thanks for your time and patience.
That is understandable. If you need a short introductory code sample showing all steps to show them in few lines of code, I can do it for you -- this is easy enough. I suggest you consider this way, and when you see it's up to my claims, accept the answer formally (green button).
djdynamix 9-Oct-12 11:30am
Once again, thank you, Sergey. Some example code would definitely be helpful.

I've now realised that the reason I was confused is merely terminology. After reading through some of the pages you linked, I now see that "serializaton" is just "marshalling" by another name. Silly of me really, as "Serialization" is a much more descriptive term!

In this case, I've decided to stick with XML just this once, as I now have the whole project working, and I'd be loathe to go back and recode it when I'm on a deadline. The problem was a result of my not realising that a User AppConfig file is stored separately, in a different location to the main AppConfig file (which actually makes sense, as we don't want end users fiddling with stuff in the main application folder!), and I was reading the wrong one.

I will, however, change to the new Data Contract way of doing things at the next major application update, when, hopefully, I'll have more time. I'll also consider moving a couple of other settings out of registry at that time as well.

In the meantime, as I do intend to use your suggestion then, I'll mark your answer accepted and I thank you once again for your time and patience.
You don't need to apologize, those are mere confusion. My invitation for code sample I would prepare for you is still in place -- it will take just few lines to show how to persist all those fonts, colors and texts you have in the configuration model.

About a deadline, I'm not sure -- I find application settings bothersome. Besides, the Data Contract can help you to deal with evolving data model. A new version should add some members to the contract, and, when dealing with the persistent (XML) version of it stored by outdated assembly code, new code will simply assume members missing from XML elements as defaults, so, if the mechanism used accurately, you will never bother about different versions of files and their compatibilities, without writing any extra code to leverage this feature. And, if you can use some URI (like company site based URI) as a world-unique namespace URI, your files will be marked with the world-unique "data format tag", if I may call it this way.

As to the locations, I already provided complete recipes and code in my past answers:

Good luck,
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Have a look at: User Settings Applied[^]

Seems to do what you want ...

Best regards
Espen Harlinn
djdynamix 8-Oct-12 20:53pm
Thanks Espen. I've had a quick scan, and I think the information I need will probably be in there somewhere. I'm going to download the accompanying code and read through the article properly. The problem I've found with this subject is that most of the XML information out there is "bloated", with "Data Grid Views" and all sorts of database and dataset calls which I simply don't need. I just wish someone would put a tutorial out there which just simply explains how to do with XML what we used to do with INI files and nothing more. To be honest, if I'd known it was going to be this convoluted, I'd just have stuck a few keys and values in the registry and written the text to a .TXT file!
Espen Harlinn 9-Oct-12 3:58am
It is a bit convoluted, but it's also pretty flexible ...
Nice article, my 5, but I don't know what OP needs -- probably, due to denial of any considerable effort on his side. Having programming experience since 1974 and still being uncomfortable with pretty simple reading and understanding is something which is hard to fix in a short answer. I met a number of such cases, never knew anything which could help.
Espen Harlinn 9-Oct-12 3:58am
Thank you, Sergey :-D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170915.1 | Last Updated 10 Nov 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100