-1

I am new to c# and this project is for the purpose of learning only. I trying to make a program where the user enters some information inn a command line, and then the result is displayed in a forms window. How can I make that work?

Here is my failed attempt:

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);
        }
mosern
  • 11
  • 5

2 Answers2

1

You generally can't mix console and winforms applications together. It's technically possible, but the framework is generally designed to treat these as separate application types: Show Console in Windows Application?

If your goal is to allow data input from commandline prior to displaying the form, consider just using command line parameters, which are much easier to support in a winforms application: How do I pass command-line arguments to a WinForms application?

Community
  • 1
  • 1
  • That does not answer the question at all. – SLaks Aug 24 '14 at 17:05
  • @SLaks. What I said is correct. There is no console in a winforms application by default, and you can't exactly create display a form in a console application very easily. Can you please elaborate? – Nicholas Hornback Aug 24 '14 at 17:10
  • 1
    You can easily create a form in a console application. A console application is just an EXE that also opens a console. – SLaks Aug 24 '14 at 17:11
  • Well, okay, it's possible to go the other way (winforms from console application), but that leads to other problems. See Tergiver's answer here: http://stackoverflow.com/questions/277771/how-to-run-a-winform-from-console-application. I think that the conceptual problem is best solved with commandline parameters, depending on the amount of data. – Nicholas Hornback Aug 24 '14 at 17:15
  • It is quite possible to go either way. No one promised that you won't have to write any code, though. The easiest way is to create a WinForms application, and then use `AllocConsole` and `FreeConsole` to create and destroy the console window, respectively, as desired. And I don't know what you mean about command line parameters. Those don't really have anything at all to do with the console window. – Cody Gray - on strike Aug 24 '14 at 17:16
1

Unfortunately, I can't really understand that error message but Application.SetCompatibleTextRenderingDefault(false); must be called before the window is created (e.g. before form constructor is called).

Kurubaran
  • 8,696
  • 5
  • 43
  • 65
Martin Cerman
  • 333
  • 2
  • 11