2

I would like to call the database for a text and display it. Then let the user edit the displayed text as if she/he opened it in a text editor. Of course at the end I would like to save the edited version back to the database.

Unfortunately all of my ideas were dead ends, so eventually I decided to ask you, professionals and more experienced programmers, to help me as I am really just a beginner.

Here is my starting point:

Database.xml

<?xml version="1.0" encoding="utf-8" ?> 
<Database>

  <Texts>

      <Text Id="00">Default text 00</Text>
      <Text Id="01">Default text 01</Text>
      <Text Id="02">Default text 02</Text>

  </Texts>

</Database>

C# file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace EditableText
{
    class Program
    {

        static void Main(string[] args)
        {

            SelectMenuOption();

            Console.WriteLine("Thanks for stopping by!");
            Console.ReadLine();

        }

        private static void SelectMenuOption()
        {
            bool tf = true;
            while (tf)
            {
                Menu();
                string userInput = Console.ReadLine().ToLower();
                Console.WriteLine("--------------------------------------------------");

                if (userInput == "text_00")
                {
                    Console.Write(CallDatabase("00"));
                    tf = false;
                }
                else if (userInput == "text_01")
                {
                    Console.Write(CallDatabase("01"));
                    tf = false;
                }
                else if (userInput == "text_02")
                {
                    Console.Write(CallDatabase("02"));
                    tf = false;
                }
                else if (userInput == "quit")
                {
                    tf = false;
                }
                else
                {
                    Console.WriteLine("Error");
                }

                Console.ReadLine();
                Console.Clear();
            }

        }

        private static void Menu()
        {

            Console.WriteLine("You may play or write quit to say bye.");
            Console.WriteLine("");

            string[] texts = new string[]
            {
                "Text_00",
                "Text_01",
                "Text_02"
            };

            Console.WriteLine("Choose: ");

            foreach (var text in texts)
            {
                Console.Write("   " + text);
            }

            Console.WriteLine("");

        }

        private static string CallDatabase(string idNumber)
        {

            XElement database = XElement.Load(@"Database.xml");
            string dText = database.Element("Texts").Elements("Text").Where(x => x.Attribute("Id").Value == idNumber).FirstOrDefault().Value.ToString();
            return dText;

        }

    }
}

Edited
I've tried this:

    string text = "It's a text.";

    char[] textC = text.ToCharArray();
    foreach (var textL in textC)
    {
        Console.Write(textL);
    }

    int n = 0;

    while (0 <= textC.Length - n)
    {
        if (Console.ReadKey().Key == ConsoleKey.LeftArrow)
        {
            Console.SetCursorPosition(textC.Length - n, 0);
            n++;
        }
    }

And the problem was that when the cursor moved, the previous letter disappeared.

  • 1
    Look at this question [How to set default input value in .Net Console App](http://stackoverflow.com/questions/1655318/how-to-set-default-input-value-in-net-console-app) – dePatinkin Jul 02 '16 at 18:52
  • I've tried something like this, but when the cursor moved, the previous letter disappeared. – Zoltán Coly Jul 02 '16 at 19:43

1 Answers1

0
private static void SelectMenuOption()
    {
        bool tf = true;
        while (tf)
        {
            Menu();
            string userInput = Console.ReadLine().ToLower();
            Console.WriteLine("--------------------------------------------------");

            string id = string.Empty;

            if (userInput == "text_00")
            {

                Console.Write(CallDatabase("00"));
                id = "00";
                tf = false;
            }
            else if (userInput == "text_01")
            {
                Console.Write(CallDatabase("01"));
                id = "01";
                tf = false;
            }
            else if (userInput == "text_02")
            {
                Console.Write(CallDatabase("02"));
                id = "02";
                tf = false;
            }
            else if (userInput == "quit")
            {
                tf = false;
            }
            else
            {
                Console.WriteLine("Error");
            }

            var chage  = Console.ReadLine();

            Replace(id, chage);

            Console.Clear();
        }

    }

    private static void Replace(string id, string chage)
    {

        XmlDocument xml = new XmlDocument();
        xml.Load(@"Database.xml");

        XmlNodeList elements = xml.SelectNodes("//Text");



        foreach (XmlNode element in elements)
        {
            if (element.Attributes["Id"].Value == id)
            {
                element.InnerText = chage;

                xml.Save("Database.xml");
            }
        }

    }

Replace your functions(SelectMenuOption) from the code below. and Add one more method Replace in it as below. It will work. You can improve the code for sure, I just provided a solution. Hope it will help.

Saket Choubey
  • 916
  • 6
  • 11
  • Thank you! It's great, but I would be interested in a solution where the user can move the cursor to the displayed text like when the Console.ReadLine() method is used and the input comes from the user. – Zoltán Coly Jul 02 '16 at 20:00
  • That's what it does, replace method replaces the xml default value from user input. And this user input gets stored in the change variable. Did you try to run the code above. – Saket Choubey Jul 02 '16 at 20:04
  • 1
    Yes, I've tried it :) It writes the original text and the user can type something that is saved after she/he presses the enter. However, I want the user to have access to the original text directly, because if the user mistypes something and wants to correct it, I don't want him/her to retype the whole text. I would like her/him to be able to move the cursor to the mistyped letter and change it, then save it. – Zoltán Coly Jul 02 '16 at 20:11
  • Okay, Thanks for answering. Please try this help[http://stackoverflow.com/questions/7565415/edit-text-in-c-sharp-console-application]. This may help. – Saket Choubey Jul 02 '16 at 20:16