locked
How would I convert this Console Code to a GUI program? RRS feed

  • Question

  • Hello all, I have a good working console program and I do not ever mess with GUI's. The console code adds the total of interior and exterior murals and calculates the price. Here is my console code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MarshallsRevenue                                  
    {                                                          
        class Program                   
        {
            static void Main(string[] args)
            {
                int InteriorMurals = 0;
                int ExteriorMurals = 0;
                double Interior_Cost = 500.00;
                double Exterior_Cost = 750.00;
                double TotalCost = 0;
    
                Console.WriteLine("How many Interior Murals do you want?");
                InteriorMurals = Convert.ToInt32( Console.ReadLine());
    
    
                Console.WriteLine("How many Exterior Murals do you want?");
                ExteriorMurals = Convert.ToInt32(Console.ReadLine());
    
                TotalCost = (InteriorMurals * Interior_Cost) + (ExteriorMurals * Exterior_Cost);
    
    
                Console.WriteLine("Total Cost for Interior Murals is {0}  ", (InteriorMurals * Interior_Cost));
                Console.WriteLine("Total Cost for Exterior Murals is {0}  ", (Exterior_Cost* ExteriorMurals));
                Console.WriteLine("Total price for all Murals is {0}  ", TotalCost);
                Console.WriteLine("Please recieve the amount....!!! ");
                Console.ReadKey();
            }
        }
    }

    I am lost and need examples of how to use this with a GUI. Thank you all for you're valuable time, and I look forward to hearing from you.

    Friday, December 6, 2013 2:56 AM

Answers

  • Greetings DJ.

    There isn't a standard formulaic way to translate a console app to a GUI app. Generally speaking, you need to spend some time designing (at least in your head, if not on paper) how the GUI should look and feel, then set up a form with the right controls, then port your existing code over so that it gets called in events triggered by the form's controls.

    In the case of very large programs it can be a very complicated process, and it's often best to just start from scratch.

    In your case, I would suggest something like the following.

    Create a form with two textboxes and a button. Put labels next to each of the textboxes, like "Number of Interior Murals" and "Number of Exterior Murals". Make the button's text something like "Calculate".

    In the button's click event, put your code for calculating the cost, except that the numbers of murals will come from the textboxes' Text properties instead of from Console.ReadLine. Also in the click event, after the calculations, construct a string with the results (the bits currently in the Console.WriteLine calls near the end) and display it using MessageBox.Show.

    There are a few ways to be a bit more sophisticated, but this is a simple design to get you started.
    • Marked as answer by Eason_H Friday, December 13, 2013 1:30 AM
    Friday, December 6, 2013 5:06 AM
  • Hi ,

    Use Windows forms to show the details in the UI.

    Create a Windows forms application 

    


    Add logic in the button_click event as below-

     private void button1_Click(object sender, EventArgs e)
            {
                int InteriorMurals = 0;
                int ExteriorMurals = 0;
                double Interior_Cost = 500.00;
                double Exterior_Cost = 750.00;
                double TotalCost = 0;
    
                int.TryParse(textBox1.Text, out InteriorMurals);
                int.TryParse(textBox2.Text, out ExteriorMurals);
                TotalCost = (InteriorMurals * Interior_Cost) + (ExteriorMurals * Exterior_Cost);
                TotalLabel.Text = TotalCost.ToString();
                Total_Int_MuralsLabel.Text = (InteriorMurals * Interior_Cost).ToString();
                Total_Ext_MuralsLabel.Text = (ExteriorMurals * Exterior_Cost).ToString();
            }

    Thanks.


    -Arun

    • Proposed as answer by Eason_H Friday, December 6, 2013 6:42 AM
    • Marked as answer by Eason_H Friday, December 13, 2013 1:30 AM
    Friday, December 6, 2013 5:34 AM

All replies

  • Greetings DJ.

    There isn't a standard formulaic way to translate a console app to a GUI app. Generally speaking, you need to spend some time designing (at least in your head, if not on paper) how the GUI should look and feel, then set up a form with the right controls, then port your existing code over so that it gets called in events triggered by the form's controls.

    In the case of very large programs it can be a very complicated process, and it's often best to just start from scratch.

    In your case, I would suggest something like the following.

    Create a form with two textboxes and a button. Put labels next to each of the textboxes, like "Number of Interior Murals" and "Number of Exterior Murals". Make the button's text something like "Calculate".

    In the button's click event, put your code for calculating the cost, except that the numbers of murals will come from the textboxes' Text properties instead of from Console.ReadLine. Also in the click event, after the calculations, construct a string with the results (the bits currently in the Console.WriteLine calls near the end) and display it using MessageBox.Show.

    There are a few ways to be a bit more sophisticated, but this is a simple design to get you started.
    • Marked as answer by Eason_H Friday, December 13, 2013 1:30 AM
    Friday, December 6, 2013 5:06 AM
  • Hey, DJ! It's Justin from class. I can help you out. What did you need help with in the GUI?

    Friday, December 6, 2013 5:09 AM
  • Hi ,

    Use Windows forms to show the details in the UI.

    Create a Windows forms application 

    


    Add logic in the button_click event as below-

     private void button1_Click(object sender, EventArgs e)
            {
                int InteriorMurals = 0;
                int ExteriorMurals = 0;
                double Interior_Cost = 500.00;
                double Exterior_Cost = 750.00;
                double TotalCost = 0;
    
                int.TryParse(textBox1.Text, out InteriorMurals);
                int.TryParse(textBox2.Text, out ExteriorMurals);
                TotalCost = (InteriorMurals * Interior_Cost) + (ExteriorMurals * Exterior_Cost);
                TotalLabel.Text = TotalCost.ToString();
                Total_Int_MuralsLabel.Text = (InteriorMurals * Interior_Cost).ToString();
                Total_Ext_MuralsLabel.Text = (ExteriorMurals * Exterior_Cost).ToString();
            }

    Thanks.


    -Arun

    • Proposed as answer by Eason_H Friday, December 6, 2013 6:42 AM
    • Marked as answer by Eason_H Friday, December 13, 2013 1:30 AM
    Friday, December 6, 2013 5:34 AM
  • Greetings Arun.

    When someone asks a question on these forums that is obviously about a homework assignment, we are not supposed to write the code for them. We can give them hints and point them in the right direction, and if they write some code and get stuck we can help them with fixes, but really, they need to do as much as possible of the work themselves.
    Friday, December 6, 2013 6:03 AM
  • Hi DJ Maier,

    Arun's code is perfect, you can try it.

    But I think you must know more things about the code. Here are so many similar question that you can refer to.

    http://stackoverflow.com/questions/144701/how-do-i-convert-a-net-console-application-to-a-winforms-or-wpf-application

    http://stackoverflow.com/questions/11077081/how-to-change-a-console-application-to-a-windows-form-application 


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, December 6, 2013 6:46 AM
  • I needed help with the button to get the math working. I'm more better at coding the consoles rather than the GUI's AND vice versa. I really hate having to convert code from console to gui or from gui to console. Either way, I'm glad this class is over after this wednsday. Thanks for the reply Justin, and good luck on the final.
    Friday, December 6, 2013 5:50 PM
  • Thanks a million brother
    Friday, December 6, 2013 6:16 PM