Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Rich text box selection problem
 

Rich text box selection problem

Hi, i have noticed that when i have text in the RTB, and during runtime, i want to be able to select the text i desire at the specific text itself, and not being able to select the text while i am in the whitespace of the RTB, is this possible?

Another question is this, my RTB is in a form, and whenever i select text inside the RTB, and my mouse leaves the RTB control area, and it goes back in, and i select some new text, the select highlights the old selected text that i selected before leaving the control. Just wondering if its just my computer or is this just a code problem? Was trying to find a way to reset the old selected text so it wont remember what i selected once i mouse up, i tried using DeselectAll() for the RTB but it dosent work at times, please advise thanks!
systemX  Thursday, February 14, 2008 8:26 AM

You can manipulate the RichTextBox.SelectionStart and RichTextBox.SelectionEnd properties to customize your selection. Here’s an example, it only allows the user to select from the 20th character to the 30th character.

Code Snippet

private void richTextBox1_SelectionChanged(object sender, EventArgs e)

{

// Only allows to select from 20th character to 30th character

int allowedStartIndex = 20;

int allowedEndIndex = 30;

int selectionStart = richTextBox1.SelectionStart;

int selectionEnd = richTextBox1.SelectionStart + richTextBox1.SelectionLength;

// starts at 15

if (selectionStart < allowedStartIndex)

{

// ends at 18

if (selectionEnd < allowedStartIndex)

{

richTextBox1.SelectionLength = 0;

}

// ends at 25

else if (selectionEnd <= allowedEndIndex)

{

richTextBox1.SelectionStart = allowedStartIndex;

richTextBox1.SelectionLength = selectionEnd - allowedStartIndex;

}

// ends at 31

else //if (selectionEnd > allowedEndIndex)

{

richTextBox1.SelectionStart = allowedStartIndex;

richTextBox1.SelectionLength = allowedEndIndex - allowedStartIndex;

}

}

// starts at 25

else if (selectionStart <= allowedEndIndex)

{

// ends at 31

if (selectionEnd > allowedEndIndex)

{

richTextBox1.SelectionLength = allowedEndIndex - selectionStart;

}

// ends at 18

else if (selectionEnd < allowedStartIndex)

{

richTextBox1.SelectionLength = selectionStart - allowedStartIndex;

}

}

// starts at 35

else // if(selectionStart > allowedEndIndex)

{

// end at 31

if (selectionEnd > allowedEndIndex)

{

richTextBox1.SelectionLength = 0;

}

// ends at 28

else if (selectionEnd >= allowedStartIndex)

{

richTextBox1.SelectionStart = allowedEndIndex;

richTextBox1.SelectionLength = allowedEndIndex - selectionEnd;

}

// ends at 18

else // if (selectionEnd < allowedStartIndex)

{

richTextBox1.SelectionStart = allowedEndIndex;

richTextBox1.SelectionLength = allowedEndIndex - allowedStartIndex;

}

}

}

Jacob Sui - MSFT  Wednesday, February 20, 2008 9:15 AM

For your second problem, i think it's your windows' theme problem. Anwindows form application adopts to the current users theme. It has nothing to do with code.

Jacob Sui - MSFT  Wednesday, February 20, 2008 9:20 AM
hi jacob, i tried your code, it didnt seem to be what i was looking for, although this will be something useful in the future Smile. What i really meant in my earlier question was, how do i disable the user from selecting text from the whitespace that can be found in the rich text box itself? This means, the user is selecting the text when he clicks on the whitespace and not actually clicking on the text itself to select it. Is there a way to detect a user clicking the whitespace region and then disabling the user from doing anything in the whitespace?
systemX  Wednesday, February 20, 2008 5:30 PM

Which whitespace? Is it the white space sits between your content or the white space after your content? You wouldn't be able to select the white space after your content at all.

Jacob Sui - MSFT  Thursday, February 21, 2008 5:16 AM

You can use google to search for other answers

Custom Search

More Threads

• Collection Editor of Delegate
• &Key Do not Show at Run Time
• Controls position on window resizings...
• CollectionEditor questions re: Names in left pane
• Problem binding usercontrol's collection
• Container Control
• Continuous Forms
• default toolbox tab for usercontrol
• How create the Task Controls like the ones in XP.
• Custom SaveAs Dialog