Home > other >  How to change c# console code to windows forms application
How to change c# console code to windows forms application

Time:03-23

I have a console application that calculates how much a customer should get back at the stated price and what the customer has paid with. The customer gets back his exchange in different denominations and how many of each denomination it should receive. Swedish kronor is used in my code.

I want to get my console code to work with windows form application so that it works there with, but got stuck right now. Has come a bit on the road where I get a message box but want to make the windows application calculate how much I should get back. Have searched around on youtube and other similar problems but have not helped me much.

Here is my code so far in Windows application:

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

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        internal enum Valörer
        {
            Enkrona = 1,
            Tvåkrona = 2,
            Femkrona = 5,
            Tiokrona = 10,
            Tjugolapp = 20,
            Femtiolapp = 50,
            Hundralapp = 100,
            Tvåhundralapp = 200,
            Femhundralapp = 500,
        }

        public void button1_Click(object sender, EventArgs e)
        {
            string inpris;
            inpris = Console.ReadLine();
            int pris = int.Parse(textBox1.Text);

            //Skrivs vad kunden har betalat(t.ex 500kr) och sen kommer vad kunden ska ha tillbaka i växel  kund betalt = customer paid, växel tillbaka = change back

            Console.WriteLine("Kund betalt: ");
            inpris = Console.ReadLine();
            double betalt = double.Parse(textBox2.Text);

            var tebaxs = betalt - pris;

            var valorer = ((Valörer[])Enum.GetValues(typeof(Valörer))).OrderByDescending(x => x);
            Console.WriteLine("Växel tillbaka: ");
            foreach (Valörer changes in valorer)

                
            {
                var change = (int)changes;
                var numberOfReturns = 0;

                while ((change % tebaxs == change && tebaxs != 0) || (change == tebaxs))
                {
                    numberOfReturns  ;
                    tebaxs = tebaxs - change;
                }

                if (numberOfReturns != 0)
                    Console.WriteLine($"{numberOfReturns} {changes.ToString()}");
            }

            //Console.ReadLine();


            MessageBox.Show(" Växel tillbaka : "   betalt   pris);
        }
    }
}

CodePudding user response:

Add a new windows form project to your solution and then you should be able to communicate with your console application with using statements in each project.

CodePudding user response:

I changed some console syntax to windows forms syntax. It should work. Replace Console.WriteLine Text into the Label: The Code should work but give me some feedback if you have problems.

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

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        internal enum Valörer
        {
            Enkrona = 1,
            Tvåkrona = 2,
            Femkrona = 5,
            Tiokrona = 10,
            Tjugolapp = 20,
            Femtiolapp = 50,
            Hundralapp = 100,
            Tvåhundralapp = 200,
            Femhundralapp = 500,
        }

        public void button1_Click(object sender, EventArgs e)
        {
            // Place a Textbox on the Form. For this Example i use textboxInpris
            string inpris;
            inpris = textboxInpris.Text;

            int pris = int.Parse(textBox1.Text);

            //Skrivs vad kunden har betalat(t.ex 500kr) och sen kommer vad kunden ska ha tillbaka i växel  kund betalt = customer paid, växel tillbaka = change back

            // Place a Label above the textbox with the Text "Kund betalt: " (label1)
            // Console.WriteLine("Kund betalt: ");
            inpris = textboxInpris.Text;
            double betalt = double.Parse(textBox2.Text);

            double tebaxs = betalt - pris;

            var valorer = ((Valörer[])Enum.GetValues(typeof(Valörer))).OrderByDescending(x => x);
           
            // Again place a Label with the text "Växel tillbaka: " (label2)
            // Console.WriteLine("Växel tillbaka: ");
            foreach (Valörer changes in valorer)

                
            {
                int change = (int)changes;
                int numberOfReturns = 0;

                while ((change % tebaxs == change && tebaxs != 0) || (change == tebaxs))
                {
                    numberOfReturns  ;
                    tebaxs = tebaxs - change;
                }

                if (numberOfReturns != 0)
                    MessageBox.Show($"{numberOfReturns} {changes.ToString()}");
            }

            MessageBox.Show(" Växel tillbaka : "   betalt   pris);
        }
    }
}
  • Related