News Ticker

Menu

Browsing "Older Posts"

Browsing Category ".NET Winform"

Tuần 2 - Phần 2 - Câu 2

Thứ Bảy, 4 tháng 1, 2020 / No Comments

---- Link Download ----
Download: OutSource
Download: DataBase
Folder: Tuan2 

Tuần 2 - Phần 2 - Câu 1

/ No Comments


---- Link Download ----
Download: OutSource
Download: DataBase
Folder: Tuan2 

.NET Chủ đề 9 - Nhóm 7

Thứ Bảy, 7 tháng 12, 2019 / No Comments

Link OutSource: OutSource
Group: QST Team!

Giải Bài Kiểm Tra .NET

Thứ Năm, 28 tháng 11, 2019 / No Comments
 Link Download Project: Click Download

Câu 1: Lập trình Console



Class Book:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cau1
{
    class Book
    {
        private string m_Title;
        private string m_Code;
        private int m_Page;

        public string Title { get => m_Title; set => m_Title = value; }
        public string Code { get => m_Code; set => m_Code = value; }
        public int Page { get => m_Page; set => m_Page = value; }

        public Book()
        {
            this.Title = "";
            this.Page = 0;
            this.Code = "";
        }

        public Book(string code)
        {
            this.Code = code;
        }

        public Book(string title, string code)
        {
            this.Title = title;
            this.Code = code;
        }

        public Book(string title, string code, int page)
        {
            this.Title = title;
            this.Code = code;
            this.Page = page;
        }

        public Book(Book bk)
        {
            this.Title = bk.Title;
            this.Code = bk.Code;
            this.Page = bk.Page;
        }

    }
}

Class Library:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cau1
{
    class Library
    {
        ArrayList m_BookShelf;

        public Library()
        {
            m_BookShelf = new ArrayList();
        }

        public void AddBook(Book newBook)
        {
            m_BookShelf.Add(newBook);
        }

        public Book RemoveBook(string code)
        {
            int i = 0;

            foreach (Book item in m_BookShelf)
            {
                if (item.Code == code)
                    m_BookShelf.RemoveAt(i);

                i++;
            }

            return null;
        }

        public Book FindBook(string title)
        {
            foreach (Book item in m_BookShelf)
            {
                if (item.Title == title)
                    return item;
            }

            return null;
        }
    }
}

Class Main:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cau1
{
    class Program
    {
        static void Main(string[] args)
        {
            int lua_chon = 0;
            Library lb = new Library();

            do
            {
                Console.WriteLine("===== MENU =====");
                Console.WriteLine("1. Nhap Sach");
                Console.WriteLine("2. Tim Sach");
                Console.WriteLine("3. Xoa Sach");
                Console.WriteLine("0. Exit");
                Console.Write("Ban muon chon?: ");
                lua_chon = int.Parse(Console.ReadLine());
                Console.WriteLine("\n");

                switch (lua_chon)
                {
                    case 1:
                        Console.WriteLine("----- THEM SACH -----");
                        Console.Write("Nhap so luong sach: ");
                        int sl = int.Parse(Console.ReadLine());

                        for (int i = 0; i < sl; i++)
                        {
                            Book bk = new Book();

                            Console.Write("Nhap ten sach: ");
                            bk.Title = Console.ReadLine();

                            Console.Write("Nhap ma sach: ");
                            bk.Code = Console.ReadLine();

                            Console.Write("Nhap trang sach: ");
                            bk.Page = int.Parse(Console.ReadLine());

                            lb.AddBook(bk);
                        }
                        break;
                    case 2:
                        Console.WriteLine("----- TIM SACH -----");
                        Console.Write("Nhap ten sach can tim: ");
                        string ten = Console.ReadLine();
                        Book bk1 = new Book();
                        bk1 = lb.FindBook(ten);

                        if (bk1 != null)
                        {
                            Console.WriteLine("Da tim thay ["+ten+"]");
                            Console.WriteLine("Ten sach: "+bk1.Title);
                            Console.WriteLine("Ma sach: " + bk1.Code);
                            Console.WriteLine("Page sach: " + bk1.Page);
                        }
                        else
                            Console.WriteLine("Khong tim thay [" + ten + "] trong thu vien!");

                        break;
                    case 3:
                        Console.WriteLine("----- XOA SACH -----");
                        Console.Write("Nhap ma sach can xoa: ");
                        string ma_sach = Console.ReadLine();
                        //Book temp = new Book();
                        lb.RemoveBook(ma_sach);

                        break;
                    case 0:
                        Console.WriteLine("Ban da thoat chuong trinh!");
                        break;
                    default:
                        Console.WriteLine("Ban da chon sai! Hay chon lai!\n\n");
                        break;
                }

            } while (lua_chon != 0);

            Console.ReadKey();
        }
    }
}
Câu 2: Lập trình winform, mô hình 3 lớp


Class DataBase (Lớp thao tác CSDL ):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Cau2
{
    public class Database
    {
        SqlConnection Connection;
        SqlCommand cmd;

        public Database()
        {
            string strConnect = "";
            Connection = new SqlConnection(strConnect);
        }

        public DataTable ExecuteQuery(string query)
        {
            Connection.Open();
            cmd = new SqlCommand(query, Connection);
            DataTable data = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            adapter.Fill(data);
            Connection.Close();
            return data;
        }

        public int ExecuteNonQuery(string query)
        {
            int data = 0;
            Connection.Open();
            SqlCommand command = new SqlCommand(query, Connection);
            data = command.ExecuteNonQuery();
            Connection.Close();
            return data;
        }
    }
}


Class DocGia (Lớp xử lý nghiệp vụ ):
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace Cau2
{
    public class DocGia
    {
        public DataTable LoadData()
        {
            string query = "select *from DocGia";
            DataTable result = new Database().ExecuteQuery(query);
            return result;
        }

        public bool ThemDocGia(string ten, DateTime ngaysinh, string dia_chi, string email, DateTime ngay_lap, DateTime ngay_het_han, float tien_no)
        {
            string query = "insert into DocGia values(N'"+ten+"', '"+ngaysinh+"', N'"+dia_chi+"', '"+email+"', '"+ngay_lap+"', '"+ngay_het_han+"', "+tien_no+")";
            int result = new Database().ExecuteNonQuery(query);
            return result > 0;
        }

        public bool XoaDocGia(int id)
        {
            string query = "delete from DocGia where MaDocGia = '"+id+"'";
            int result = new Database().ExecuteNonQuery(query);
            return result > 0;
        }

        public bool SuaDocGia(string id, string ten, DateTime ngaysinh, string dia_chi, string email, DateTime ngay_lap, DateTime ngay_het_han, float tien_no)
        {
            string query = "Update DocGia set HoTenDocGia = N'"+ten+"', NgaySinh = '"+ngaysinh+"', DiaChi = N'"+dia_chi+"', Email = '"+email+ "', NgayLapThe = '"+ngay_lap+ "', NgayHetHan = '"+ngay_het_han+ "', TienNo = '"+tien_no+"'  where MaDocGia = '" + id+"'";
            int result = new Database().ExecuteNonQuery(query);
            return result > 0;
        }
    }
}

Class fQuanLyDocGia(Lớp xử lý giao diện ):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Cau2
{
    public partial class fQuanLyDocGia : Form
    {
        public fQuanLyDocGia()
        {
            InitializeComponent();
        }

        private void fQuanLyDocGia_Load(object sender, EventArgs e)
        {
            HienThi();
        }

        void HienThi()
        {
            lsvViewDocGia.Items.Clear();
            DataTable data = new DocGia().LoadData();
            int i = 0;

            foreach (DataRow row in data.Rows)
            {
                lsvViewDocGia.Items.Add(row["MaDocGia"].ToString());

                lsvViewDocGia.Items[i].SubItems.Add(row["HoTenDocGia"].ToString());

                lsvViewDocGia.Items[i].SubItems.Add(row["NgaySinh"].ToString());

                lsvViewDocGia.Items[i].SubItems.Add(row["DiaChi"].ToString());

                lsvViewDocGia.Items[i].SubItems.Add(row["Email"].ToString());

                lsvViewDocGia.Items[i].SubItems.Add(row["NgayLapThe"].ToString());

                lsvViewDocGia.Items[i].SubItems.Add(row["NgayHetHan"].ToString());

                lsvViewDocGia.Items[i].SubItems.Add(row["TienNo"].ToString());

                i++;
            }
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            HienThi();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (new DocGia().ThemDocGia(txbTen.Text, dtpkNgaySinh.Value, txbAdress.Text, txbEmail.Text, dtpkNgayLap.Value, dtpkHetHan.Value, float.Parse(txbTienNo.Text)))
            {
                MessageBox.Show("Thêm thành công!");
                HienThi();
            }
            else
                MessageBox.Show("Thêm thất bại!");
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (new DocGia().XoaDocGia(int.Parse(txbID.Text)))
            {
                MessageBox.Show("Xóa thành công!");
                HienThi();
            }
            else
                MessageBox.Show("Xóa thất bại!");
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (new DocGia().SuaDocGia(txbID.Text,txbTen.Text, dtpkNgaySinh.Value, txbAdress.Text, txbEmail.Text, dtpkNgayLap.Value, dtpkHetHan.Value, float.Parse(txbTienNo.Text)))
            {
                MessageBox.Show("Sửa thành công!");
                HienThi();
            }
            else
                MessageBox.Show("Sửa thất bại!");
        }
    }
}

Code SQL:
CREATE DATABASE qltv_db
GO

USE qltv_db 
GO

CREATE TABLE DOCGia
(
 MaDocGia INT IDENTITY(1,1) PRIMARY KEY,
 HoTenDocGia NVARCHAR(30) NULL,
 NgaySinh DATE NULL,
 DiaChi NVARCHAR(30) NULL,
 Email CHAR(30) NULL,
 NgayLapThe DATE NULL,
 NgayHetHan DATE NULL,
 TienNo FLOAT NULL
)
GO

Project .NET (Phần mềm quản lý dự án)

Thứ Hai, 4 tháng 11, 2019 / No Comments
I. Hình ảnh demo:


- Phầm mềm thuộc sở hữu trí tuệ của QST Team!
- Viết trên farmework 4.5

II. Link tải:

Download OutSource: Click Download

III. Tài liệu về phần mềm (có hướng dẫn đọc code)

Tổng Hợp Bài Tập .NET

Chủ Nhật, 3 tháng 11, 2019 / No Comments


I. Phần mô tả



Tên Phiên Bản
Mô Tả
Link Download
Bản Dành Cho Sinh Viên
(sinh viên nên dùng bản này, đọc code sẽ dễ hiểu hơn)
Homework .NET - Beta 1.1.0
- Chỉ mói update xong 7 bài tập tuần 1
Homework .NET Beta 2.1.4
- Update thêm bài tập tuần 2
Homework dot Net Beta 3.1.2
- Update thêm bài tập tuần 3
Bản Dành Cho Giáo Viên
Homework dot Net Beta 3.5.6
- Updete tới tuần 4, Lap01 -> Lap 09








OutSource: Click Download
Pass: Connect to...

Code Advance

Thứ Sáu, 18 tháng 10, 2019 / No Comments

I - Di chuyển Panel

using System.Runtime.InteropServices;

        [DllImport("user32.dll", EntryPoint = "ReleaseCapture")]
        private extern static void ReleaseCapture();
        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        private extern static int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        
private void pnlTitle_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, 0x112, 0xf012, 0);
        }

II -Text center

lbName.Location = new Point((pnlAvartar.Width - lbName.Width)/2-5 , lbName.Location.Y);

III - DateTime

lbDongHo.Text = DateTime.Now.ToString("hh:mm tt");
 lbDongHo.Text = DateTime.Now.ToString("hh:mm ss");

IV-  Kiểm tra đầu vào có phải là chuỗi số hay không

public bool IsNumber(string pValue)
        {
            foreach (Char c in pValue)
            {
                if (!Char.IsDigit(c))
                    return false;
            }
            return true;
        }

V- 

Cách di chuyển form khi không có thanh tiêu đề (custom)

Thứ Hai, 30 tháng 9, 2019 / No Comments

Cách 1:

Sử dụng Windows API

Nguyên lý của cách này là giả lập thông điệp chuột được nhấn lên title bar của form và di chuyển. Để thực hiện ta cần dùng đến Windows API SendMessage() để gửi thông điệp đến cửa sổ cùng với hai tham số để xác định thông điệp là WM_NCLBUTTONDOWN và HTCAPTION.
Để khai báo hàm SendMessage(), ta cần thêm namespace System.Runtime.InteropServices và dùng attribute [DllImport]:
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);


sing System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
 
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        const int WM_NCLBUTTONDOWN = 0xA1;
        const int HT_CAPTION = 0x2;
 
        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
 
        public Form1()
        {
            InitializeComponent();
        }
 
        protected override void OnMouseDown(MouseEventArgs e)
        {
 
            if (e.Button == MouseButtons.Left)
            {
                this.Capture = false;
                SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
            base.OnMouseDown(e);
        }
 
    }
}


Cách 2:

Tính toán và thay đổi vị trí form theo tọa độ chuột

Phương pháp này rất đơn giản và không cần dùng đến Windows API. Ta chỉ cần lưu lại vị trí khi chuột nhấn xuống (MouseDown) và khi chuột di chuyển, ta sẽ tính toán lại vị trí form dựa vào sự thay đổi giữa vị trí mới và vị trí của của chuột. Sử dụng thêm một biến boolean isMouseDown để xác định trạng thái của chuột.

using System.Windows.Forms;
using System.Drawing;
 
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        bool isMouseDown;
 
        int xLast;
        int yLast;
 
        public Form1()
        {
            InitializeComponent();
        }
 
        protected override void OnMouseDown(MouseEventArgs e)
        {
            isMouseDown = true;
            xLast = e.X;
            yLast = e.Y;
 
            base.OnMouseDown(e);
        }
 
        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (isMouseDown)
            {
                int newY = this.Top + (e.Y - yLast);
                int newX = this.Left + (e.X - xLast);
 
                this.Location = new Point(newX, newY);
            }
 
            base.OnMouseMove(e);
        }
 
        protected override void OnMouseUp(MouseEventArgs e)
        {
            isMouseDown = false;
 
            base.OnMouseUp(e);
        }
 
    }
}

Cách 3:

Thay đổi thông điệp của cửa sổ

Phương pháp này được thực hiện bằng cách thay đổi thông điệp nhấn chuột trên client area (vùng trong cửa sổ trừ title bar) thành thông điệp trên title bar. Bằng cách override phương thức WndProc(), ta sẽ thay đổi property Message.Result từ HTCLIENT thành HTCAPTION. Property này tương ứng với tham số wParam trong hàm SensMessage().
Đây là cách tốt nhất và đơn giản nhất mà bạn nên áp dụng.

using System;
using System.Windows.Forms;
using System.Drawing;
 
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        const int WM_NCHITTEST = 0x84;
        const int HTCLIENT = 0x1;
        const int HTCAPTION = 0x2;
 
        public Form1()
        {
            InitializeComponent();
        }
        protected override void WndProc(ref Message message)
        {
            base.WndProc(ref message);
 
            if (message.Msg == WM_NCHITTEST && (int)message.Result == HTCLIENT)
                message.Result = (IntPtr)HTCAPTION;
        }
    }
}