0% found this document useful (0 votes)
2 views

programs_visual_studio

The document contains a series of C# programs demonstrating various functionalities such as displaying messages, performing arithmetic operations, converting data types, and handling user input through forms. Each program is encapsulated in a class with methods that respond to button clicks, showcasing different programming concepts and user interface interactions. The programs include tasks like calculating sums, changing label texts, swapping forms, and validating user inputs.

Uploaded by

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

programs_visual_studio

The document contains a series of C# programs demonstrating various functionalities such as displaying messages, performing arithmetic operations, converting data types, and handling user input through forms. Each program is encapsulated in a class with methods that respond to button clicks, showcasing different programming concepts and user interface interactions. The programs include tasks like calculating sums, changing label texts, swapping forms, and validating user inputs.

Uploaded by

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

program :1 Display MessageBox with "hi cs".

public partial class Form1 : Form


{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{

MessageBox.Show("hi cs");
}

private void label1_Click(object sender, EventArgs e)


{
label1.Text = "hello cs";

program:2 Addition of Two Numbers from TextBoxes in label

public partial class Form1 : Form


{
public Form1()
{
InitializeComponent();
}

private void label1_Click(object sender, EventArgs e)


{

private void textBox1_TextChanged(object sender, EventArgs e)


{

private void button1_Click(object sender, EventArgs e)


{
int a;
int b;
a = Convert.ToInt32(textBox1.Text);
b = Convert.ToInt32(textBox2.Text);
int sum = a + b;
label3.Text = "sum:" + sum;
}
}

program :3 Change Label Text on Button Click

public partial class Form1 : Form


{
public Form1()
{
InitializeComponent();
}

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:4 Change Button Text on Click buttons (Focus and Lost Focus)
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

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 :5 Sum of First and Second Name in a TextBox
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
textBox1.Text = "FIRST NAME";
textBox2.Text = " SECOND NAME";
textBox3.Text = textBox1.Text + textBox2.Text;
}
}
program:6 Write a program that contain two forms and each form have one buttons
BY click on button form swap with other forms .
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
Form2 form2 =new Form2();
form2.Show();
this.Hide();

}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
Form1 form1 = new Form1();
form1.Show();
this.Hide();
}
}
program:7 Write a program that contain three forms and each form have two buttons
BY click on buttons form swap with others forms

public partial class Form1 : Form


{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
Form2 form2 = new Form2();
form2.Show();
this.Hide();
}

private void button2_Click(object sender, EventArgs e)


{
Form3 form3 = new Form3();
form3.Show();
this.Hide();
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
Form1 form1 = new Form1();
form1.Show();
this.Hide();
}
private void button2_Click(object sender, EventArgs e)
{
Form3 form3 = new Form3();
form3.Show();
this.Hide();
}
}
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
Form1 form1 = new Form1();
form1.Show();
this.Hide();

private void button2_Click(object sender, EventArgs e)


{

Form2 form2 = new Form2();


form2.Show();
this.Hide();
}
}
program:8 Change Background Color on Button Click

public partial class Form1 : Form


{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
this.BackColor = Color.Red;

private void button2_Click(object sender, EventArgs e)


{
this.BackColor = Color.Green;

private void button3_Click(object sender, EventArgs e)


{
this.BackColor = Color.Black;
}
}
program:9 Display Personal Information in Labels
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
label1.Text = "your name";
label2.Text = "grade";
label3.Text = "BSCS";
label4.Text = "Ag Number";
label5.Text = "average";

}
}
program :10 Basic Arithmetic Operations and display in labels and show welcome
in MessageBox
and name of message box is output show messagebox buttons yes,no,cancel. and show
icon also.

public partial class Form1 : Form


{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
int num1 = 8;
int num2 = 4;
int addition = num1 + num2;
int subtraction = num1 - num2;
int multiplicaTION= num1 *num2;
int DIVISION = num1 /num2;
int MODULUS = num1 % num2;

label1.Text = "ADDITIION:" + (num1 + num2);


label2.Text = "subtraction:" + (num1 - num2);
label3.Text = "multiplication:" + (num1 * num2);
label4.Text = "division" + (num1 / num2);
label5.Text = "modulus:" + (num1 % num2);
MessageBox.Show("welcome", "output",
MessageBoxButtons .YesNoCancel,
MessageBoxIcon.Question);

}
}
program:11 Input two numbers in textboxes from user Addition of Two Numbers and
Display in TextBox

public partial class Form1 : Form


{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
int num1 = int.Parse(textBox1.Text);
int num2 = int.Parse(textBox2.Text);
int sum = num1 + num2;
textBox3.Text = sum.ToString();
}
}
program:12 Convert Number to Boolean and Display in messagebox.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{

int number = Convert.ToInt32(textBox1.Text);//convert input to integer


bool result = number != 0;//0=false,any other number = true
MessageBox.Show("boolean value:" + result, "result");//show result
}
}
program:13 Calculate Total Cost of Books
public partial class Form1 : Form
{
double BOOKS_PRIZE = 15.00;
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
int NOOFBOOKS = Convert.ToInt32(textBox1.Text);
double totalcost = NOOFBOOKS * BOOKS_PRIZE;
MessageBox.Show("total caost:$" + totalcost, "cost calculation");

}
}
program:14 Extract and Display First Character of a String
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string input = (textBox1.Text);
Char result = input[0];
MessageBox.Show("character:" + result, "Result");
}
}
program:15 Convert String to Double and Display in MessageBox
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
string inputText = textBox1.Text;
double result = Convert.ToDouble(inputText);
Console.WriteLine(result);
MessageBox.Show("convert into double:" + result, "success");
}
}
program:16 Convert String to Integer and Display in MessageBox
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
String inputText = textBox1.Text;
double number = Convert.ToDouble(inputText);
int intvalue = (int)number;
MessageBox.Show("converted integer value:" + intvalue, "success");
}
}
program:17 Convert String to Decimal and Display in MessageBox
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
string inputText = textBox1.Text;
double number = Convert.ToDouble(inputText);
decimal decimalvalue = (decimal)number;
MessageBox.Show("converted decimal value:" + decimalvalue, "success");

}
}
program:18 Convert Double to String and Display in MessageBox
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
double doublevalue = 2345.5657878;
string stringvalue = doublevalue.ToString();
Console.WriteLine(stringvalue);
MessageBox.Show("converted string value:" + stringvalue, "success");
}
}
program:19 Display Different Date and Time Formats
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
DateTime now = DateTime.Now;

string time = "diifferent date & time formats:\n";


time += $"full date &time:{now.ToString("f")}\n";
time += $"short date:{now.ToString("d")}\n";
time += $"long date:{now.ToString("D")}\n";
time += $"short Time:{now.ToString("t")}\n";
time += $"long Time:{now.ToString("T")}\n";
time += $"custom format 1:{now.ToString("yyyy-MM-dd HH-mm-ss")}\n";
time += $"custom format 2:{now.ToString("MMM dd,yyyy hh:mm tt")}\n";
time += $"DAY of week:{now.ToString("dddd")}\n";
MessageBox.Show(time);

}
}
program 20 Write a program that contains two textboxes and one button .
when user clicks the button after entering some value to textboxes
Both value should be inter-change.

public partial class Form1 : Form


{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
string temp = textBox1.Text;
textBox1.Text = textBox2.Text;
textBox2.Text = temp;
}
}
program 21 Calculate Percentage from Obtained and Total Marks(input from user)
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
double obtainedmarks = Convert.ToDouble(textBox1.Text);
double totalmarks = Convert.ToDouble(textBox2.Text);
double percentage =( obtainedmarks / totalmarks) * 100;
MessageBox.Show("percentage:" + percentage.ToString("0.00") + "%");
}
}
program 22 Write a program that gets tempreture from user in Fahrenheit and
convert to Celsius using formula C=5/9(F-32).
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
double fahrenheit = Convert.ToDouble(textBox1.Text);
double celsius = (5.0 / 9.0) * (fahrenheit - 32);

MessageBox.Show ("Celsius: "+ celsius.ToString("0.00") + "°C");

}
}
program 23 Write a program that gets the temperature from user in Celsius and
convert to Fahrenheit
using formula F=9/5C+32.

public partial class Form1 : Form


{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
double celsius = Convert.ToDouble(textBox1.Text);
double fahrenheit = (9.0 / 5.0) * celsius + 32;

MessageBox.Show ("Fahrenheit: " + fahrenheit.ToString("0.00") + "°F");

}
}
program 24 Wrie a program that inputs second from the user and display
hours ,minutes and seconds.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
int totalSeconds = Convert.ToInt32(textBox1.Text);
int hours = totalSeconds / 3600;
int minutes = (totalSeconds % 3600) / 60;
int seconds = totalSeconds % 60;

MessageBox.Show ( $"{hours}h : {minutes}m : {seconds}s");

}
}
program 25 Write a program that gets the 3 digit number from user and display it
in Reverse order.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
string input = textBox1.Text;

// Check if the input is a three-digit number


if (input.Length != 3 || !int.TryParse(input, out _))
{
MessageBox.Show("Invalid input. Please enter a three-digit number.");
return;
}

// Reverse the number


char[] arr = input.ToCharArray();
Array.Reverse(arr);
string reversed = new string(arr);
MessageBox.Show ( "Reversed number: " + reversed);
}
}
program 26 Write a program that gets your age in years in textbox and display the
age in days and months.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int ageinyears = int.Parse(textBox1.Text);
int ageinmonths = ageinyears * 12;
int ageindays = (int)(ageinyears * 365.25);
MessageBox.Show($"your age:\nmonths:{ageinmonths}\ndays:{ageindays}");
}
}
program 27 Write a program in C# Sharp to check whether a number is a palindrome
or not.

public partial class Form1 : Form


{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string input = textBox1.Text;
char[] arr = input.ToCharArray();
Array.Reverse(arr);
string reversed = new string(arr);
if (input==reversed)
{
MessageBox.Show("this is palindrome:");
}
else
{
MessageBox.Show("the number is not palindrome:");
}
}
}
program 27 Write a program that checks the input letter in text box is vowel or
not.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
string input = textBox1.Text;
if("aeiouAEIOU".Contains(input))
{
MessageBox.Show("VOWELS","VOWEL CHECKER", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
else
{
MessageBox.Show("NOT VOWELS", "VOWEL CHECKER",MessageBoxButtons.OK,
MessageBoxIcon.Information );
}
}
}
program 28 Write a program that input marks from the user and display
congrautulation! you have passed.
if marks are above 40.and display you are failed if marks less than 40.

public partial class Form1 : Form


{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{

private void button1_Click(object sender, EventArgs e)


{
int marks = Convert.ToInt32(textBox1.Text);
if(marks>=40)
{
MessageBox.Show("Congraulation!you have passed");
}
else
{
MessageBox.Show("you are failed");
}
}
}

You might also like