8 Replies - 6543 Views - Last Post: 07 February 2012 - 08:08 PM Rate Topic: -----

#1 babbu25  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 68
  • Joined: 26-November 11

how to call a windows form from a console application

Posted 07 February 2012 - 01:07 PM

Hi friends .

How to call a windows form from an console..I have following code that generates my Rectangle area and circle area and also decides which ones radius is big. .The rectangle and circle objects to be drawn should be passed to the windows form’s constructor.

Some people advised me to use APplication.run() method.How does one can call an windows form using this method.I am a bit confused .Can some one clear it up so that i can work along ..

I am ready to post my code if necessary.Still i thought my question is clear i didnt put my code.

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: how to call a windows form from a console application

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: how to call a windows form from a console application

Posted 07 February 2012 - 02:04 PM

The Application.Run method has three ways of executing. The most popular way is to pass a Form(which is how WinForm applications are started).

Application.Run(new SomeForm());

It would be nice if you posted your code.
Was This Post Helpful? 0
  • +
  • -

#3 babbu25  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 68
  • Joined: 26-November 11

Re: how to call a windows form from a console application

Posted 07 February 2012 - 02:08 PM

ok.. here is my console class...I dont know how to call the windows form...

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

namespace Lab3
{
   public class Shapes
    {
        
        static Shapes()
        {
            Console.WriteLine("Drawing Shapes in Windows Forms");
        }

        //-	a static method CompareArea( ) with two parameters (an object of Circle and an object of Rectangle)
        //passed by reference to compare their corresponding areas and output which one is greater
        static  double Comparearea(ref circle c1, ref rectangle r1)
        {
            if (c1.Area > r1.Area)
            {
                Console.WriteLine("Circle Area is greater than Rectangle Area");
                return c1.Area;
            }
            else
            {
                Console.WriteLine("Rectangle Area is greater than circle Area");
                return r1.Area;
            }
        }
      public  static void Main(string[] args)
        {
            double r1,r2;
            double l1, b1, l2, b2;
            Console.Write("Enter the radius of circle 1 :==>");
            r1=Convert.ToDouble(Console.ReadLine());
            Console.Write("Enter the radius of circle 2 : ==>");
            r2=Convert.ToDouble(Console.ReadLine());
            circle c1=new circle(r1);            
            circle c2=new circle(r2);
            circle c3 = new circle(0);
            c3 = c2 + c1;
            Console.WriteLine("The total Area of a Circle1 and Circle2 is {0):==>", c3.Area);
            Console.WriteLine(c2==c1);
            Console.WriteLine(c2 != c1);

            // RECTANGLE 

            Console.Write("Enter the Length  of rectangle1 =>");
            l1 = Convert.ToDouble(Console.ReadLine());
            Console.Write("Enter the Breadth  of rectangle1 =>");
            b1 = Convert.ToDouble(Console.ReadLine());
            Console.Write("Enter the Length  of rectangle2 =>");
            l2 = Convert.ToDouble(Console.ReadLine());
            Console.Write("Enter the Breadth  of rectangle2 =>");
            b2 = Convert.ToDouble(Console.ReadLine());
            rectangle rec1 = new rectangle(l1, b1);
            rectangle rec2 = new rectangle(l2, b2);
            rectangle rec3 = new rectangle(0,0);
            rec3 = rec1 + rec2;
            Console.WriteLine("The total Area of a Rectangle1 and Rectangle2 is {0) :", rec3);
            Console.WriteLine(rec1 == rec2);
            Console.WriteLine(rec1 != rec2);
            
            //compare Area using CompareArea() Method

          //double greaterarea=  Comparearea(ref c3,ref rec3);
          //Console.WriteLine("The Greater area is {0} ==>", greaterarea);
          //Testgraph n = new Testgraph();

          //System.Windows.Forms.Application.Run( new Testgraph);
            
            
        }
    }

    public class circle
    {
        private double r, a;
        const double pi = 3.14;

        public double radius
        {
            get
            {
                return r;
            }
            set
            {
                r = value;
            }
        }

        public double Area
        {
            get
            {
                return a;
            }
            set
            {
                a = value;
            }
        }
        //-	non-static constructor with an argument to initialize radius and compute and initialize area.
        public circle(double r)
        {
            this.r = r;
            a = pi * r * r;
        }

        //-	an operator+( ) method to add two objects of Circle and return a resulting object
        public static circle operator+(circle c1,circle c2)
    {
            
        circle temp = new circle(0);
        temp.r = c1.r + c2.r;
        temp.a = pi * temp.r *temp. r;
        return temp;

    }

        
        //-	an operator==( ) method to compare two objects of Circle by equality and return true (if equal), or false (if not equal) 
        public static bool operator ==(circle c1, circle c2)
        {                     
              if ( c1.r == c2.r && c1.a == c2.a)         
            {
                Console.WriteLine("Both the circles are equal");
                return true;  
              
            }
            else
            {
                Console.WriteLine("Both the circles are not equal");
                return false;
            }
        }
        //-	an operator!=( ) method to compare two objects of Circle and return true (if not 
    // equal), or false (if equal)

        public static bool operator !=(circle c1, circle c2)
        {
            circle temp = new circle(0);
            if (c1.r == c2.r && c1.a == c2.a)
            {
                Console.WriteLine("Both the circles are not equal");
                return false;
            }
            else
            {
                Console.WriteLine("Both the circles are equal");
                return true ;
            }
        }
    }

    /// <summary>
    /// Defining new class ------------For RECTANGLE
    /// </summary>
    public class rectangle
    {
        double l, b, a;
        public double length
        {
            get
            {
                return l;
            }
            set
            {
                l = value;
            }
        }

        public double breath
        {
            get
            {
                return b;
            }
            set
            {
                b = value;
            }
        }


        public double Area
        {
            get
            {
                return a;
            }
            set
            {
                a= value;
            }
        } 
         public rectangle(double l,double B)/>
        {
            this.l = l;
            this.b = b;
            a = l * b;
        }
         public static rectangle operator +(rectangle r1, rectangle r2)
         {

             rectangle temp = new rectangle(0, 0);
             temp.l = r1.l + r2.l;
             temp.b = r1.b + r2.b;
             temp.a = temp.l * temp.b;
             return temp;

         }
         public static bool operator ==(rectangle r1, rectangle r2)
         {

             rectangle temp = new rectangle(0,0);
             if ( r1.a == r2.a)
             {
               return true;
             }
             else
             {
                 return false;
             }
         }
         public static bool operator !=(rectangle r1, rectangle r2)
         {

             rectangle temp = new rectangle(0,0);
             if ( r1.a == r2.a)
             {


                 return false;

             }
             else
             {

                 return true;
             }
         }
    }
}

    This is my console class which has to call a window form and on that form I have draw a rectangle and circle.





Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: how to call a windows form from a console application

Posted 07 February 2012 - 02:17 PM

You may need to add a reference to System.Windows.Forms. I don't remember if that reference is added by default to a console application(I don't think it is).

Then change this line...

//System.Windows.Forms.Application.Run( new Testgraph);



to this...

System.Windows.Forms.Application.Run(new Testgraph());



Now, assuming you have an actual form called Testgraph, that should work fine.
Was This Post Helpful? 0
  • +
  • -

#5 babbu25  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 68
  • Joined: 26-November 11

Re: how to call a windows form from a console application

Posted 07 February 2012 - 02:30 PM

yeah it works .. thanks eclipsed4utoo.Can u tell me what exactly we have done here..I just want to make clear..
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,457
  • Joined: 08-June 10

Re: how to call a windows form from a console application

Posted 07 February 2012 - 02:41 PM

Well, if you mean adding the reference...it means you told your application that it relies on an assembly named System.Windows.Forms. This particular assembly is a DLL included in the .NET Framework, so the system knows where to look for it.

This is confusing to a lot of people new to .NET development. Because there are several references added by default for you, they never know that it's necessary. But in the case of a Console Application, it does not add a reference to the Forms assembly, because most Console applications don't need to do things with forms. They leave that up to you.
Was This Post Helpful? 0
  • +
  • -

#7 babbu25  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 68
  • Joined: 26-November 11

Re: how to call a windows form from a console application

Posted 07 February 2012 - 04:12 PM

yeah..Thanks curtis for the info..

SO after all i have done ..I am not able to see my rectangle or circle in the form..following is my code

CONSOLE

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

namespace Lab3
{
   public class Shapes
    {
        
        static Shapes()
        {
            Console.WriteLine("Drawing Shapes in Windows Forms");
        }

        //-	a static method CompareArea( ) with two parameters (an object of Circle and an object of Rectangle)
        //passed by reference to compare their corresponding areas and output which one is greater
        static  double Comparearea(ref circle c1, ref rectangle r1)
        {
            if (c1.Area > r1.Area)
            {
                Console.WriteLine("Circle Area is greater than Rectangle Area");
                return c1.Area;
            }
            else
            {
                Console.WriteLine("Rectangle Area is greater than circle Area");
                return r1.Area;
            }
        }
      public  static void Main(string[] args)
        {
            double r1,r2;
            double l1, b1, l2, b2;
            Console.Write("Enter the radius of circle 1 :==>");
            r1=Convert.ToDouble(Console.ReadLine());
            Console.Write("Enter the radius of circle 2 : ==>");
            r2=Convert.ToDouble(Console.ReadLine());
            circle c1=new circle(r1);            
            circle c2=new circle(r2);
            circle c3 = new circle(0);
            c3 = c2 + c1;
            Console.WriteLine("The total Area of a Circle1 and Circle2 is {0}:==>", c3.Area);
            Console.WriteLine(c2==c1);
            Console.WriteLine(c2 != c1);

            // RECTANGLE 

            Console.Write("Enter the Length  of rectangle1 =>");
            l1 = Convert.ToDouble(Console.ReadLine());
            Console.Write("Enter the Breadth  of rectangle1 =>");
            b1 = Convert.ToDouble(Console.ReadLine());
            Console.Write("Enter the Length  of rectangle2 =>");
            l2 = Convert.ToDouble(Console.ReadLine());
            Console.Write("Enter the Breadth  of rectangle2 =>");
            b2 = Convert.ToDouble(Console.ReadLine());
            rectangle rec1 = new rectangle(l1, b1);
            rectangle rec2 = new rectangle(l2, b2);
            rectangle rec3 = new rectangle(0,0);
            rec3 = rec1 + rec2;
            Console.WriteLine("The total Area of a Rectangle1 and Rectangle2 is {0} :", rec3);
            Console.WriteLine(rec1 == rec2);
            Console.WriteLine(rec1 != rec2);
            
            //compare Area using CompareArea() Method

          double greaterarea=  Comparearea(ref c3,ref rec3);
          Console.WriteLine("The Greater area is {0} ==>", greaterarea);
         

          Application.Run(new TestDraw( c3, rec3));
            
            
        }
    }

    public class circle
    {
        private double r, a;
        const double pi = 3.14;

        public double radius
        {
            get
            {
                return r;
            }
            set
            {
                r = value;
            }
        }

        public double Area
        {
            get
            {
                return a;
            }
            set
            {
                a = value;
            }
        }
        //-	non-static constructor with an argument to initialize radius and compute and initialize area.
        public circle(double r)
        {
            this.r = r;
            a = pi * r * r;
        }

        //-	an operator+( ) method to add two objects of Circle and return a resulting object
        public static circle operator+(circle c1,circle c2)
    {
            
        circle temp = new circle(0);
        temp.r = c1.r + c2.r;
        temp.a = pi * temp.r *temp. r;
        return temp;

    }

        
        //-	an operator==( ) method to compare two objects of Circle by equality and return true (if equal), or false (if not equal) 
        public static bool operator ==(circle c1, circle c2)
        {                     
              if ( c1.r == c2.r && c1.a == c2.a)         
            {
                Console.WriteLine("Both the circles are equal");
                return true;  
              
            }
            else
            {
                Console.WriteLine("Both the circles are not equal");
                return false;
            }
        }
        //-	an operator!=( ) method to compare two objects of Circle and return true (if not 
    // equal), or false (if equal)

        public static bool operator !=(circle c1, circle c2)
        {
            circle temp = new circle(0);
            if (c1.r == c2.r && c1.a == c2.a)
            {
                Console.WriteLine("Both the circles are not equal");
                return false;
            }
            else
            {
                Console.WriteLine("Both the circles are equal");
                return true ;
            }
        }
    }

    /// <summary>
    /// Defining new class ------------For RECTANGLE
    /// </summary>
    public class rectangle
    {
        double l, b, a;
        public double length
        {
            get
            {
                return l;
            }
            set
            {
                l = value;
            }
        }

        public double breath
        {
            get
            {
                return b;
            }
            set
            {
                b = value;
            }
        }


        public double Area
        {
            get
            {
                return a;
            }
            set
            {
                a= value;
            }
        } 
         public rectangle(double l,double B)/>
        {
            this.l = l;
            this.b = b;
            a = l * b;
        }
         public static rectangle operator +(rectangle r1, rectangle r2)
         {

             rectangle temp = new rectangle(0, 0);
             temp.l = r1.l + r2.l;
             temp.b = r1.b + r2.b;
             temp.a = temp.l * temp.b;
             return temp;

         }
         public static bool operator ==(rectangle r1, rectangle r2)
         {

             rectangle temp = new rectangle(0,0);
             if ( r1.a == r2.a)
             {
               return true;
             }
             else
             {
                 return false;
             }
         }
         public static bool operator !=(rectangle r1, rectangle r2)
         {

             rectangle temp = new rectangle(0,0);
             if ( r1.a == r2.a)
             {


                 return false;

             }
             else
             {

                 return true;
             }
         }
    }
}




TESTDRAW.cs----------This is my form which contains the constructor

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Lab3
{
    public partial class TestDraw : Form
    {
        public TestDraw()
        {
            InitializeComponent();
        }

        public TestDraw(circle c3, rectangle rec3)
        {
            Graphics surface;
            surface = this.CreateGraphics();
            Pen pen1 = new Pen(Color.Blue, 1.0f);
            //surface.DrawLine(pen1, 10, 10, 100, 100);
            Rectangle reclass = new Rectangle(100, 10, Convert.ToInt32(rec3.length), Convert.ToInt32(rec3.breath));
            Rectangle reclass1 = new Rectangle(200, 10, Convert.ToInt32(c3.Area), Convert.ToInt32(c3.radius));
            surface.DrawRectangle(pen1, reclass);
            surface.DrawEllipse(pen1, reclass1);
            
        }
        private void TestDraw_Load(object sender, EventArgs e)
            
        {
            
        }
    }
}





I am not able to see any graph on my form ...I am trying to draw an Circle and rectangle..
Thanks
Was This Post Helpful? 0
  • +
  • -

#8 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1251
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: how to call a windows form from a console application

Posted 07 February 2012 - 06:59 PM

Invoke this in your overloaded constructor:

InitializeComponent();


This little method handles all of the control initialization, layout and whatnot. :) Without it your form will be empty.
Was This Post Helpful? 0
  • +
  • -

#9 babbu25  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 68
  • Joined: 26-November 11

Re: how to call a windows form from a console application

Posted 07 February 2012 - 08:08 PM

hmm.I have kept this Initialcomponent().still i cant see the Graph in my form.Is that m missing some one ...help me guys
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1