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.