【问题标题】:Move input cursor in console to next line将控制台中的输入光标移动到下一行
【发布时间】:2013-07-25 19:23:48
【问题描述】:

这是一个有两个线程的程序;一个用于输出,一个用于输入。 (其中 _ 是控制台光标)

Please enter a number:
12_

当您输入 12 时,会生成输出,清除当前行并覆盖它,所以会发生这种情况:

Please enter a number:
Output
_

我怎样才能让它把你仍在输入的 12 移到下一行,这样你就不必重新输入了?

提前致谢。

代码:

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

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            Program prog = new Program();
            Thread t1 = new Thread(prog.getInput);
            t1.Start();
            prog.otherThread();
        }

        public void otherThread()
        {
            while (true)
            {
                System.Threading.Thread.Sleep(3000);
                ClearCurrentConsoleLine();
                Console.WriteLine("Output");
            }
        }

        public void getInput()
        {
            while (true)
            {
                string msg;
                msg = Console.ReadLine();
            }
        }

        public static void ClearCurrentConsoleLine()
        {
            int currentLineCursor = Console.CursorTop;
            Console.SetCursorPosition(0, Console.CursorTop);
            for (int i = 0; i < Console.WindowWidth; i++)
            {
                Console.Write(" ");
            }
            Console.SetCursorPosition(0, currentLineCursor);
        }
    }

如您所见,当您输入“Hello”但不输入时,3 秒后它将被“Output”覆盖。我想在覆盖之前将“Hello”和输入移到第二行。

【问题讨论】:

标签: c# multithreading input console output


【解决方案1】:

我刚刚发现这个article (web archive) 讨论了光标位置和修改。我发现它非常简单。

它的核心是:

      int left = Console.CursorLeft;
      int top = Console.CursorTop;
      Console.SetCursorPosition(15, 20);

【讨论】:

    【解决方案2】:

    如果我理解正确,这应该可以。

    1. 将字符串 msg 设为全局。在所有函数之外声明它。
    2. 现在只需在清除上一行后在下一行打印它..

      类程序 {
      字符串味精;

      static void Main(string[] args)
      {
          Program prog = new Program();
          Thread t1 = new Thread(prog.getInput);
          t1.Start();
          prog.otherThread();
      }
      
      public void otherThread()
      {
          while (true)
          {
              System.Threading.Thread.Sleep(3000);
              ClearCurrentConsoleLine();
              Console.WriteLine("Output");
          }
      }
      
      public void getInput()
      {
          while (true)
          {
      
              msg = Console.ReadLine();
          }
      }
      
      public static void ClearCurrentConsoleLine()
      {
          int currentLineCursor = Console.CursorTop;
          Console.SetCursorPosition(0, Console.CursorTop);
          for (int i = 0; i < Console.WindowWidth; i++)
          {
              Console.Write(" ");
          }
      
          Console.SetCursorPosition( 0, currentLineCursor + 1 );
          Console.Write(msg);
          Console.SetCursorPosition(0, currentLineCursor);
      
      }
      

      }

    【讨论】:

    • 但你不明白。没有输入输入,在打印输出时它保持可编辑....
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-02
    • 2021-04-13
    • 1970-01-01
    • 2016-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多