0% found this document useful (0 votes)
37 views18 pages

IT-412 Final

The documents describe programs that demonstrate the use of various controls and events in Windows forms. The programs include examples of adding and removing items from list boxes, displaying images based on list box selection, performing arithmetic operations on values in text boxes, and converting between time units.

Uploaded by

mf0432663
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views18 pages

IT-412 Final

The documents describe programs that demonstrate the use of various controls and events in Windows forms. The programs include examples of adding and removing items from list boxes, displaying images based on list box selection, performing arithmetic operations on values in text boxes, and converting between time units.

Uploaded by

mf0432663
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Program 1:

List BOX :Add, Delete, Count The Item

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

private void button1_Click(object sender, EventArgs e)


{
listBox1.Items.Add(textBox1.Text);
}

private void button2_Click(object sender, EventArgs e)


{
listBox1.Items.Remove(listBox1.SelectedItem);
}

private void button3_Click(object sender, EventArgs e)


{
MessageBox.Show(listBox1.Items.Count.ToString());

private void button4_Click(object sender, EventArgs e)


{
listBox1.Items.Clear();
}

private void button5_Click(object sender, EventArgs e)


{
Application.Exit();
}
}
}

Program 2:
Write a program that displays a list box to display a list box to display a list of pictures and
a group box with different options for picture size. The user can click any picture in the list
box and size from group box to display the selected picture in the desired size in the picture
box

namespace list
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listBox1.Items.Add("Pakistan");
listBox1.Items.Add("Turkey");
listBox1.Items.Add("Dubai");
listBox1.Items.Add("Japan");
listBox1.Items.Add("South Korea");
listBox1.Items.Add("Bangladesh");
}
private void button5_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string select = listBox1.SelectedItem.ToString();
switch(select)
{
case "Pakistan":
pictureBox1.Image = Properties.Resources.pak;
break;
case "Turkey":
pictureBox1.Image = Properties.Resources.Turkey;
break;
case "Dubai":
pictureBox1.Image = Properties.Resources.Dubai;
break;
case "Japan":
pictureBox1.Image = Properties.Resources.Japan;
break;
case "South Korea":
pictureBox1.Image = Properties.Resources.South_korea;
break;
case "Bangladesh":
pictureBox1.Image = Properties.Resources.hero_bangladesh;
break;
}
}
private void rb1_CheckedChanged(object sender, EventArgs e)
{
if (rb1.Checked == true)
pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
}
private void rb2_CheckedChanged(object sender, EventArgs e)
{
if (rb2.Checked == true)
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
private void rb3_CheckedChanged(object sender, EventArgs e)
{
if (rb3.Checked == true)
pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
}
private void rb4_CheckedChanged(object sender, EventArgs e)
{
if (rb4.Checked == true)
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
}

Program 3:
Write a program that gets your age in years in text box and displays the age in days and
month
namespace Age
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
int ageYear = Convert.ToInt32(textBox1.Text);
int ageMonths = ageYear * 12;
int ageDays = ageYear * 365;
MessageBox.Show($"Age in Month: {ageMonths}\nAge in Days: {ageDays}","Message");
}
Program 4:
Write a program in which has a radio button and checkboxes selected these buttons show
an output in the label.

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

private void button1_Click(object sender, EventArgs e)


{
string input="", msg="";
if(rb1.Checked==true)
input = rb1.Text;
if (rb2.Checked == true)
input = rb2.Text;
if (rb1.Checked == true)
input = rb1.Text;
if (rb3.Checked == true)
input = rb3.Text;
if (rb4.Checked == true)
input = rb4.Text;
if (rb5.Checked == true)
input = rb5.Text;
//checkbox
if (cb1.Checked == true)
msg = cb1.Text;
if (cb2.Checked == true)
msg += cb2.Text;
if (cb3.Checked == true)
msg += cb3.Text;
if (cb4.Checked == true)
msg += cb4.Text;
label1.Text = $"{input}: {msg}";
}
}
}

Create Label, TextBox and Button:


Program 5: The following program contains three controls.
namespace WindowsFormsApp04Mach
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
textBox1.Text = "i am using VP";
}
}
}

Program 6:
In a given program three buttons and 1 label have been created, when you click on
Button1 label display a caption Hello CS, when click on show (Button 2) label show a
caption Focus on programming, when click on Clear (Button 3) label should disappear from
the screen.
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "Hello CS";
}

private void button2_Click(object sender, EventArgs e)


{
label1.Text = "Focus on programming";
}

private void button3_Click(object sender, EventArgs e)


{
label1.Text = "";
}

Program 7: The following program displays two buttons on Form. The caption of the
buttons changes to “GOTFOCUS” and “LOST FOCUS” when a specific event occurs.
private void button1_Click(object sender, EventArgs e)
{

button1.Text = "GOT FOCUS";


}

private void button2_Click(object sender, EventArgs e)


{
button2.Text = "LOST FOCUS";
}

Program 8: Write a program that concatenate the values of two text boxes and display in
the third text Box

private void button1_Click(object sender, EventArgs e)


{
textBox1.Text = "Sibgha";
textBox2.Text = "Zia";
textBox3.Text = (textBox1.Text + textBox2.Text);
}

CS-412 Program 9:
Write a program that stores two values in two integer variables and perform all arithmetic
operations on them and Display the result.
namespace math
{
public partial class Form1 : Form
{
string num1, num2;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
num1 = textBox1.Text;
num2 = textBox2.Text;
if(num1==""&&num2=="")
{
MessageBox.Show("Enter Number in Textboxes");
return;
}
int n1 = int.Parse(num1);
int n2 = int.Parse(num2);
int op = n1 + n2;
MessageBox.Show($"Answer :{op}","Operation");
}
private void button2_Click(object sender, EventArgs e)
{
num1 = textBox1.Text;
num2 = textBox2.Text;
if (num1 == "" && num2 == "")
{
MessageBox.Show("Enter Number in Textboxes");
return;
}
int n1 = int.Parse(num1); int n2 = int.Parse(num2);
int op = n1 - n2;
MessageBox.Show($"Answer :{op}", "Operation");
}
private void button3_Click(object sender, EventArgs e)
{
num1 = textBox1.Text;
num2 = textBox2.Text;
if (num1 == "" && num2 == "")
{
MessageBox.Show("Enter Number in Textboxes");
return;
}
int n1 = int.Parse(num1); int n2 = int.Parse(num2);
int op = n1 * n2;
MessageBox.Show($"Answer :{op}", "Operation");
}
private void button4_Click(object sender, EventArgs e)
{
num1 = textBox1.Text;
num2 = textBox2.Text;
if (num1 == "" && num2 == "")
{
MessageBox.Show("Enter Number in Textboxes");
return;
}
int n1 = int.Parse(num1); int n2 = int.Parse(num2);
int op = n1 / n2;
MessageBox.Show($"Answer :{op}", "Operation");
}
private void button5_Click(object sender, EventArgs e)
{
num1 = textBox1.Text;
num2 = textBox2.Text;
if (num1 == "" && num2 == "")
{
MessageBox.Show("Enter Number in Textboxes");
return;
}
int n1 = int.Parse(num1); int n2 = int.Parse(num2);
int op = (int)Math.Pow(n1,n2);
MessageBox.Show($"Answer is :{op}", "Operation");
}
private void button6_Click(object sender, EventArgs e)
{
num1 = textBox1.Text;
num2 = textBox2.Text;
if (num1 == "" && num2 == "")
{
MessageBox.Show("Enter Number in Textboxes");
return;
}
int n1 = int.Parse(num1); int n2 = int.Parse(num2);
int op = n1 % n2;
MessageBox.Show($"Answer is :{op}", "Operation");
} }}
Program 10:
Write a program to enter an hour’s(3000)in text box, when you compute it, it shows the output
in message box in weeks, days and hours

private void button1_Click(object sender, EventArgs e)


{
string hour = textBox1.Text;
if(hour == "")
{
MessageBox.Show("Enter Number in Textboxes");
return;
}
int h = int.Parse(hour);
int week = h / 168;
h = h % 168;
int day = h / 24;
h = h % 24;
MessageBox.Show($"Weeks: {week}, Days: {day}, Hours: {h}","Message");
}
Program 11:
Write a program which has radio buttons of Celsius and Fahrenheit. convert input data into
Celsius or Fahrenheit.
namespace WindowsFormsApp4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
cel.Checked = true;
}
private void button1_Click(object sender, EventArgs e)
{
double temp;
if (!Double.TryParse(txt1.Text, out temp))
{
MessageBox.Show("Invalid Input, Enter a Valid Input", "Error");
return;
}
if (cel.Checked)
{
double convert = (temp - 32) * 5 / 9;
txt2.Text = convert.ToString("0.00" + "°C");
}
else
{
double convert = temp * 9 / 5 + 32;
txt2.Text = convert.ToString("0.00" + "°F");
}
}
private void button2_Click(object sender, EventArgs e)
{ Application.Exit();
} }}
Program 12:
Write a program of Login page

private void login_Click(object sender, EventArgs e)


{
string user, pass;
user = textBox1.Text;
pass= textBox2.Text;
if (user == "" && pass == "")
MessageBox.Show("Please Enter a Username & Password", "Message");
else if (user == "admin" && pass == "123")
MessageBox.Show("Login Successfully");
else
MessageBox.Show("Incorrect Username or Password");
}

Program 13:
login page with database
private void login_Click(object sender, EventArgs e)
{
//Login Button
string user = textBox1.Text; // admin
string pass = textBox2.Text; //123
//connection string, database name reg
SqlConnection con= new SqlConnection(@"DataSource=DESKTOP-E5FM6G9\SQLEXPRESS
;Initial Catalog=reg;Integrated Security=True");
con.Open();
string query = $"SELECT * FROM users WHERE email='{user}' AND password='{pass}' ";
// users is table name
SqlCommand cmd = new SqlCommand(query, con);
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
MessageBox.Show("Login Successfully");
}
else
{
MessageBox.Show("invalid email or password");
return;
}
}

You might also like