-1

I have a WinForms App that the user can use to input values. This is then serialized into a XML.

Here is a bit of code (XML and C#) to hopefully give you guys a idea.

Design View

The WinForm Zone Tab

The Combobox in the top right edited in paint by me. The idea is the user can toggle between English, French & Russian. The static data will translate to the language change to. For user inputted data maybe the Data can be selected, edited and saved under the new language.

e.g If there was only English Data inputed and French is selected the static fields would change to their French names.

As there is no French inputed data (Look at Zone Names), the English Data will populate the Grid however the user will have to edit the value to French, so Bathroom > Salle de Bains and Salle de Bains would be saved under French. If the user then toggled to English, Bathroom would be displayed again.


C# Code View [The FCMZone Class]

public class FCMZone
{
    public string id { get; set; }
    public string name { get; set; }
    public int icon { get; set; }
    public string group { get; set; }
    public string listIndex { get; set; }
    public string excludedSources { get; set; }
    public CheckState avEnable { get; set; }
    public CheckState audioOnly { get; set; }
    public MyCheckBoxItem chkBoxAVEnable;
    public MyCheckBoxItem chkBoxAudioOnly;
    public List<FCMOption> fusionZoneOptions;
    public List<FCMSource> sourceExclusion;
    public List<FCMLightingFixture> lightingFixtures;
    public List<FCMWindowFixture> windowFixtures;
    public List<FCMLightingScene> lightingScenes;
    public List<FALutronKeypad> lightingKeypadIDs;
    public List<FALutronKeypad> windowKeypadIDs;

    public FCMZone(string zId, string nm, Icon icn, string grp, CheckBox avEn, CheckBox aOnly, string lstIndx)
    {
        id = zId;
        name = nm;
        icon = icn.IconId;
        group = grp;
        avEnable = avEn.CheckState;
        audioOnly = aOnly.CheckState;
        chkBoxAVEnable = new MyCheckBoxItem(false);
        chkBoxAudioOnly = new MyCheckBoxItem(false);
        fusionZoneOptions = new List<FCMOption>();
        sourceExclusion = new List<FCMSource>();
        listIndex = lstIndx;
        excludedSources = "All Available";
        updateCheckbox();
    }


    public FCMZone(FCMZone fZone)
    {
        id = fZone.id;
        name = fZone.name;
        icon = fZone.icon;
        group = fZone.group;
        avEnable = fZone.avEnable;
        audioOnly = fZone.audioOnly;
        listIndex = fZone.listIndex;
        chkBoxAVEnable = new MyCheckBoxItem(false);
        chkBoxAudioOnly = new MyCheckBoxItem(false);
        fusionZoneOptions = new List<FCMOption>();
        sourceExclusion = new List<FCMSource>();
        updateCheckbox();
    }

    public FCMZone(FusionZone zn, ZoneProps znProps)
    {
        id = zn.Id;
        name = zn.Name;
        icon = zn.IconID;
        var myValue = zn.Group;
        int parsedValue;

        if (Int32.TryParse(myValue, out parsedValue))
        {
            // zn.Group is a ID number
            var fcmZoneGroup = znProps.systemZoneGroupList.Find(zg => zg.id == parsedValue.ToString());
            group = fcmZoneGroup.name;
        }
        else
        {
            // zn.Group is a word.
            group = myValue;
        }
        listIndex = zn.ListIndex;
        excludedSources = zn.ExcludedSources;
        if (zn.AVEnable == "1")
            avEnable = CheckState.Checked;
        else
            avEnable = CheckState.Unchecked;
        if (zn.AudioOnly == "1")
            audioOnly = CheckState.Checked;
        else
            audioOnly = CheckState.Unchecked;
        chkBoxAVEnable = new MyCheckBoxItem(false);
        chkBoxAudioOnly = new MyCheckBoxItem(false);
        fusionZoneOptions = new List<FCMOption>();
        sourceExclusion = new List<FCMSource>();
        updateCheckbox();
    }
}

How it builds from a FCMZone to XML [The FusionSystem that contains all the lists]

public class FusionSystem
{
    // All list items that will be serialised to the fusion system.
    public List<FusionOption> optionList = new List<FusionOption>();
    public List<FusionSource> sourceList = new List<FusionSource>();
    public List<FusionZoneGroup> zoneGroupList = new List<FusionZoneGroup>();
    public List<FusionZone> zoneList = new List<FusionZone>();
    public List<FusionUser> userList = new List<FusionUser>();
    public List<FusionSipGroup> sipGroupList;
    public List<FusionSip> sipList;
    public List<FusionCamGroup> camGroupList;
    public List<FusionCam> camList;
    public FusionLightingProperties lightingProperties;
    public FusionWindowProperties windowProperties;
}

XML Snippet [ The XML, I've left the Bathroom Zone and removed the others for ease]

<?xml version="1.0" encoding="utf-8"?>
<FusionSystem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <optionList>
  <sourceList>
  </sourceList>
  <zoneGroupList>
    <FusionZoneGroup>
      <Id>1</Id>
      <Name>Fusion</Name>
      <Icon>24</Icon>
      <Ident />
      <ListIndex>0</ListIndex>
    </FusionZoneGroup>
    <FusionZoneGroup>
      <Id>2</Id>
      <Name>Demo</Name>
      <Icon>23</Icon>
      <Ident>Demo</Ident>
      <ListIndex>1</ListIndex>
    </FusionZoneGroup>
  </zoneGroupList>
  <zoneList>
    <FusionZone>
    </FusionZone>
    <FusionZone>
    </FusionZone>
    <FusionZone>
    </FusionZone>
    <FusionZone>
    </FusionZone>
    <FusionZone>
      <ZoneOptionList>
        <FusionZoneOption>
          <Id>1</Id>
          <Name>Lighting</Name>
          <Icon>1</Icon>
          <Enabled>0</Enabled>
          <ListIndex>0</ListIndex>
        </FusionZoneOption>
        <FusionZoneOption>
          <Id>2</Id>
          <Name>Window Treatments</Name>
          <Icon>2</Icon>
          <Enabled>0</Enabled>
          <ListIndex>0</ListIndex>
        </FusionZoneOption>
        <FusionZoneOption>
          <Id>3</Id>
          <Name>Climate</Name>
          <Icon>3</Icon>
          <Enabled>0</Enabled>
          <ListIndex>0</ListIndex>
        </FusionZoneOption>
        <FusionZoneOption>
          <Id>4</Id>
          <Name>Display</Name>
          <Icon>4</Icon>
          <Enabled>0</Enabled>
          <ListIndex>0</ListIndex>
        </FusionZoneOption>
      </ZoneOptionList>
      <LightingFixtureList />
      <LightingSceneList />
      <WindowFixtureList />
      <LightingKeypadIDs />
      <WindowKeypadIDs />
      <Id>5</Id>
      <Name>Bathroom</Name>
      <IconName>Bathroom(M)</IconName>
      <IconID>8</IconID>
      <ListIndex>2</ListIndex>
      <Group>1</Group>
      <AVEnable>0</AVEnable>
      <AudioOnly>1</AudioOnly>
      <ExcludedSources>All Available</ExcludedSources>
    </FusionZone>

My guess is using Globalization and Resources, but I have never used either before and if I could be directed to a relevant guide I'd be grateful.

Sorry if I added a bit too much info.

XmlSerializer

private void OpenFile(string filePath)
    {
        projProps.fileName = filePath;
        try
        {
            bool proceed = CheckLegacyTypes();
            if (!proceed)
            {
                return;
            }

            XmlSerializer xmlData = new XmlSerializer(typeof(FusionSystem));

            using (StreamReader streamRead = new StreamReader(projProps.fileName))
            {
                fusionSystem = (FusionSystem) xmlData.Deserialize(streamRead);
            }

            openFusionSystem();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

.

1 Answers1

0

My guess is using Globalization and Resources, but I have never used either before and if I could be directed to a relevant guide I'd be grateful.

Well... yes and no. If you're trying to dynamically change your UI at runtime, this answer provides a couple of solutions; this is where you'd use Globalization (for the UI thread's culture) and Resources (for storing the UI strings).

You can't write to the RESX file at runtime, though, so your only real solution is loading and unloading an XML file whenever the language dropdown changes values. For this, you're probably just going to want to use Serialization.

Community
  • 1
  • 1
Carter Wickstrom
  • 176
  • 2
  • 10
  • Ah good point, so when the user clicked French. It would have to reload the fr version of the XML and all the values would have to be inputted again from afresh ? – Andy Pillay Jun 09 '16 at 16:17
  • No; I was thinking that you'd be saving the changes to the XML file, and they would persist between language changes. – Carter Wickstrom Jun 09 '16 at 17:05
  • So with Serialization is there a setting I have to set for languages or do I have to create its own tag separately for english and french so something like ** – Andy Pillay Jun 10 '16 at 09:41
  • No settings like that, no. Your solution isn't a bad one. Other options could include something like this: `` or having one XML file per culture and put the language name in the file name. – Carter Wickstrom Jun 10 '16 at 15:47
  • To do it in the example you shown. How do you get the culture tag to display when serializing a Class? XmlAttribute? Also thank so much for all your help so far Carter. Honestly. – Andy Pillay Jun 10 '16 at 16:30
  • I'm not sure what you mean by getting the tag to display. – Carter Wickstrom Jun 10 '16 at 17:07
  • Sorry by tag I meant off the `culture="fr-FR"` attributes. – Andy Pillay Jun 13 '16 at 08:09