Here is a basic program that Returns text from the textbox. You are given an option to either print the message out in either a messagebox or withinn the window itself.
What you will need:
- A textbox (txtText)
- A label (lblMessage)
- A checkBox (chkMessage)
- A button (btnGreet)
- //This is the only class
Here's the Source code:
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 CS_Playground
{
    public partial class Form1 : Form
    {
        private string yourName;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
           
            lblMessage.Text = ""; //Make sure the label doesn't say anything
        }
        private string greeting()
        {
            
            return ("Hello " + yourName + " Nice to meet you!");
        }
        private void btnGreet_Click(object sender, EventArgs e)
        {
            yourName = txtName.Text;
            if (chkMessage.Checked == true)
            {
                showMessage(greeting());
            }
            else
            {
                lblMessage.Text = greeting(); //Print the message below the button in the label
            }
            btnGreet.Text = "Do it again?"; //Change the text of the button when clicked
            txtName.Text = ""; //Empties the textbox
        }
        //Shows a message in a messagebox
        private void showMessage(string text)
        {
            System.Windows.Forms.MessageBox.Show(text);
        }
    }
}
A good challenge can be making the name bold in the message. Thanks for reading!
 


 
No comments:
Post a Comment