简体   繁体   English

自定义文本编辑器上的彩色文本

[英]Colorful text on custom text editor

I've created a custom text editor with C# . 我已经使用C#创建了一个自定义文本编辑器。 Now, i would like to add the syntax highlighting feature by coloring the last letter/number that was inputted, in a way that the color that will be used is to be drawn on a random basis . 现在,我想通过为输入的最后一个字母/数字上色来添加语法突出显示功能,以使将要使用的颜色是随机绘制的。 How can i do so, though? 但是,我该怎么办呢? I tried a bunch of alternative ways but none worked . 我尝试了很多其他方法,但是都没有用。 Thanks for your recommendations! 感谢您的建议! (Note: I've been coding for 2 months. Sorry for any mistakes ! ) (注意:我已经编写了2个月的代码。对于任何错误,深表歉意!)

Latest example : 最新示例:

    private void userTB_KeyPress(object sender, KeyPressEventArgs e)
    {

        Random rnd = new Random();

        Color randomColor = Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255));

        userTB.SelectionColor = randomColor;

        }

Try putting "static" in front of your variable declaration. 尝试将“ static”放在变量声明的前面。

       static Random rnd = new Random();

Your code is getting fired off by a button event, and so the Random variable is getting created anew each time the button is clicked, and so it begins its random sequence at the same starting point each time. 您的代码被按钮事件触发,因此每次单击按钮时都会重新创建Random变量,因此每次都在同一起始点开始其随机序列。

===== Grayish? =====灰色? Hmm. Try this perhaps... 试试这个也许...

    static Random rnd = new Random();
    static int r1;
    static int r2;
    static int r3;

    r1 = rnd.Next(255);
    r2 = rnd.Next(255);
    r3 = rnd.Next(255);
    Color randomColor = Color.FromArgb(r1, r2, r3);

and take a look at the r1, r2, and r3 values in the debugger. 并查看调试器中的r1,r2和r3值。

. On My Point View, The Colorization Will Happen Via Code Behind As In Background Too... You Will Need a Classe Specialized On Read Each Word And Recognise What Each Word Do... 就我的观点而言,着色也会通过后台代码在背景中发生。您将需要专门研究读每个单词并识别每个单词做什么的类...

. Your Text Editor Is About What? 您的文本编辑器是关于什么的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM