WPF: RichTextBox selection style properties are reset - c#

I've made a simple text editor (based on RichTextBox) that can bolden/tilt/change font size of selected text. And those things work just fine - e.g. I can apply both Bold and Italic to the same selection.
I recently added "Capitalize" button:
private void buttonCapitalize_Click(object sender, RoutedEventArgs e)
{
if (!textField.Selection.IsEmpty)
{
textField.Selection.Text = textField.Selection.Text.ToUpper();
}
}
aand it kinda works. Whenever I click it the selected text is capitalized but also other properties (of the current selection only) such as FontStyle, FontWeight are set to normal and FontSize to default.
Is there any better way to implement this?

I run some testing and from my results it seems that the RichTextBox will always take the style from the 1st character BEFORE your selection and not the default style as you mentioned
This probably happens because
textField.Selection.Text = textField.Selection.Text.ToUpper();
will actually create a new string and not edit it (strings are immutable in C#)
If you want to keep your styling I'm guessing that you'll have to iterate over your selection and create it per selected char

Related

Editable multiline combobox WPF

I have an issue with editable combobox. I can get textbox PART_EditableTextBox and make it multiline by AcceptsReturn="True" and TextWrapping="Wrap".
The problem I have is that I cannot make it work like classic textbox for text operations.
For example key down does not navigate in text and instead just fire item selection even if there is just 1 item in my collection it effectively refresh to original value. Same for scroll bar.
I found that I can catch some events (previewkeydown on parent element/ PreviewMouseWheel) and handle them to not reset value but still missing the text navigation(moving in text by arrows) or having scroll bar.
Edit
I find solution.
Insted of using ComboBox I used ComboBoxEdit from DevExpress.
Now it gets quite easy since they have already implemented property to solve halve of my problems.
this.ComboBoxEdit.AllowSpinOnMouseWheel = false
this.ComboBoxEdit.AcceptsReturn = true;
this.ComboBoxEdit.TextWrapping = TextWrapping.Wrap;
Only problem left was text navigation by arrows.
I catched PreviewKeyDown from parent element and implemented this.
private void Grid_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.OriginalSource is TextBox tx)
{
if (e.Key == Key.Up)
{
EditingCommands.MoveUpByLine.Execute(null, tx);
e.Handled = true;
}
else if (e.Key == Key.Down)
{
EditingCommands.MoveDownByLine.Execute(null, tx);
e.Handled = true;
}
}
}
I think the plan seems like a bad idea.
There are fundamental mismatches between functionality an editable combobox gives and what you seem to require from a multiline textbox inside one.
For one, the arrows already have an inherent meaning to a combobox. That's how you navigate the autocomplete list using keyboard.
An editable combobox is intended to do autocomplete as well as possibly add a new item.
Maybe you could have a textbox and a combobox. The user switches between these using a togglebutton or some such. Depending on whether they want to add new or look up an existing item.
I think your best bet is likely to build your own usercontrol if you particularly need this to look similar to a combobox.
The editable part would be a textbox.
The list of possibles can be a listbox inside a popup or expander.
You could choose whether to use a togglebutton to show your popup like the combobox does, or not.
Add the functionality you need without the problems functionality inherent to a combobox control adds.

Change The Users' Input To Bold Into A TextBox Without Change The Existing Content.

I Have a TextBox(lets call it "ViewTxtBox") and a Button(lets call it "BoldBtn") who does the following:
• User input whatever he wants from the keyboard.
• Once the button clicked the font changes into bold.
BoldBtn Code:
ViewTxtBox.Font = new Font(ViewTxtBox.Font, FontStyle.Bold);
Output If Clicked:
"Hello World I'm all bold and I don't want that"
What I want to do is to change the users' input from the keyboard into bold without change the current content into bold.
Example:
"Hello World"
-----------"Clicks Button"-------------
"Hello World I Pushed The Button And Went Bold"
How can I achieve that?
You can achieve that with a RichTextBox.
Use the selectionFont property to format the style before the text is added, and your button is clicked, change that selectionFont to Bold.
What's your use case?
The content of a box can have only one font defined by the Font attribute. So to achieve different font, you need different text box, or you need to redesign the full text box data template (or a new user control) to allow several string with different fonts (which seems a lot of trouble to me^^)

Avoid a textbox get's focus

I'm working in a Windows Form Application, I have a textbox which I want to avoid that gets the focus.
Now I'm using the property Enable but it gives a bad appearance to the form.
Also I tried with this
private void txtMyTextbox_Enter(object sender, EventArgs e)
{
ActiveControl = objMyOtherControl;
}
But like I'm selecting words of that textbox when the event is raise the textbox lose the selection.
If you want to make the textbox unfocusable but you still want be able to select the text, what you are looking for is the ReadOnly property.
Here is an extract from the official DOC:
You can transform an editable Windows Forms text box into a read-only
control. For example, the text box may display a value that is usually
edited but may not be currently, due to the state of the application.
To create a read-only text box Set the TextBox control's ReadOnly
property to true. With the property set to true, users can still
scroll and highlight text in a text box without allowing changes. A
Copy command is functional in a text box, but Cut and Paste commands
are not. Note The ReadOnly property only affects user interaction at
run time. You can still change text box contents programmatically at
run time by changing the Text property of the text box.

Color of selected text in RichTextBox

In CSS we're able to edit the text selection's colors by using the ::selection pseudo-tag. Is it possible, to overwrite the default background color of the selection just for one control, e.g. a RichTextBox? I know, that there is no way to change the default behaviour by something like CSS, but at least overwrite it for this control might be possible.
I already googled for about an hour now, but I only found snippets of syntax highlighting. I want the text to be e.g. yellow instead the typical Windows blue.
EDIT
Like in this fiddle: http://jsfiddle.net/W99Gt/
In WPF you can accomplish this as follows:
myRichTextBox.SelectionBrush = System.Windows.Media.Brushes.Yellow; // WPF
myRichTextBox.IsInactiveSelectionHighlightEnabled = true;
Unfortunately, the desired behavior is not possible in Windows Forms (details here). The workaround would be to use a WPF RichTextBox in the Windows Form through ElementHost.
References:
TextBoxBase.SelectionBrush Property (WPF)
TextBoxBase.IsInactiveSelectionHighlightEnabled Property (WPF)
EDIT: Removed the WinForms solution, because SelectionBackColor does not provide the desired behavior.
There is a property RichTextBox.SelectionColor which should do the work. Quoting MSDN
A Color that represents the color to apply to the current text
selection

Read-only textbox in C#

In C#, I am creating a form window for a LAN messenger with two textboxes. I need to create a particular textbox as read-only, but any text submitted to it is appearing grey which is not desirable. Is there any way that can be prevented?
I would use a Textbox and set ReadOnly to true, ForeColor to Color.Black, and BackColor to Color.White. This way you can still select the text and copy it with Ctrl-C.
You could replace it with a label or on the text box in the KeyPress event, set handled to true:
void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
}
You can set the colour of the text by setting the Textbox ForeColor property.
For example:
myTextBox.ForeColor = Color.Black
In order to keep the textbox white (or Window) when it's read-only, you must explicitly set the BackColor property to Window. To do this, you must first set the BackColor to some other value, then back to Window. The backcolor property should become bold indicating it is no longer the default value.
The grey color is indicative of the ReadOnly state of the textbox. It is a visual indication to the user who will not need to enter text to discover that the textbox is in fact, disabled.
If you need only the readonly behaviour, you would be better off using a Label instead.

Categories

Resources