1. Run Visual Studio

2. File> New project, select ‘C# windows Form Application’ and give it a name.
I name it ‘ButtonsTest’

3. In your hard drive you fill find in the folder ‘ButtonsTest’/ButtonsTest/ here C# files

4. On the right column select Form1.cs, on the left open the toolbox, drag and drop over the Form:
– Button
– TextBox

5. On the right column in the Property Window under the ‘Design’ section (Progettazione) change (Name):
– Button -> myButton
– TextBox -> myTextBox

6. Double-click the button to create the myButton_Click event handler, and add the following code in Form1.cs


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 ButtonsTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        } // end public Form1

        private void myButton_Click(object sender, EventArgs e)
        {
            // get the string in the Text Box
            string myText;
            myText = this.myTextBox.Text;
            // show the string in a Message Box
            MessageBox.Show(myText);
        }// end myButton_Click
    }// end class Form1
}// end namespace ButtonsTest

7. Run

Check if a TextBox is empty


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 ButtonsTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        } // end public Form1

        private void myButton_Click(object sender, EventArgs e)
        {
            // get the string in the Text Box
            string myText;
            myText = this.myTextBox.Text;

            string myTextError;
            myTextError = "You have to input a string";

            // if there is no text give an error message
            if (!(string.IsNullOrEmpty(myTextBox.Text)))
            {
                MessageBox.Show(myText);
            }
            else
            {
                MessageBox.Show(myTextError);
            }

        }// end myButton_Click
    }// end class Form1
}// end namespace ButtonsTest