Porting console based ui to a GUI one?

As most of you experienced, developing a console app is as easy as:

void mainloop(){
    while (1){
        giveInstructions();
        getInput();
        if (!process()) break;
        printOutput();
    }
}

int main(){
    mainloop();
    return 0;
}

However, in GUI it becomes an issue.

We can still giveInstructions(), process(), and printOutput(), but getInput() wouldn't work because it relies on an event, usually button click or key down.

How can I port a console app to a gui app with minimum code changes? (preferably do not change the main method, and as little change to the mainloop function as possible)

Note: I'm not too comfortable with threading yet.

Answers


Since there is no specific language given, I will show an example in C# where you would be able to use the same code as the console app with a simple GUI.

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //using form-editor, double-click buttons or use the following
            btnInput.Click += new EventHandler(btnInput_Click);
            btnContinue.Click += new EventHandler(btnContinue_Click);
            giveInstructions();
        }

        private void giveInstructions()
        {
            txtInfo.Text = "";
            txtInput.Text = "";
            //display instructions to multi-line textbox
        }

        private void btnInput_Click(object sender, EventArgs e)
        {
            //or you can just add another button for exit.
            if (txtInput.Text == "expected value for exit")
            {
                Application.Exit();
            }
            else
            {
                getInput();
            }
        }

        private void getInput()
        {
            string strInput = txtInput.Text;
            //do stuff

            printOutput();
        }

        private void printOutput()
        {
            //display output to multi-line textbox
        }

        private void btnContinue_Click(object sender, EventArgs e)
        {
            giveInstructions();
        }
    }

Need Your Help

SqlAlchemy - Filtering by Relationship Attribute

filter sqlalchemy foreign-key-relationship

I haven't much experience with SQLAlchemy and an problem, which I can't solve but searching an trying a lot of code.

Integrating ROR community engine to my existing app

ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 rubygems

Am trying to integrate Community Engine to my application.But it is showing dependency error.I tried to correct all the dependencies,but it is not integrating.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.