【问题标题】:C# Open winform from commandline [closed]C#从命令行打开winform [关闭]
【发布时间】:2014-08-24 16:41:09
【问题描述】:

我是 c# 新手,这个项目仅用于学习目的。我试图制作一个程序,用户在命令行中输入一些信息,然后结果显示在表单窗口中。我怎样才能使它工作?

这是我失败的尝试:

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

namespace Øving1
{
    class Program
    {
       static int Length;
       static int Width;

        [STAThread]
        static void Main(string[] args)
        {
            GUI test = new GUI();

            Console.WriteLine("Dette Programet regner ut areal og omkrets av et rektangel, angi en lengde og brede(eks. 10 enter 10)");
            Length = Convert.ToInt32(Console.ReadLine());
            Width = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("AREAL: " + Area(Length, Width) + " OMKRETS: " + Circumference(Length, Width));


            test.richTextBox1.Clear();
            test.richTextBox1.AppendText("AREAL: " + Area(Length, Width) + " OMKRETS: " + Circumference(Length, Width));

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(test);


        }

        public static int Area(int Length, int Width)
        {
            return Length * Width;
        }

        public static int Circumference(int Length, int Width)
        {
            return (Length * 2) + (Width * 2);
        }

【问题讨论】:

标签: c# winforms forms command-line


【解决方案1】:

不幸的是,我无法真正理解该错误消息,但必须在创建窗口之前调用 Application.SetCompatibleTextRenderingDefault(false);(例如,在调用表单构造函数之前)。

【讨论】:

  • 这是正确答案。
【解决方案2】:

您通常不能将控制台和 winforms 应用程序混合在一起。这在技术上是可行的,但该框架通常旨在将这些视为单独的应用程序类型:Show Console in Windows Application?

如果您的目标是在显示表单之前允许从命令行输入数据,请考虑仅使用命令行参数,这在 winforms 应用程序中更容易支持:How do I pass command-line arguments to a WinForms application?

【讨论】:

  • 这根本不能回答问题。
  • @SLaks。我说的是对的。默认情况下,winforms 应用程序中没有控制台,您不能非常容易地在控制台应用程序中准确地创建显示表单。能详细点吗?
  • 您可以在控制台应用程序中轻松创建表单。控制台应用程序只是一个可以打开控制台的 EXE。
  • 嗯,好吧,可以采用其他方式(来自控制台应用程序的winforms),但这会导致其他问题。在此处查看 Tergiver 的答案:stackoverflow.com/questions/277771/…。我认为概念问题最好用命令行参数来解决,具体取决于数据量。
  • 两种方式都可以。但是,没有人承诺您不必编写任何代码。最简单的方法是创建一个 WinForms 应用程序,然后根据需要使用AllocConsoleFreeConsole 分别创建和销毁控制台窗口。而且我不知道您对命令行参数的意思。这些与控制台窗口根本没有任何关系。
猜你喜欢
  • 2021-09-07
  • 1970-01-01
  • 1970-01-01
  • 2015-01-26
  • 2012-12-30
  • 1970-01-01
  • 2012-09-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多