Solved

change C# console app to windows form

Posted on 2011-02-14
3
410 Views
Last Modified: 2012-05-11
not school work-
can i turn this console C# program into a program using a windows form?
if possible- any first ideas as to how to go about it?
program asks for salaries-
then sums them up with array and a for loop-
i have a more complicated program to convert- but do not understand how to have for loop and array in a gui form
thank you
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace test1
{
    class Program
    {
        static void Main(string[] args)
        {
            
            int i;
            int[] salary = new int[10];
            int sumofsalary = 0;
            
            for (i = 0;i <=4; i++)
            {
                Console.WriteLine("enter salary of Employee: {0}", i + 1);
                salary[i] = Int32.Parse(Console.ReadLine());
            }
            for (i = 0; i <= 4; i++)
            {
                sumofsalary = sumofsalary + salary[1];
            }
            Console.WriteLine("total salary given to employees is = " + sumofsalary);

Open in new window

0
Comment
Question by:davetough
  • 2
3 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 400 total points
ID: 34891775
The Console app stops at ReadLine() and waits for input...giving just one path thru the app.

In a WinForms app you'd place a TextBox on the Form for entry, and a Button to initiate the action.  You could use a Label to display the results.

When the button is pressed it generates a Click() event that the Form subscribes to.  So you'd put the "meat" of the code into the Click() handler and then update the Label with the result...
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 34891804
*You'd have to make a design-decision, supply ONE TextBox and track the number of entries with each Button click.
...or use multiple TextBoxes, one for each slot in the array.
...or use a DataGrid type control instead.
0
 
LVL 7

Assisted Solution

by:dimaj
dimaj earned 100 total points
ID: 34892052
What you'll have to do is:
1) Create a form that would prompt for the values you need to compute
2) Then, from your main, you'll make these calls:
Form myfrm = new MyForm(); // where MyForm is the name of your form from step 1
myfrm.ShowDialog();

Until you physically close the form, you app will not quit.

If you want to pass in your command line arguments to the form, make sure that your form's constructor can handle your arguments and then just call new MyForm (args);

Hope this helps.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question