PDA

View Full Version : Bài thực hành tuần 1


giangnt
11-06-2012, 09:59 PM
Tuần 1: Maths Operators.<br>
<br>
Download code đầy đủ + giao diện ở file đính kèm.<br>
<br>
Giao diện:<br>
<br>
<img src="http://clickzoom.net/attachment.php?attachmentid=224&d=1314710158" border="0" alt="" onload="NcodeImageResizer.createOn(this);"><br>
<br>
Code xử lý tính toán.<br>
<br>
<div class="bbcode_container">
<div class="bbcode_description">Code:</div>
<pre class="bbcode_code" style="height:492px;">private void Tinh_Click(object sender, EventArgs e)
{
try
{
int lhs, rhs;
float outcome;
lhs = Int32.Parse(So1.Text);
rhs = Int32.Parse(So2.Text);

if (Cong.Checked)
outcome = PhepCong(lhs, rhs);
else if (Tru.Checked)
outcome = PhepTru(lhs, rhs);
else if (Nhan.Checked)
outcome = PhepNhan(lhs, rhs);
else
outcome = PhepChia(lhs, rhs);

KetQua.Text = outcome.ToString();
}
catch (Exception ex)
{
KetQua.Text = ex.Message;
}
So1.Focus();
So1.SelectAll();
//So1.Select(0, So1.TextLength);
}

private int PhepCong(int lhs, int rhs)
{
return lhs + rhs;
}

private int PhepTru(int lhs, int rhs)
{
return lhs - rhs;
}

private int PhepNhan(int lhs, int rhs)
{
return lhs * rhs;
}

private float PhepChia(int lhs, int rhs)
{
return (float)lhs / rhs;
}

private void Thoat_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void Form1_Load(object sender, EventArgs e)
{
Cong.Checked = true;
}</pre>
</div>Code cho 1 số sự kiện.<br>
<br>
<div class="bbcode_container">
<div class="bbcode_description">Code:</div>
<pre class="bbcode_code" style="height:492px;">private bool nonNumberEnter;

private void So1_KeyDown(object sender, KeyEventArgs e)
{
nonNumberEnter = false;

if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
{
if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
{
if (e.KeyCode != Keys.Back)
{
nonNumberEnter = true;
}
}
if (Control.ModifierKeys == Keys.Shift)
nonNumberEnter = true;

if (e.KeyCode == Keys.Enter)
{
Tinh.PerformClick();
}
}
}

private void So1_KeyPress(object sender, KeyPressEventArgs e)
{
if (nonNumberEnter == true)
e.Handled = true;
}

private void So1_Enter(object sender, EventArgs e)
{
So1.SelectAll();
}

private void So2_Enter(object sender, EventArgs e)
{
So2.SelectAll();
}</pre>
</div>Còn cái calculator giống win nữa. Hic.<br>
<br>
Tham khảo code ở cuốn Tung buoc hoc lap trinh Visual C#.net + MSDN.