NEWS

[C#] Cách tạo font, color random in RichtextBox

[C#] Cách tạo font, color random in RichtextBox
Đăng bởi: Thảo Meo - Lượt xem: 8143 08:47:35, 28/08/2018C#   In bài viết

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:

random text, color, font C#

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 heart

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Cách tạo font, color random in RichtextBox
Đăng bởi: Thảo Meo - Lượt xem: 8143 08:47:35, 28/08/2018C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.