C# RichTextBox Bold, Italic, Markup butonu nasıl yapılır?

246941

Petapat
İlk 5 Haneli Mesajınız!
Katılım
30 Ocak 2019
Mesajlar
21.099
Makaleler
9
Çözümler
222
@300319 Yardım edersen sevinirim. Oldukça basit gibi duruyor yapması ama kafam basmadı bir türlü.

Yapmak istediğimi söyleyeyim.

Örneğin <b> test </b> merhaba <i>dünya</i> yazdım. Bunu bir buton basınca böyle görünmesini istiyorum.

Örneğin test merhaba dünya şeklinde çıkmasını istiyorum RichTextBox ile.
 

Kod:
private void boldButton_Click(object sender, EventArgs e) { // Seçili metni kalınlaştır richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, FontStyle.Bold); }

private void italicButton_Click(object sender, EventArgs e) { // Seçili metni eğik hale getir richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, FontStyle.Italic);

Markupu bulmaya calısıyorum

Kod:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>

void ApplyMarkup(HWND hwnd)
{
    // Seçili metnin biçimlendirme işlemi yapılacak
    // RTF biçimindeki biçimlendirme belirteçlerini kullanarak biçimlendirme uygula

    // Örnek RTF belirteçleri: \b kalın, \i eğik, \u altı çizili
    char* bold = "\\b";
    char* italic = "\\i";
    char* underline = "\\u";

    // Seçili metni al
    int start, end;
    SendMessage(hwnd, EM_GETSEL, (WPARAM)&start, (LPARAM)&end);
    char buffer[1024];
    SendMessage(hwnd, EM_GETSELTEXT, 0, (LPARAM)buffer);

    // RTF belirteçlerini metne ekle
    char* result = (char*)malloc(strlen(buffer) * 2);
    strcpy(result, "");
    strcat(result, bold);
    strcat(result, italic);
    strcat(result, italic);
    strcat(result, bold);

    // Seçili metni RTF biçiminde yenisiyle değiştir
    SendMessage(hwnd, EM_REPLACESEL, 0, (LPARAM)result);

    free(result);
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CREATE:
        {
            // Markup düğmesini oluştur
            CreateWindow("BUTTON", "Markup", WS_VISIBLE | WS_CHILD, 10, 10, 100, 30, hwnd, (HMENU)1, NULL, NULL);
            break;
        }
        case WM_COMMAND:
        {
            switch(LOWORD(wParam))
            {
                case 1:
                    // Markup düğmesine tıklandığında, seçili metni biçimlendir
                    ApplyMarkup(hwnd);
                    break;
            }
            break;
        }
        case WM_DESTROY:
        {
            PostQuitMessage(0);
            break;
        }
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASS wc = {0};
    wc.lpfnWndProc = WndProc;
    wc.hInstance = hInstance;
    wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);
    wc.lpszClassName = "MarkupButton";
    RegisterClass(&wc);

    HWND hwnd = CreateWindow("MarkupButton", "Markup Button", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,

Tam olarak istediğini anlayamadım ama markup butonu ve italic bold richtextbox ile yazı oluşturma kodlarını attım
 
C++ kodu neden attınız?

BBCode'u görüntüye çevirmek istiyorum. <b> </b> arasına yazılan metin kalın olacak butona bastığımda.
 
C++ kodu neden attınız?

BBCode'u görüntüye çevirmek istiyorum. <b> </b> arasına yazılan metin kalın olacak butona bastığımda.

Kod:
private void btnBold_Click(object sender, EventArgs e)
{
 // Seçili metnin stilini kalın yap
 string text = richTextBox1.SelectedText;
 richTextBox1.SelectedText = "[b]" + text + "[/b]";
}
 
Kod:
private void btnBold_Click(object sender, EventArgs e)
{
 // Seçili metnin stilini kalın yap
 string text = richTextBox1.SelectedText;
 richTextBox1.SelectedText = "[b]" + text + "[/b]";
}

CSS değil bu.

@246941

C#:
        private void button1_Click(object sender, EventArgs e)
        {
            richTextBox1.Font = new Font(richTextBox1.Font, FontStyle.Italic);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            richTextBox1.Font = new Font(richTextBox1.Font, FontStyle.Bold);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            richTextBox1.Text = "Technopat Sosyal";
        }


 
Son düzenleme:
C#:
        private void styleText()
        {
            int n1 = 0; int n2 = richTextBox1.Text.Length;
            while ((n1 = richTextBox1.Text.IndexOf("[b]", n1)) != -1 && (n2 = richTextBox1.Text.IndexOf("[/b]", n1)) != -1)
            {
                richTextBox1.Select(n1 + 3, n2 - n1 - 3);
                using (var boldFont = new Font(richTextBox1.SelectionFont, FontStyle.Bold))
                    richTextBox1.SelectionFont = boldFont;

                n1 = n2;
            }
            richTextBox1.Rtf = richTextBox1.Rtf.Replace("[b]", string.Empty);
            richTextBox1.Rtf = richTextBox1.Rtf.Replace("[/b]", string.Empty);

            n1 = 0;
            n2 = richTextBox1.Text.Length;
            while ((n1 = richTextBox1.Text.IndexOf("[i]", n1)) != -1 && (n2 = richTextBox1.Text.IndexOf("[/i]", n1)) != -1)
            {
                richTextBox1.Select(n1 + 3, n2 - n1 - 3);
                using (var italicFont = new Font(richTextBox1.SelectionFont, FontStyle.Italic))
                    richTextBox1.SelectionFont = italicFont;

                n1 = n2;
            }
            richTextBox1.Rtf = richTextBox1.Rtf.Replace("[i]", string.Empty);
            richTextBox1.Rtf = richTextBox1.Rtf.Replace("[/i]", string.Empty);

            n1 = 0;
            n2 = richTextBox1.Text.Length;
            while ((n1 = richTextBox1.Text.IndexOf("[s]", n1)) != -1 && (n2 = richTextBox1.Text.IndexOf("[/s]", n1)) != -1)
            {
                richTextBox1.Select(n1 + 3, n2 - n1 - 3);
                using (var strikeOut = new Font(richTextBox1.SelectionFont, FontStyle.Strikeout))
                    richTextBox1.SelectionFont = strikeOut;

                n1 = n2;
            }
            richTextBox1.Rtf = richTextBox1.Rtf.Replace("[s]", string.Empty);
            richTextBox1.Rtf = richTextBox1.Rtf.Replace("[/s]", string.Empty);
        }

İstediğimi bu fonksiyonla yaptım. Döngüleri düzeltmem lazım ancak şu anlık işimi gördü.
 


Bu kadar uzun koda gerek yok bence.
 
Bu kadar uzun koda gerek yok bence.
C#:
        private void styleText()
        {
            string[] types = {"[b]","[i]", "[s]", "[u]"};
            string[] types2 = { "[/b]", "[/i]", "[/s]", "[/u]" };
            for (int i = 0; i< types.Count(); i++)
            {
                int n1 = 0; int n2 = richTextBox1.Text.Length;
                while ((n1 = richTextBox1.Text.IndexOf(types[i], n1)) != -1 && (n2 = richTextBox1.Text.IndexOf(types2[i], n1)) != -1)
                {
                    richTextBox1.Select(n1 + 3, n2 - n1 - 3);
                    if(i == 0)
                    {
                        using (var boldFont = new Font(richTextBox1.SelectionFont, FontStyle.Bold)) richTextBox1.SelectionFont = boldFont;
                    }
                    else if(i == 1)
                    {
                        using (var italicFont = new Font(richTextBox1.SelectionFont, FontStyle.Italic)) richTextBox1.SelectionFont = italicFont;
                    }

                    else if (i == 2)
                    {
                        using (var strikeFont = new Font(richTextBox1.SelectionFont, FontStyle.Strikeout)) richTextBox1.SelectionFont = strikeFont;
                    }
                    else if (i == 3)
                    {
                        using (var underlineFont = new Font(richTextBox1.SelectionFont, FontStyle.Underline)) richTextBox1.SelectionFont = underlineFont;
                    }
                    n1 = n2;
                }
                richTextBox1.Rtf = richTextBox1.Rtf.Replace(types[i], string.Empty);
                richTextBox1.Rtf = richTextBox1.Rtf.Replace(types2[i], string.Empty);
            }
        }

Bu şekilde bayağı kısalttım.

Kodu kullanmak isteyen varsa types kısmında yeni meta kodunu girip else if kısmına ekleme yapabilir.
 

Evet, bu şekilde daha iyi olmuş.
 
Bu siteyi kullanmak için çerezler gereklidir. Siteyi kullanmaya devam etmek için çerezleri kabul etmelisiniz. Daha Fazlasını Öğren.…