- [C#] Hướng dẫn bảo mật ứng dụng 2FA (Multi-factor Authentication) trên Winform
- [C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform
- [C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform
- Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin
- Hướng dẫn cách tạo Product Id cho ứng dụng phần mềm XXXXX-XXXXX-XXXXX-XXXXX
- [SQLSERVER] Hướng dẫn tạo script sql từ ứng dụng Sqlserver management Studio
- [C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform
- [VB.NET] Hướng dẫn lấy thông tin tài khoản đăng nhập windows và khởi động lại ứng dụng ở chế độ Administrator
- [C#] Sử dụng thư viện Polly gửi lại request api khi request bị lỗi hay rớt mạng
- [DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform
- [DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
- [C#] Tạo form đăng nhập và đăng ký với hiệu ứng Sliding Animation Effect
- [C#] Hướng dẫn tạo thanh toán đơn hàng qua mã vạch VietQR sử dụng API PayOS hoàn toàn miễn phí
- [C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog
- [C#] Hướng dẫn cách tạo mã QR Code trên file Excel
- [VB.NET] Gởi tin nhắn và file đính kèm qua ứng dụng gởi tin nhắn Whats App
- [C#] Viết ứng dụng Auto Fill list Textbox from clipboard Winform
- [TOOL] Chia sẻ phần mềm thay đổi thông tin cấu hình máy tính
- [C#] Hướng dẫn Export dữ liệu ra file Microsoft Word Template
- [C#] Chia sẻ source code tool kiểm tra domain website
[C#] Hướng dẫn soạn thảo các biểu thức toán học vào lưu vào dữ liệu Sqlserver
Hôm trước, có một bạn hỏi mình cách làm thế nào để soạn thảo đề thi toán học trên TextBox và lưu trữ dữ liệu của các câu hỏi biểu thức toán học vào SQLSERVER.
[C#] Tutorial Equation Editor Winform Save to Sqlserver
Như các bạn đã biết, để soạn thảo được toán học, cần insert các biểu thức toán học vào, và tuy nhiên trên công cụ soạn thảo mặc định của Winform chỉ có 2 loại là TextBox và RichtextBox.
Và cả hai công cụ này đều không thể cho phép chúng ta chèn các ký hiệu toán học vào được.
Dưới đây, mình sẽ hướng dẫn các bạn giải pháp để tạo các biểu thức toán học.
1. Sử dụng RichTextBox để hiển thị các công thức toán học.
2. Khi tạo câu hỏi mới, chúng ta sẽ tạo ra 1 file rtf và mở ứng dụng winword để đọc file này lên, và soạn thảo trực tiếp trên công cụ này, vì Win word có hỗ trợ chúng ta soạn thảo các công thức toán học.
3. Sử dụng File System Watcher trong C#, để khi dữ liệu thay đổi trên WinWord, chúng ta sẽ bắt sự kiện vào lấy dữ liệu từ Trình soạn thảo về RichTextBox của mình.
Dưới đây là giao diện soạn thảo Toán học C#:
1. Tạo cơ sở dữ liệu với tên equation
và bảng table_data
như hình bên dưới:
Mình tạo demo 2 trường dữ liệu:
- Id với kiểu dữ liệu varchar
- Data với kiểu dữ liệu ntext
Video demo ứng dụng:
Full source code C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Equation_Editor
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string filename = "";
sqlserver provider = new sqlserver(".", "sa", "minh123", "equation");
private void btn_soanthao_Click(object sender, EventArgs e)
{
filename = $@"{txt_id.Text}.rtf";
CreateEmptyFile(filename);
fileSystemWatcher.Path = Application.StartupPath;
fileSystemWatcher.NotifyFilter = NotifyFilters.Attributes |
NotifyFilters.CreationTime |
NotifyFilters.DirectoryName |
NotifyFilters.FileName |
NotifyFilters.LastAccess |
NotifyFilters.LastWrite |
NotifyFilters.Size;
fileSystemWatcher.Changed += new FileSystemEventHandler(OnChanged);
fileSystemWatcher.EnableRaisingEvents = true;
Process.Start("winword", filename);
}
public void CreateEmptyFile(string filename)
{
try
{
File.Create(filename).Dispose();
}
catch (Exception ex)
{
MessageBox.Show("Lỗi: " + ex.Message);
}
}
private void OnChanged(object source, FileSystemEventArgs e)
{
if (e.FullPath == Application.StartupPath + "\" + filename)
{
if (File.Exists(e.FullPath))
{
rtb_data.LoadFile(filename);
}
}
}
private void btn_luu_Click(object sender, EventArgs e)
{
SqlConnection cnn;
string connetionString = "Data Source=.;Initial Catalog=equation;User ID=sa;Password=minh123";
cnn = new SqlConnection(connetionString);
cnn.Open();
string sSQL = "INSERT INTO tbl_data (id, data) VALUES (@id, @RTF)";
SqlCommand clsCommand = new SqlCommand(sSQL, cnn);
clsCommand.Parameters.Add("@id", txt_id.Text);
clsCommand.Parameters.Add("@RTF", rtb_data.Rtf);
clsCommand.ExecuteNonQuery();
cnn.Close();
File.Delete(Application.StartupPath + "\" + filename);
Form1_Load(sender, e);
}
private void Form1_Load(object sender, EventArgs e)
{
var table = provider.ExecuteQuery("select * from tbl_data");
dataGridView1.DataSource = table;
txt_id.DataBindings.Clear();
rtb_data.DataBindings.Clear();
txt_id.DataBindings.Add("text", table, "id");
rtb_data.DataBindings.Add("rtf", table, "data");
}
private void btn_xoa_Click(object sender, EventArgs e)
{
provider.ExecuteNonQuery($"delete from tbl_data where id = '{txt_id.Text}'");
Form1_Load(sender, e);
}
private void btn_Them_Click(object sender, EventArgs e)
{
txt_id.Text = "";
rtb_data.Text = "";
txt_id.Focus();
}
}
}
HAPPY CODING