- [C#] Ẩn ứng dụng winform từ Apps vào Background Process trên Task Manager
- [SQLSERVER] Xử lý ngoại lệ sử dụng TRY...CATCH và RAISEERROR
- [C#] Bắt sự kiện bàn phím chuột bên ngoài ứng dụng winform sử dụng thư viện MouseKeyHook
- [DEVEXPRESS] Đặt mật khẩu và bỏ mật khẩu tập tin file PDF
- [C#] Thêm ứng dụng vào Taskbar sử dụng SharpShell DeskBand
- [C#] Hướng dẫn thêm text vào hình ảnh icon winform
- [C#] Chia sẽ tổng hợp source code đồ án về Csharp
- [C#] Hướng dẫn viết ứng dụng quay màn hình video winform, Screen Recorder
- [C#] Hướng dẫn sử dụng thư viện Input Simulator để làm việc với Keyboard, Mouse Virtual
- [DEVEXPRESS] Hướng dẫn tích Filter Contain khi click chuột phải vào cell selection trên Gridview
- [C#] Tra cứu mã số thuế cá nhân bằng CMND hoặc CCCD
- [C#] Convert hình ảnh image thành Blurhash sử dụng trong loading image winform
- [POWERSHELL] Script sao lưu backup và nén database sqlserver
- [C#] Giới thiệu thư viện Autofac Dependency Injection
- [C#] Hướng dẫn tạo Windows Services đơn giản Winform
- [C#] Một click chuột điều khiển máy tính từ xa sử dụng Ultraviewer
- Hướng dẫn đóng gói phần mềm sử dụng Powershell biên dịch script thành file exe
- [C#] Hướng dẫn sử dụng Task Dialog trên NET 5
- [C#] Hướng dẫn xem lịch sử các trang web đã truy cập trên Chrome Browser
- [C#] Hướng dẫn lấy thông tin Your ID và Password của Ultraviewer Winform
[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