【问题标题】:How can I display a text and then let the user edit it? (console application)如何显示文本然后让用户编辑它? (控制台应用程序)
【发布时间】:2016-07-02 18:41:33
【问题描述】:

我想调用数据库获取文本并显示它。然后让用户编辑显示的文本,就像她/他在文本编辑器中打开它一样。当然最后我想将编辑后的版本保存回数据库。

不幸的是,我所有的想法都是死胡同,所以最终我决定请专业人士和更有经验的程序员来帮助我,因为我真的只是一个初学者。

这是我的出发点:

数据库.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# 文件

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;

        }

    }
}

已编辑
我试过这个:

    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++;
        }
    }

问题是当光标移动时,前一个字母消失了。

【问题讨论】:

标签: c# console-application text-editor


【解决方案1】:
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");
            }
        }

    }

从下面的代码中替换您的函数(SelectMenuOption)。并在其中添加另一种方法替换,如下所示。它会起作用的。您可以肯定地改进代码,我只是提供了一个解决方案。希望它会有所帮助。

【讨论】:

  • 谢谢!这很棒,但我会对用户可以将光标移动到显示的文本的解决方案感兴趣,例如使用 Console.ReadLine() 方法并且输入来自用户时。
  • 就是这样,replace 方法替换用户输入的xml默认值。此用户输入存储在更改变量中。你有没有尝试运行上面的代码。
  • 是的,我试过了 :) 它会写入原始文本,用户可以键入在她/他按下回车键后保存的内容。但是,我希望用户能够直接访问原始文本,因为如果用户输入错误并想要更正它,我不希望他/她重新输入整个文本。我希望她/他能够将光标移动到输入错误的字母并更改它,然后保存。
  • 好的,感谢您的回答。请试试这个帮助[stackoverflow.com/questions/7565415/….这可能会有所帮助。
猜你喜欢
  • 1970-01-01
  • 2011-11-25
  • 2021-12-25
  • 1970-01-01
  • 1970-01-01
  • 2010-12-12
  • 1970-01-01
  • 1970-01-01
  • 2019-09-10
相关资源
最近更新 更多