In: Computer Science
In C# Create a windows application which accepts the month
number and displays the month name in a label.
Use a nested if... else statement to determine the month
name.
For months not in the range 1-12 display the message "Not a valid
month"
Answer:
Note: Please rename form component as displayed in screenshot
Code:
using System;
using System.Windows.Forms;
using System;
using System.Windows.Forms;
namespace MonthName
{
public partial class Month : Form
{
public Month()
{
InitializeComponent();
}
private void btnDisplay_Click(object sender, EventArgs e)
{
//convert text into number
int num = Convert.ToInt32(txtNum.Text);
if(num == 1)//check for number
lblmonth.Text="January"; //Display month name
else if (num == 2)
lblmonth.Text = "February";
else if (num == 3)
lblmonth.Text = "March";
else if (num == 4)
lblmonth.Text = "April";
else if (num == 5)
lblmonth.Text = "May";
else if (num == 6)
lblmonth.Text = "June";
else if (num == 7)
lblmonth.Text = "July";
else if (num == 8)
lblmonth.Text = "August";
else if (num == 9)
lblmonth.Text = "September";
else if (num == 10)
lblmonth.Text = "October";
else if (num == 11)
lblmonth.Text = "November";
else if (num == 12)
lblmonth.Text = "December";
else //number is not from 1 to 12
lblmonth.Text = "Not a valid month";
}
}
}
Screenshot: