1. Trang chủ
  2. » Cao đẳng - Đại học

Đáp án môn lập trình c

18 1,1K 1

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 18
Dung lượng 48,12 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Từ Toolbox kéo công cụ OpenFileDialog vào form, đặt tên đối tượng này là ofd.. Từ Toolbox kéo công cụ SaveFileDialog vào form, đặt tên đối tượng này là sfd... Từ Toolbox kéo công cụ Fo

Trang 1

ĐÁP ÁN CÂU 10 ĐIỂM

1 Xây dựng chương trình soạn thảo văn bản

Thiết kế giao diện:

Mở Microsoft Visual Studio, tạo và đặt tên ứng dụng

Tạo form, đặt tên form, tên tiêu đề form

Tạo mainMenu

Tạo menuItem File và submenuItem New, Open, Save, Exit trên mainMenu

Tạo menuItem Edit và submenuItem Cut, Copy, Paste trên mainMenu

Tạo menuItem Format và submenuItem Font trên mainMenu Tạo menuItem Help và submenuItem About trên mainMenu

Sử dụng TextBox làm vùng soạn thảo

Thuộc tính Giá trị

Xây dựng các phương thức xử lý:

- Chức năng tạo file mới (File\New)

private void mnew_Click(object sender, System.EventArgs e) {

checkdirty();

}

private void checkdirty()

{

if (dirty) {

DialogResult click = MessageBox.Show(this,"Do You wish to save this

Document?","Save",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Qu estion,MessageBoxDefaultButton.Button1);

if (click == DialogResult.Yes) {

savedata();

txt.Text="";

fname = "";

dirty = false;

Trang 2

}

if (click == DialogResult.No) {

txt.Text="";

fname = "";

dirty = false;

} }

else

{

txt.Text = "";

fname = "";

}

}

- Chức năng mở file trên đĩa (File\Open)

Từ Toolbox kéo công cụ OpenFileDialog vào form, đặt tên đối tượng

này là ofd

private void mopen_Click(object sender, System.EventArgs e)

{

ofd.Multiselect = false;

ofd.Filter = "Text Files|*.txt";

ofd.ShowDialog();

if (File.Exists(ofd.FileName)) {

fname = ofd.FileName;

StreamReader sr = new StreamReader(fname); txt.Text=sr.ReadToEnd();

dirty = false;

sr.Close();

} }

- Chức năng lưu file lên đĩa (File\Save)

Từ Toolbox kéo công cụ SaveFileDialog vào form, đặt tên đối tượng

này là sfd

private void msave_Click(object sender, System.EventArgs e)

{

savedata();

}

private void savedata()

{

if (fname == "") {

sfd.Filter = "Text Files|*.txt";

DialogResult res = sfd.ShowDialog();

Trang 3

if (res == DialogResult.Cancel) {

return;

} fname = sfd.FileName;

MessageBox.Show(fname);

}

StreamWriter sw = new StreamWriter(fname);

sw.WriteLine(txt.Text);

sw.Flush();

sw.Close();

dirty = false;

}

- Chức năng thoát ứng dụng (File\Exit)

private void mexit_Click(object sender, System.EventArgs e)

{

checkdirty();

this.Dispose();

}

- Chức năng Cut (Edit\Cut)

private void mcut_Click(object sender, System.EventArgs e)

{

txt.Cut();

dirty = true;

}

- Chức năng Copy (Edit\Copy)

private void mcopy_Click(object sender, System.EventArgs e)

{

Clipboard.SetDataObject(txt.SelectedText, true); }

- Chức năng Paste (Edit\Paste)

private void mpaste_Click(object sender, System.EventArgs e)

{

IDataObject iData = Clipboard.GetDataObject();

if (iData.GetDataPresent(DataFormats.Text))

{

txt.SelectedText = iData.GetData(DataFormats.Text).ToString();

} }

- Chức năng định dạng văn bản (Format\Font)

Từ Toolbox kéo công cụ FontDialog vào form, đặt tên đối tượng này là

fd

private void mfont_Click(object sender, System.EventArgs e)

{

fd.ShowColor = true;

fd.ShowDialog();

Trang 4

txt.Font = fd.Font;

txt.ForeColor=fd.Color;

}

- Chức năng About (Help\About)

Trong cửa sổ Solution Explorer, bấm chuột phải vào tên ứng dụng, chọn mục Add\Windows form, trong cửa sổ hiện ra chọn About form Trong cửa sổ Properties đặt tên cho đối tượng form này là NewForm

trong mục name

private void mabout_Click(object sender, System.EventArgs e)

{

NewForm nf = new NewForm();

nf.ShowDialog();

}

Câu Ý Nội dung

2 Xây dựng chương trình soạn thảo văn bản

Thiết kế giao diện

- Mở ứng dụng Microsoft Visual Studio, tạo một Project mới

- Đặt tên Form là F_chinh, thay đổi tiêu đề của Form

- Tạo menu cho Form:

+ Trong cửa sổ Toolbox, chọn mục Menu&Toolbars kéo công cụ MenuStrip thả vào form, đặt tên cho menu này

+ Tạo menu File và submenu Exit

- Tạo thanh công cụ tắt:

+ Trong cửa sổ Toolbox, chọn mục Menu&Toolbars kéo công cụ ToolStrip thả vào form, đặt tên cho thanh công cụ này

+ Trên thanh công cụ vừa tạo, tạo các button: tạo file mới, mở file đã

có, ghi file…, các comboBox chọn font chữ, cỡ chữ

- Tạo Form con:

+ Bấm chuột phải vào tên ứng dụng, chọn mục Add, chọn Windows Form Trong cửa sổ hiện ra chọn kiểu form là Windows Form, đặt tên cho form là F_con

+ Trên Form con kéo thả công cụ TextBox vào làm vùng soạn thảo

Thuộc tính Giá trị

Xây dựng các phương thức xử lý:

Trang 5

- Tạo file mới:

void taomoi()

{

F_con fc = new F_con();

fc.MdiParent = this;

fc.Text = "Document " + (stt++).ToString();

closeToolStripMenuItem.Enabled = true;

saveToolStripMenuItem.Enabled = true;

tsmauto.Enabled = true;

toolStripSave.Enabled = true;

fc.Show();

}

void newopen(string s)

{

F_con fc = new F_con();

fc.MdiParent = this;

fc.Text = s;

closeToolStripMenuItem.Enabled = true;

saveToolStripMenuItem.Enabled = true;

toolStripSave.Enabled = true;

fc.Show();

}

private void toolStripButton1_Click(object sender, EventArgs e) {

taomoi();

}

- Mở file đã có trên đĩa:

Từ Toolbox kéo công cụ OpenFileDialog vào form, đặt tên đối tượng này là open

void openfile()

{

OpenFileDialog open = new OpenFileDialog();

open.Filter = "Text Files (.txt)|*.txt|Ms Word Files

(.doc)|*.doc|Rich Text Files (.rtf)|*.rtf|All Files (*.*)|*.*"; open.Multiselect = true;

open.FilterIndex = 2;

if (open.ShowDialog() == DialogResult.OK)

{

newopen(open.FileName);

((F_con)this.ActiveMdiChild).txtnoidung.LoadFile(open.FileName); }

}

private void toolStripButton2_Click(object sender, EventArgs e) {

openfile();

}

Trang 6

- Ghi file đã có soạn thảo:

Từ Toolbox kéo công cụ SaveFileDialog vào form, đặt tên đối tượng này là savefile.

void savefile_new()

{

SaveFileDialog savefile = new SaveFileDialog();

savefile.Filter = "Text Files (.txt)|*.txt|Ms Word Files (.doc)|*.doc|Rich Text Files (.rtf)|*.rtf|All Files

(*.*)|*.*";

savefile.FilterIndex = 2;

savefile.RestoreDirectory = true;

if (savefile.ShowDialog() == DialogResult.OK)

{

((F_con)this.ActiveMdiChild).txtnoidung.SaveFile(savefile.FileName );

((F_con)this.ActiveMdiChild).Text =

savefile.FileName;

}

}

void SaveFileTong()

{

string s = this.ActiveMdiChild.Text;

if (s.Substring(0, 8).Equals("Document"))

{

savefile_new();

}

else

{

((F_con)this.ActiveMdiChild).txtnoidung.SaveFile(s);

}

}

private void toolStripSave_Click(object sender, EventArgs e) {

SaveFileTong();

}

3 Xây dựng chương trình xử lý văn bản

Thiết kế giao diện:

- Mở Microsoft Visual Studio, tạo và đặt tên ứng dụng

- Tạo form, đặt tên form, thay đổi tiêu đề của form

- Tạo textBox chứa nội dung của văn bản, thay đổi một số thuộc tính của textBox cho phù hợp

- Tạo các label với text là Find what, Replace with

- Tạo textBox để nhập vào từ cần tìm kiếm, đặt tên textBox

Trang 7

- Tạo textBox để nhập vào từ cần thay thế, đặt tên textBox

- Tạo button Find, đặt tên button này

- Tạo button Find next, đặt tên button này

- Tạo button Replace, đặt tên button này

- Tạo button Replace all, đặt tên button này

- Tạo button Undo, đặt tên button này

- Tạo button Exit, đặt tên button này

Xây dựng các phương thức xử lý:

- Xử lý khi người dùng nhấn button Find

private void btnTim_Click(object sender, EventArgs e)

{

if (txtVanBan.Text == "") MessageBox.Show("Bạn chưa nhập nội dung văn bản", "Thông báo");

BatDau = txtVanBan.Text.IndexOf(txtChuoiTim.Text,

StringComparison.OrdinalIgnoreCase);

if (BatDau >= 0)

{

txtVanBan.SelectionStart = BatDau;

txtVanBan.SelectionLength =

txtChuoiTim.TextLength;

txtVanBan.Focus();

VitriBD=BatDau+txtChuoiTim.TextLength;

}

else

{

MessageBox.Show("Không có chuỗi này trong văn bản", "Tìm Kiếm");

BatDau = 0;

}

}

- Xử lý khi người dùng nhấn button Find Next

private void btnTimtiep_Click(object sender, EventArgs e)

{

if (txtVanBan.Text == "") MessageBox.Show("Bạn chưa nhập nội dung văn bản", "Thông báo");

int TimTiep;

if (VitriBD > txtVanBan.TextLength -

txtChuoiTim.TextLength)

{

MessageBox.Show("Không còn tìm thấy chuổi, đã hết văn bản", "Tìm Tiếp");

VitriBD = 0; return;

Trang 8

}

TimTiep = txtVanBan.Text.IndexOf(txtChuoiTim.Text, VitriBD, StringComparison.OrdinalIgnoreCase);

if (TimTiep >= 0)

{

txtVanBan.SelectionStart = TimTiep;

txtVanBan.SelectionLength =

txtChuoiTim.TextLength;

txtVanBan.Focus();

VitriBD = TimTiep + txtChuoiTim.TextLength; }

else

{

if (VitriBD == 0)

{

MessageBox.Show("Không có chuỗi này trong văn bản", "Tìm kiếm");

}

else

{

MessageBox.Show("Không còn tìm thấy chuổi, đã hết văn bản", "Tìm Tiếp");

VitriBD = 0;

}

}

}

- Xử lý khi người dùng nhấn button Replace

private void btnThayThe_Click(object sender, EventArgs e)

{

if (txtVanBan.Text == "") MessageBox.Show("Bạn chưa nhập nội dung văn bản", "Thông báo");

else NoiDung = txtVanBan.Text;

if (txtChuoiTim.Text == "")

{

MessageBox.Show("Chưa có chuỗi cần tìm", "Tìm và thay thế");

return;

}

BatDau = txtVanBan.Text.IndexOf(txtChuoiTim.Text, VitriBD, StringComparison.OrdinalIgnoreCase);

if (BatDau >= 0)

{

txtVanBan.SelectionStart = BatDau;

txtVanBan.SelectionLength =

txtChuoiTim.TextLength;

VitriBD = BatDau + txtChuoiTim.TextLength;

txtVanBan.Focus();

Trang 9

txtVanBan.SelectedText =

txtVanBan.SelectedText.Replace(txtVanBan.SelectedText,

txtChuoiThay.Text);

}

else

{

if (VitriBD == 0)

{

MessageBox.Show("Không có chuỗi này trong văn bản", "Tìm và thay thế");

}

else

{

MessageBox.Show("Không còn tìm thấy,đã hết văn bản", "Tìm và thay thế");

BatDau = 0;

VitriBD = 0;

}

}

}

Trang 10

- Xử lý khi người dùng nhấn button Replace All

private void btnThayTatCa_Click(object sender, EventArgs e) {

if (txtVanBan.Text == "") MessageBox.Show("Bạn chưa nhập nội dung văn bản", "Thông báo");

if (txtChuoiTim.Text == "")

{

MessageBox.Show("Chưa có chổ cần tìm", "Tìm và thay thế");

return;

}

BatDau = txtVanBan.Text.IndexOf(txtChuoiTim.Text, VitriBD,StringComparison.OrdinalIgnoreCase);

if (BatDau == -1)

{

MessageBox.Show("Không có chuỗi này trong văn bản", "Tìm và thay thế");

BatDau = 0; return;

}

while (BatDau >= 0)

{

txtVanBan.SelectionStart = BatDau;

txtVanBan.SelectionLength =

txtChuoiTim.TextLength;

VitriBD = BatDau + txtChuoiTim.TextLength;

txtVanBan.Focus();

txtVanBan.SelectedText =

txtVanBan.SelectedText.Replace(txtVanBan.SelectedText,txtChuoiTha y.Text);

BatDau = txtVanBan.Text.IndexOf(txtChuoiTim.Text, VitriBD,StringComparison.OrdinalIgnoreCase);

}

MessageBox.Show("Đã hết văn bản", "Tìm và thay thế"); BatDau = 0;

VitriBD = 0

}

- Xử lý khi người dùng nhấn button Undo

(noidung = txtVanban.text)

private void btnLamlai_Click(object sender, EventArgs e)

{

txtVanBan.Text = NoiDung;

txtChuoiTim.Clear();

txtChuoiThay.Clear();

txtChuoiTim.Focus();

BatDau = 0;

VitriBD = 0;

}

Trang 11

- Xử lý khi người dùng nhấn button Exit

private void btnThoat_Click(object sender, EventArgs e)

{

this.Close();

}

4 Xây dựng chương trình vẽ hình

Thiết kế giao diện:

- Mở Microsoft Visual Studio, tạo và đặt tên ứng dụng

- Tạo form, đặt tên form, thay đổi tiêu đề của form

- Tạo toolStrip dùng để chứa các mục chọn chức năng

- Trên toolStrip thêm được các mục: Pen Size, PenColor, Clear, BackGround Color, Draw Shape

- Tạo numericUpDown dùng để chỉ giá trị của Pen Size

- Tạo numericUpDown dùng để chỉ giá trị của Shape Size

- Tạo Panel dùng để làm cửa sổ vẽ

Xây dựng các phương thức xử lý:

- Xử lý xự kiện khi người dùng chọn màu vẽ (PenColor)

private void toolStripButton1_Click(object sender, EventArgs e) {

ColorDialog cd = new ColorDialog();

if (cd.ShowDialog() == DialogResult.OK)

{

toolStripButton1.ForeColor = cd.Color;

}

}

- Xử lý xự kiện khi người dùng chọn màu nền (BachGround Color)

private void toolStripButton3_Click(object sender, EventArgs e) {

ColorDialog cd = new ColorDialog();

if (cd.ShowDialog() == DialogResult.OK)

{

toolStripButton3.ForeColor = cd.Color;

panel1.BackColor = cd.Color;

}

}

- Xử lý sự kiện khi người dùng muốn xóa tất cả các hình trên cửa sổ vẽ (Clear)

private void toolStripButton2_Click(object sender, EventArgs e) {

Trang 12

_g.Clear(panel1.BackColor);

}

- Xử lý sự kiện khi người dùng vẽ bằng bút vẽ

private void panel1_MouseMove(object sender, MouseEventArgs e) {

if (_canPaint)

{

Pen pen = new Pen(toolStripButton1.ForeColor, float.Parse(numericUpDown1.Value.ToString()));

_g.DrawLine(pen, new Point(_preX, _preY), new

Point(e.X, e.Y));

}

_preX = e.X; _preY = e.Y;

}

- Xử lý sự kiện khi người dùng vẽ hình vuông, hình chữ nhật, hình tròn

private void panel1_MouseDown(object sender, MouseEventArgs e) {

_canPaint = true;

if (_drawSquare)

{

_canPaint = false;

_drawSquare = false;

SolidBrush sb = new

SolidBrush(toolStripButton1.ForeColor);

_g.FillRectangle(sb, e.X, e.Y,

int.Parse(numericUpDown2.Value.ToString()),

int.Parse(numericUpDown2.Value.ToString()));

}

else if (_drawRectangle)

{

_canPaint = false;

_drawRectangle = false;

SolidBrush sb = new

SolidBrush(toolStripButton1.ForeColor);

_g.FillRectangle(sb, e.X, e.Y,

int.Parse(numericUpDown2.Value.ToString()) * 2,

int.Parse(numericUpDown2.Value.ToString()));

}

else if (_drawCircle)

{

_canPaint = false;

_drawCircle = false;

SolidBrush sb = new

SolidBrush(toolStripButton1.ForeColor);

_g.FillEllipse(sb, e.X, e.Y,

Trang 13

int.Parse(numericUpDown2.Value.ToString()),

int.Parse(numericUpDown2.Value.ToString()));

}

}

Câu Ý Nội dung

5 Xây dựng chương trình quản lý học sinh

Thiết kế CSDL:

- Mở Microsoft SQL Server Management Studio tạo cơ sở dữ liệu

HocSinh

- Trong cơ sở dữ liệu đã tạo tạo bảng hocsinh có cấu trúc theo yêu cầu

- Nhập được thông tin của ít nhất 5 học sinh

Thiết kế giao diện:`

- Mở Microsoft Visual Studio, tạo và đặt tên ứng dụng

- Tạo form, đặt tên form, thay đổi tiêu đề của form

- Tạo label có text là Mã học sinh, textBox chứa mã học sinh, đặt tên textBox

- Tạo label có text là Họ và tên, textBox chứa họ tên học sinh, đặt tên textBox

- Tạo label có text là Ngày sinh, textBox chứa ngày sinh, đặt tên textBox

- Tạo label có text Giới tính, textBox chứa giới tính, đặt tên textBox

- Tạo label có text Quê quán, textBox chứa quê quán, đặt tên textBox

- Tạo label có text Email, textBox chứa email, đặt tên textBox

- Tạo và đặt tên các button điều khiển việc xem thông tin học sinh

- Tạo button Add New, đặt tên cho button này

- Tạo button Save, đặt tên cho button này

- Tạo button Edit, đặt tên cho button này

- Tạo button Delete, đặt tên cho button này

- Tạo button Cancel, đặt tên cho button này

Viết các phương thức xử lý:

- Tạo kết nối đến cơ sở dữ liệu:

public frmHocsinh()

{

InitializeComponent();

connectionString = "Server=TUQUYEN; Database=hocsinh;

Trusted_connection=true";

sqlconHocsinh = new SqlConnection(connectionString);

sqlcomHocsinh = new SqlCommand();

}

Ngày đăng: 29/03/2016, 10:43

TỪ KHÓA LIÊN QUAN

w