Click here to Skip to main content
15,867,937 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am trying to figure out how to select characters and change the color without changing the cursor position, that means no using the RichTextBox.Select method. Any ideas?

-Jordan
Posted

You can do it without using the said method, but other approaches (using selection properties) will be equivalent. Like it or not, the concept of selection is bound with the caret position, you cannot operate them independently.

And this is how all text editors and word processors work these days. You can change it only if you develop your own conceptually different editor by yourself, from scratch.

—SA
 
Share this answer
 
Comments
Sicppy 27-Aug-13 12:48pm    
Alright this brings me to another question, how would i go about creating a windows forms object in c#?
Sergey Alexandrovich Kryukov 27-Aug-13 13:40pm    
How it could be a problem?

MyForm : System.Windows.Forms.Form {/* ... */}

//...

MyForm myForm = new MyForm();

If something is unclear, this would be a matter of a separate question.

But how about first accepting this answer formally (green button)?

—SA
Sicppy 27-Aug-13 15:03pm    
What I mean is, how would i go about creating a new type of object, so i could create a custom richtextbox
Sergey Alexandrovich Kryukov 27-Aug-13 21:57pm    
It won't help you. You only can create a fresh new type nearly from scratch, from the base type System.Windows.Forms.Control or System.Windows.Forms.ScrollableControl control. I won't advise you doing it, it would be too big work.
—SA
Sicppy 30-Aug-13 21:01pm    
I need to make a new control because i need to be able to change the color of the text without changing the cursor position.
I don't see any conceptual barrier to your getting this done: here's a simple demonstration that requires a RichTextBox, and a Button on a Form.

Wire-up the RichTextBox's SelectionChanged EventHandler as shown here, and the Button's Click EventHandler as shown here:
C#
//keep track of the selection start and end points
private int selStart;

// note we don't make use of this in this example, but we could
// in some future use case
private int selEnd;

private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
    selStart = richTextBox1.SelectionStart;
    selEnd = selStart + richTextBox1.SelectionLength;
}

private void button1_Click(object sender, EventArgs e)
{
    // set the ForeColor of selected Text
    richTextBox1.SelectionColor = Color.Red;
    
    // put the focus back on the RichTextBox
    richTextBox1.Focus();

    // if we exited here, the existing selection would remain unchanged

    // restore the insert caret to the selection start
    richTextBox1.SelectionStart = selStart;

    // make sure there's nothing selected now
    richTextBox1.SelectionLength = 0;
}
You can do whatever you like after you have set Color, Font, or whatever, to the current Selection.

When you are done you can do nothing which would leave the current selection as-is; or, as shown here, set the insert caret at the start and have no selection; or, use the other variable 'selEnd to set the insertion caret at the end of the selection, and so on.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 30-Aug-13 23:52pm    
This is not what OP initially wanted. This code will change some color, but not selection without moving the caret.
Please see my answer.
However, in the latest comment, OP started to talk about change of color, not selection. Who knows what this person really wants?
—SA
Sergey Alexandrovich Kryukov 4-Sep-13 13:16pm    
As OP recently confirmed that only the highlighting is needed, not selection, I voted 5 for your answer. Let's think you managed to read the question between lines... :-)
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900