从命令行打开winform

本文关键字:winform 命令行 | 更新日期: 2023-09-27 18:19:24

我是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);
        }

从命令行打开winform

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

您通常不能将控制台和winforms应用程序混合在一起。这在技术上是可能的,但框架通常被设计成将这些作为独立的应用程序类型:

如果您的目标是允许在显示表单之前从命令行输入数据,请考虑只使用命令行参数,这在winforms应用程序中更容易支持: