Good day. I want to make a program that would have text abbreviations like ":), :(" and so on by clicking on the button on the form translated into emoticons. I added the usual textbox to the incoming data acceptance form, but to display the emoticons themselves, I have a component RichTextBox. I have a set of emoticons static in the sense that these are just png-format pictures. I have to say, I tried to find such information and found it. Inserting Smiles

But I do not quite understand which part of the code to put. Yes, and I have some other requirements. It was after clicking on the button that such a replacement would be carried out and placed in the RichEditBox.

    1 answer 1

    Good evening, in fact in the example that you left the link there is all the necessary code to implement your goal. After reading your requirements I have compiled a small form.

    Application form

    After taking the source code from the example a bit of his corrections and got the desired result. The logic here is this: the entire text from textBox1 was transferred to richTextBox1, then the very richTextBox1 itself replaces characters with pictures (smiles), you can also clear textBox1, but this is up to you. enter image description here enter image description here

    I hope this will satisfy your requirements. I will leave the whole code below. Ask questions, try to answer.

    private void add_smileys(TextBox tb,RichTextBox rtb) { var smileys = new Dictionary<string, Image>() { { @":)", Resources.in_smile }, { @">:(", Resources.in_angry }, }; //Изменения здесь. Очищаем поле richTextBox1 перед его использованием richTextBox1.Text = null; //Вставляем текст из textBox1 в richTextBox1 rtb.Text = tb.Text; foreach (var smiley in smileys) { add_smiley(rtb, smiley.Key, smiley.Value); } } private void add_smiley(RichTextBox rtb, string token, Image smiley) { while (true) { var selectionStart = rtb.Find(token, RichTextBoxFinds.WholeWord); if (selectionStart < 0) break; try { rtb.SelectionStart = selectionStart; rtb.SelectionLength = token.Length; Clipboard.SetImage(smiley); rtb.Paste(); } catch (Exception ex) { MessageBox.Show(ex.Message); break; } } } private void button1_Click(object sender, EventArgs e) { add_smileys(textBox1,richTextBox1); } 
    • Thanks :) The only thing is still there. Appeal to resources to emoticons themselves. Through Properties.Resources, this is how I see emoticons. Maybe I'm not right to add something? - Petr
    • And what resources did you add to? which is in the form itself or common in the project? But in general it seems to be not critical, indicate the link just to Properties - John Kravchenko
    • through the properties of the project I throw. But I think this is not critical. Since everything works fine from Properties. Thanks again for the help :) - Petr