csnoob
csnoob

Reputation: 73

Colorful text on custom text editor

I've created a custom text editor with 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 ! )

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;

        }

Upvotes: 0

Views: 270

Answers (2)

Warren Stephens
Warren Stephens

Reputation: 92

Try putting "static" in front of your variable declaration.

       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.

===== 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.

Upvotes: 1

Eleandro Duzentos
Eleandro Duzentos

Reputation: 23

. 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?

Upvotes: 0

Related Questions