简体   繁体   English

如何显示文本然后让用户编辑它? (控制台应用)

[英]How can I display a text and then let the user edit it? (console application)

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 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 C#文件

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. 问题是当光标移动时,前一个字母消失了。

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. 从下面的代码替换您的功能(SelectMenuOption)。 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. 希望它会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何让用户仅复制TextEdit的文本 - how can i let the user to only copy the TextEdit's text 我如何编辑app.config文件以使应用程序从同一文件夹附加数据库 - how can i edit the app.config file to let the application attach the database from the same folder 我怎么能放一个规则,只允许您在C#的控制台应用程序中输入数字 - how can i put a rule that will only let you enter a number in a console application on C# 如何在 C# Windows 窗体应用程序中修复窗体大小而不让用户更改其大小? - How can I fix the form size in a C# Windows Forms application and not to let user change its size? 如何在 WPF 应用程序中显示 XML 文件并让用户编辑/保存它? - How do you display an XML file in a WPF App and let the user edit/save it? 我如何让用户为网站输入德语/法语文本 - How do i let the user input text in German / French for a website 如何在C#控制台应用程序中显示上标文本? - How to display superscript text in C# Console application? 如何在文本文件中显示字符串? (C#控制台应用程序) - How to display a string within a text file? (C# Console Application) 如何在用户输入后将文本文件显示到控制台? 在C#中 - How to display a text file into console after user input? in C# 如何通过文本文件在控制台上显示学生的姓名和平均成绩? - How can I display the student's name and his average grade on the console from a text file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM