collent
collent

Reputation: 13

storing and using special symbols in a C# winforms application

I am currently working on a application which will give flash card like questions which I have stored on the system. I was wondering what would be my best option to store lists of information. The formatting I am currently using is where the first line is the question, next line is the correct answer and all subsequent lines are distractors, this being accomplished through file.read and write. The problem I have with this is that I cant use special symbols, like say I wanted to write NH4+, I would have to write it as NH4+.

So far I have looked into SQL lite and JSON, but neither seam to work, any advice is appreciated.

I cant seam to get txt files to properly return Unicode values and they feel almost clunky to use. with that I am unsure how to get Winforms to read and properly display Unicode either

Upvotes: 1

Views: 135

Answers (1)

Flydog57
Flydog57

Reputation: 7111

  1. Create a new Windows Forms app.
  2. Add a new item (JSON) named "text.json"
    • Set it's "Copy to Output Directory" property to "Copy Always"
  3. Add the following to the file (typing this is the hardest part)
{
  "fractions": [
    "2½",
    "4¾"
  ],
  "chemistry": [
    "NH₄",
    "H₂SO₄",
    "³He"
  ],
  "cards": [
    "A♠",
    "2♥",
    "10♢",
    "J♧"
  ]
}
  1. Add two multiline text boxes to the form (they'll be named textBox1 and textBox2 by default)
  2. Double-click the Caption (title area) of the form in the designer, this will bring up a wired up Load handler for the form
  3. Paste this in as the body of the Load handler:
private void Form1_Load(object sender, EventArgs e)
{
    var json = File.ReadAllText("text.json");
    textBox1.Text = json;
    var data = JsonConvert.DeserializeObject<Dictionary<string, string[]>>(json);
    var buffer = new StringBuilder();
    foreach (var topLevel in data)
    {
        buffer.AppendLine(topLevel.Key);
        foreach (var item in topLevel.Value)
        {
            buffer.AppendLine($"    {item}");
        }
    }
    textBox2.Text = buffer.ToString();
}
  1. Run the program. You will get this in textBox1:
{
  "fractions": [
    "2½",
    "4¾"
  ],
  "chemistry": [
    "NH₄",
    "H₂SO₄",
    "³He"
  ],
  "cards": [
    "A♠",
    "2♥",
    "10♢",
    "J♧"
  ]
}
  1. And this in textBox2
fractions
    2½
    4¾
chemistry
    NH₄
    H₂SO₄
    ³He
cards
    A♠
    2♥
    10♢
    J♧

Is your issue more complicated than this? This worked the first time - I did no magic to get it to Just Work

Upvotes: 1

Related Questions