I'm making a word processor, and want the user to have the ability to bold only the text you select on windows phone with the tap/hold/drag. I know how to detect what text is selected by the user with Filebox.SelectedText (filebox is the name of the textbox), but have no idea where to go from there - how do I take the text that is selected and ONLY make that selected text bolded?
Note: I am using ComponentOne's Rich Textbox control.
You can make use of the RichTextBox.Selection property.
For example:
private void ModifySelectedText()
{
// Determine if text is selected in the control.
if (richTextBox1.SelectionLength > 0)
{
// Set the color of the selected text in the control.
richTextBox1.SelectionColor = Color.Red;
// Set the font of the selected text to bold and underlined.
richTextBox1.SelectionFont = new Font("Arial",10,FontStyle.Bold | FontStyle.Underline);
// Protect the selected text from modification.
richTextBox1.SelectionProtected = true;
}
}
Related
I am attempting to highlight or set the background of some selected text in a WPF TextBlock.
Say I have 2 text files that I load into memory, complete a diff, and then want to diplay in a WPF App. Imagine looping through each line and then appending text to the textblock and changing color based on Deleted, inserted, or equal text.
for (int i = 0; i < theDiffs.Count; i++)
{
switch (theDiffs[i].operation)
{
case Operation.DELETE:
// set color to red on Source control version TextBlock
break;
case Operation.INSERT:
WorkspaceVersion.AppendText(theDiffs[i].text);
// set the background color (or highlight) of appended text to green
break;
case Operation.EQUAL:
WorkspaceVersion.AppendText(theDiffs[i].text);
// Set the background color (highlight) of appended text to yellow
break;
default:
throw new ArgumentOutOfRangeException();
}
}
You'll want to append Run inline elements to the TextBlock Inlines. Eg (assuming "WorkspaceVersion" is a TextBlock):
case Operation.INSERT:
// set the background color (or highlight) of appended text to green
string text = theDiffs[i].text;
Brush background = Brushes.Green;
var run = new Run { Text = text, Background = background };
WorkspaceVersion.Inlines.Add(run);
break;
I'm using the RichEditBox to build a simple editor.
I found a piece of code which toggles bold text on a selection within the document window
private void RichEditBox_KeyDown(object sender, KeyRoutedEventArgs e)
{
var state = Window.Current.CoreWindow.GetKeyState(Windows.System.VirtualKey.Control);
if ((state & CoreVirtualKeyStates.Down) == CoreVirtualKeyStates.Down)
{
if (e.Key == Windows.System.VirtualKey.B)
{
if (Editor.Document.Selection.Text.Length > 0)
{
ITextCharacterFormat format = Editor.Document.Selection.CharacterFormat;
format.Bold = FormatEffect.On;
}
else
{
// CTRL + B should toggle bold mode on or off here.
}
}
}
}
When I highlight a piece of text, and press CTRL+B, it bolds the text. Result. However, anything I type after that point is also in bold.
This is not what I expected. According to the code above, I'm affecting the format of the Selection only.
When I select some text and press CTRL+B it should toggle bold formatting on that selection and leave the default format as is.
I've tried using FormatEffect.Toggle
format.Bold = FormatEffect.Toggle
I've tried saving out the Document Character format first, then reapplying
ITextCharacterFormat original_format = Editor.Document.GetDefaultCharacterFormat();
ITextCharacterFormat format = Editor.Document.Selection.CharacterFormat;
format.Bold = FormatEffect.On;
Editor.Document.SetDefaultCharacterFormat(original_format);
This should reset the default back to what it was after bolding. But it doesn't
I could set the selection to nothing, then set format.Bold = FormatEffect.Off again, then reselect the text, but that seems like the long way around (and it probably won't work). There must be a simple way to do this?
NOTE: I have tagged this with the RichTextBox tag as there is no RichEditBox tag. Can someone with >1500 rep add one?
That's the normal behaviour when using SelectionFont and setting bold style with richtextbox.
If the current text selection has more than one font specified, this
property is null. If no text is currently selected, the font specified
in this property is applied to the current insertion point and to all
text that is typed into the control after the insertion point.
It's probably the same issue that you have.
Wordpad and Word work this way too.
I have found a hack, that works. I'm posting this as an answer for anyone who's stuck, but I'm not accepting the answer, because there must be a better one.
private void RichEditBox_KeyDown(object sender, KeyRoutedEventArgs e)
{
var state = Window.Current.CoreWindow.GetKeyState(Windows.System.VirtualKey.Control);
if ((state & CoreVirtualKeyStates.Down) == CoreVirtualKeyStates.Down)
{
if (e.Key == Windows.System.VirtualKey.B)
{
if (Editor.Document.Selection.Text.Length > 0)
{
// text is selected. make it bold
ITextCharacterFormat format =
Editor.Document.Selection.CharacterFormat;
format.Bold = FormatEffect.On;
var start_pos = Editor.Document.Selection.StartPosition;
Editor.Document.Selection.StartPosition =
Editor.Document.Selection.EndPosition;
format.Bold = FormatEffect.Off;
// Editor.Document.Selection.StartPosition = start_pos;
// this is where I was re-selecting the text after switching bold OFF
// but doing so switches it back on again. which makes no sense
}
else
{
// no text selected. just enable bold mode
ITextCharacterFormat format =
Editor.Document.Selection.CharacterFormat;
format.Bold = FormatEffect.Toggle;
}
}
}
}
This isn't perfect, because after you've selected and bolded the text it is automatically de-selected. However in practice I find that this actually works fine for me. Still, it feels like a hack, because it is a hack
I'm trying to create a chat application like msn. When i do "textBox.Text = textBox.Text+text" it updates the textbox and the text i got selected is no longer selected. In MSN you can have selected text and still recieve messages in different colors etc.. How do they do it? I figure its something like push messages, maybe they create a new textbox under another textbox? Any clues?
I hope you guys know what i'm talking about here. I just want my text to behave like MSN used to do, not update the whole textbox, just push a new message under the current message etc.
If I understand your question, you just want text to stay selected when you append messages to a RichTextBox?
int selectionStart = textBox.SelectionStart;
int selectionLength = textBox.SelectionLength;
int carat = textBox.TextLength;
textBox.Text += Environment.NewLine;
textBox.Text += newText;
//optional styling code for newly appended text
textBox.Select(carat, newText.Length);
textBox.SelectionColor = //value;
//etc.
//reapply original selection
if(selectionStart >= 0 && selectionLength > 0)
{
textBox.Select(selectionStart, selectionLength);
}
I want to change font style for a textbox when I click the button.For this my code is below and it is okay;
protected void Button1_Click(object sender, System.EventArgs e)
{
TextBox1.Font.Size = FontUnit.XLarge;
TextBox1.ForeColor = System.Drawing.Color.Crimson;
TextBox1.BackColor = System.Drawing.Color.Snow;
TextBox1.BorderColor = System.Drawing.Color.HotPink;
}
But Can I do where I select of write which is in the textbox it changes only that part.For example textbox1.Text="Computer Programs" and the user select only "Computer" part of textbox1.Only "Computer" part must be changed.
From the sounds of it you want to format only part of the text? For that you'll need to look at a RichTextBox control.
With a RichTextBox you can make use of text selections and set formatting on only those areas:
RichTextBox rich = new RichTextBox();
rich.Text = "Here is some text for the Rich Text Box";
rich.SelectionStart = 0;
rich.SelectionLength = 4;
rich.SelectionFont = new Font(rich.Font, FontStyle.Bold);
After you're done setting your font to your selection you should set the font immediately afterwards back to the original so that you don't continue the style outside the area you wanted to apply it:
rich.SelectionStart = rich.SelectionStart + rich.SelectionLength;
rich.SelectionLength = 0;
rich.SelectionFont = rich.Font;
This should result in "Here is some text for the Rich Text Box" changing to look like: "Here is some text for the Rich Text Box".
In Visual C#.NET:
How do I add/append text directly to where the user's cursor is in a Rich Text Box?
For example, if the user clicked a button, and their cursor was somewhere in the rich text box, text would be immediately added to the location of their cursor.
Use the SelectedText property:
textBox.SelectedText = "New text";
This will overwrite any selected text they have though. If you don't want that you can first set the SelectionLength property to 0:
textBox.SelectionLength = 0;
textBox.SelectedText = "New text";
rtb.SelectionStart += rtb.SelectionLength;
rtb.SelectionLength = 0;
rtb.SelectedText = "asdf";
This moves the cursor just past the end of the current selection, then adds "asdf" at the end.