- [POWER AUTOMATE] Hướng dẫn gởi tin nhắn zalo từ file Excel - No code
- [C#] Chia sẻ code lock và unlock user trong domain Window
- [SOFTWARE] Giới thiệu bộ phần mềm tính Kết Cấu Thép HatteSale, Mộng Đơn, Dầm, Sàn, Móng Cọc, Vách, Xà Gồ, Tính Tải Trọng
- [DEVEXPRESS] Vẽ Biểu Đồ Stock Chứng Khoán - Công Cụ Thiết Yếu Cho Nhà Đầu Tư trên Winform
- [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#] Cách tạo font, color random in RichtextBox
Bài viết hôm nay, mình tiếp tục chia sẽ cho các bạn source code cách tạo random font chữ, màu sắc vào RichTextBox C#.
Ở ứng dụng này, mình sẽ bắt sự kiện Text Change khi nhập text vào ô input, và mình sẽ lấy chữ vừa gõ thay đổi random font chữ và color (màu sắc).
Giao diện demo ứng dụng:
Source code C#:
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 howto_ransom_note_rtf
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string[] FontNames =
{
"Times New Roman",
"Courier New",
"Comic Sans MS",
"Arial",
"Tahoma",
"AvantGarde Md BT",
"Benguiat Bk BT",
"Bookman Old Style",
"Bremen Bd BT",
"Century Gothic",
"Dauphin",
"Curlz MT",
"GoudyHandtooled BT",
};
private Color[] FontColors =
{
Color.Red,
Color.Green,
Color.Blue,
Color.Orange,
Color.Brown,
Color.Magenta,
Color.Purple,
Color.BurlyWood,
Color.HotPink,
};
private Random Rand = new Random();
private void Form1_Load(object sender, EventArgs e)
{
DrawText();
}
private void txtText_TextChanged(object sender, EventArgs e)
{
DrawText();
}
private void DrawText()
{
rchText.Text = "";
foreach (char ch in txtText.Text)
{
DrawCharacter(ch);
}
}
private void DrawCharacter(char ch)
{
const float min_size = 25;
const float max_size = 35;
string font_name = FontNames[Rand.Next(0, FontNames.Length)];
float font_size = (float)(min_size + Rand.NextDouble() * (max_size - min_size));
FontStyle font_style = FontStyle.Regular;
if (Rand.Next(0, 2) == 1) font_style |= FontStyle.Bold;
if (Rand.Next(0, 2) == 1) font_style |= FontStyle.Italic;
rchText.SelectionFont = new Font(font_name, font_size, font_style);
rchText.SelectionColor = FontColors[Rand.Next(0, FontColors.Length)];
rchText.AppendText(ch.ToString());
}
}
}
HAVE FUN