csharp

Microsoft Visual Studio – ComboBox

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
– ComboBox

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

and for the ComboBox> Properties> Items> write:
stringone
stringtwo
stringthree

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)
        {
            if (string.IsNullOrEmpty(myComboBox.Text))
            {
                MessageBox.Show("No Item is Selected");
            }
            else
            {
                MessageBox.Show(myComboBox.Text);
            }

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

7. Run

By |C# .NET, Microsoft Visual Studio|Commenti disabilitati su Microsoft Visual Studio – ComboBox

Microsoft Visual Studio – CheckedListBox

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
– CheckedListBox

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

and for the CheckedListBox> Properties> Items> write:
stringone
stringtwo
stringthree

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)
        {
            // Determine if there are any items checked.
            if (myCheckedListBox.CheckedItems.Count != 0)
            {
                // If so, loop through all checked items and print results.
                string s = "";
                for (int x = 0; x <= myCheckedListBox.CheckedItems.Count - 1; x++)
                {
                    s = s + "Checked Item " + (x + 1).ToString() + " = " + myCheckedListBox.CheckedItems[x].ToString() + "\n";
                }
                MessageBox.Show(s);
            }
            else
            {
                MessageBox.Show("No one checked");
            }

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

7. Run, check the second and the third, click the Button, you will see in the Message Box:
Checked Item 1 = stringtwo
Checked Item 2 = stringthree

By |C# .NET, Microsoft Visual Studio|Commenti disabilitati su Microsoft Visual Studio – CheckedListBox

Microsoft Visual Studio – CheckBox

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
– CheckBox

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

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)
        {
            // if checked
            if (myCheckBox.Checked)
            {
                MessageBox.Show("You have checked it");
            }
            else
            {
                MessageBox.Show("You have not checked it");
            }

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

7. Run

By |C# .NET, Microsoft Visual Studio|Commenti disabilitati su Microsoft Visual Studio – CheckBox

Microsoft Visual Studio – TextBox – MessageBox

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

By |C# .NET, Microsoft Visual Studio|Commenti disabilitati su Microsoft Visual Studio – TextBox – MessageBox

Microsoft Visual Studio – Buttons – Labels – Simple Build Setup

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
– Label

5. Inside the Form select the Button, on the right column in the Property Window you can change the button property as Text, Font, Color, Image etc…
Under the ‘Design’ section (Progettazione) change (Name) to ‘myButton’.

6. Inside the Form select the Label, on the right> Property> Design> (Name) to ‘myLabel’

7. 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)
        {
            // change the property Text of myLabel, the name is defined in Properties> Design> (Name)
            this.myLabel.Text = "Text Changed by myButton at: " + DateTime.Now.ToLongTimeString(); // Text Changed by myButton at: 10:40:59
            this.myLabel.ForeColor = Color.FromArgb(255, 0, 0); // Label Forecolor of RED 255, BLUE 0, GREEN 0
        }// end myButton_Click
    }// end class Form1
}// end namespace ButtonsTest

8. Run

How does it work?

On the right column open Program.cs, you will find:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ButtonsTest
{
    static class Program
    {
        /// <summary>
        /// Punto di ingresso principale dell'applicazione.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

A C# console application must contain a Main method, in which control starts and ends. The Main method is where you create objects and execute other methods.

Main method run Form1.cs -> Form1.cs manage the design components of Form1

Build (Compilazione)

If you do not care about sign your program when you publish go to:
1. main top menu> Project> Properties> Signing (Firma)> un-check Sign the ClickOnce manifest

2. On the main top menu select ‘Build’ (Compilazione) or CTRL+MAISC+B

3. Read the console to see where ButtonsTest.exe is loacated.

4. Run from the folder ButtonsTest.exe

By |C# .NET, Microsoft Visual Studio|Commenti disabilitati su Microsoft Visual Studio – Buttons – Labels – Simple Build Setup