Tuesday`s questions` answers

advertisement
Faculty of Information Technology
Tutorial #1
Lecturer
Module
: Mrs. Eman Alnaji
: Visual Programming
Q1. (Counter Application) Your counter application will consist of a
Label and Button on the Form. The Label initially displays 0, but, each
time a user clicks the Button, the value in the Label is increased by 1.
solution
public partial class Form1 : Form
{
int count = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
count++;
label1.Text = Convert.ToString(count);
}
}
Q2. Create a project "Student Information" and design the following form:
When you run the project, the form should displayed in this way:
The user should enter username "201010200" and password "123456" (password
textbox should display stars). On login button, if any of them is empty, display an
error message
If the user enters a wrong value either in username or in password, an error message
will be displayed.
If both username and password are correct, the hide the "Login" groupbox, and
display the "Information" groupbox
The left side textboxes should contain the default values as displayed above, and
should be locked.
Whenever the user enters a number in "Payment" textbox, and presses Add, the
amount will be added to the amount in "Paid Fees". If the "Paid Fees" exceeds the
Total Fees, display a label with a red message to display the extra credit.
When Logout button is pressed, the "Information" groupbox is hidden, and the "login"
groupbox is displayed again but with empty textboxes.
Solution
private void button2_Click(object sender, EventArgs e)
{
int x;
textBox6.Text = Convert.ToString(Convert.ToInt32(textBox6.Text) +
Convert.ToInt32(textBox7.Text));
if (Convert.ToInt32(textBox6.Text) >
Convert.ToInt32(textBox5.Text))
{
x = (Convert.ToInt32(textBox5.Text) Convert.ToInt32(textBox6.Text)) * -1;
label8.Text = "You have extra credit = " + x;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
MessageBox.Show("Username is not entered", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
else if (textBox2.Text == "")
MessageBox.Show("Password is not entered", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
else if ((textBox1.Text != "201010200") || (textBox2.Text !=
"123456"))
MessageBox.Show("Wrong username or password", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
else
{
groupBox2.Show();
groupBox1.Hide();
}
}
private void button3_Click(object sender, EventArgs e)
{
groupBox1.Show();
textBox1.Text = "";
textBox2.Text = "";
groupBox2.Hide();
}
Q3: Create the following form, and whenever a checkbox is checked, add the letter next
to it to the textbox, and when a checkbox is unchecked, remove the corresponding letter
from the textbox.
Solution1:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
textBox1.Text = "";
if (checkBox1.Checked)
textBox1.Text += "a";
if (checkBox2.Checked)
textBox1.Text += "b";
if (checkBox3.Checked)
textBox1.Text += "c";
if (checkBox4.Checked)
textBox1.Text += "d";
}
Solution2:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
textBox1.Text += "a";
else
textBox1.Text = textBox1.Text.Replace("a", "");
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (checkBox2.Checked)
textBox1.Text += "b";
else
textBox1.Text = textBox1.Text.Replace("b", "");
}
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
if (checkBox3.Checked)
textBox1.Text += "c";
else
textBox1.Text = textBox1.Text.Replace("c", "");
}
private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
if (checkBox4.Checked)
textBox1.Text += "d";
else
textBox1.Text = textBox1.Text.Replace("d", "");
}
Download