RichTextBox - retain original formatting (font), even after paste - vb.net

I need to use a RichTextBox, not a normal textbox because of the way it keeps the caret position, from line to line. But I need to keep the text in the same font all the time even if it is pasted.
At the moment I have it selecting the entire text and changing the font to the original (Lucida Console) but it look horrible when you paste into it as it flashes blue.

If you are handling the pasting programatically don't use the Paste method. Instead use Clipboard.GetDataObject().GetData(DataFormats.Text) to get the text in a string and then add the text using the Rtf or Text property to the RichTextBox:
string s = (string)Clipboard.GetDataObject().GetData(DataFormats.Text);
richTextBox.Text += s;
Otherwise you could handle the Ctrl+V key press:
void RichTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if(e.Control == true && e.KeyCode == Keys.V)
{
string s = (string)Clipboard.GetDataObject().GetData(DataFormats.Text);
richTextBox.Text += s;
e.Handled = true; // disable Ctrl+V
}
}

Darin's method ignores caret position and always appends to the end of text.
Actually, there are better method. Use overload of RichTextBox.Paste():
DataFormats.Format plaintext_format = DataFormats.GetFormat(DataFormats.Text);
this.Paste(plaintext_format);
Works like charm for me.

Both #Darin & #idn's answers are good, however I could get neither to work when pasting the following rich text:
 This is text after an arrow.
This is a new line
The font would always change to WingDings. I had copied this from MS Word:
Specifically, the plain-text format method described by #idn above did indeed just paste plain text, but something was happening in which the font was changed too.
The following code handles the KeyUp event to just select all text and replace its original colours and font (i.e. formatting). To ensure that this isn't visible on the screen as a flicker, a special method of disabling window repaint events was employed. Control draw disablement occurs in the KeyDown event, the RichTextBox control handles the paste event by itself, and then Control drawing is re-enabled at the end. Finally, this only happens for CTL+V and SHIFT+INS, both of which are standard paste commands:
/// <summary>
/// An application sends the WM_SETREDRAW message to a window to allow changes in that
/// window to be redrawn or to prevent changes in that window from being redrawn.
/// </summary>
private const int WM_SETREDRAW = 11;
private void txtRichTextBox_KeyDown(object sender, KeyEventArgs e)
{
// For supported Paste key shortcut combinations, suspend painting
// of control in preparation for RTF formatting updates on KeyUp
if ((e.Control && !e.Shift && !e.Alt && e.KeyCode == Keys.V) || // CTL+V
(!e.Control && e.Shift && !e.Alt && e.KeyCode == Keys.Insert)) // SHIFT+INS
{
// Send Suspend Redraw message to avoid flicker. Drawing is
// restored in txtRichTextBox_KeyUp event handler
// [this.SuspendLayout() doesn't work properly]
Message msgSuspendUpdate = Message.Create(
txtRichTextBox.Handle, WM_SETREDRAW, IntPtr.Zero, IntPtr.Zero);
NativeWindow window = NativeWindow.FromHandle(txtRichTextBox.Handle);
window.DefWndProc(ref msgSuspendUpdate);
}
}
private void txtRichTextBox_KeyUp(object sender, KeyEventArgs e)
{
// Following supported Paste key shortcut combinations, restore
// original formatting, then resume painting of control.
if ((e.Control && !e.Shift && !e.Alt && e.KeyCode == Keys.V) || // CTL+V
(!e.Control && e.Shift && !e.Alt && e.KeyCode == Keys.Insert)) // SHIFT+INS
{
// Layout already suspended during KeyDown event
// Capture cursor position. Cursor will later be placed
// after inserted text
int selStart = txtRichTextBox.SelectionStart;
int selLen = txtRichTextBox.SelectionLength;
// Replace all text with original font & colours
txtRichTextBox.SelectAll();
txtRichTextBox.SelectionFont = txtRichTextBox.Font;
txtRichTextBox.SelectionColor = txtRichTextBox.ForeColor;
txtRichTextBox.SelectionBackColor = txtRichTextBox.BackColor;
// Restore original selection
txtRichTextBox.SelectionStart = selStart;
txtRichTextBox.SelectionLength = selLen;
txtRichTextBox.ScrollToCaret();
// Resume painting of control
IntPtr wparam = new IntPtr(1); // Create a C "true" boolean as an IntPtr
Message msgResumeUpdate = Message.Create(
txtRichTextBox.Handle, WM_SETREDRAW, wparam, IntPtr.Zero);
NativeWindow window = NativeWindow.FromHandle(txtRichTextBox.Handle);
window.DefWndProc(ref msgResumeUpdate);
txtRichTextBox.Invalidate();
txtRichTextBox.Refresh();
}
}
A caveat of this approach is that, because the events are not suppressed (e.Handled = true;), the standard CTL+Z (undo) operation is supported. However this process cycles through undoing the format changes too. I don't see this as a big problem, because the next time that text is pasted, formatting is once again removed.
This approach isn't perfect, because if the text is copied and pasted from the RichTextBox (into another application), the newly applied formatting remains, but in my opinion, that's better than losing the undo functionality. If the undo functionality isn't important, then replace the text selection and formatting application with a replacement of the text to remove all formatting, as per this answer: https://stackoverflow.com/a/1557270/3063884
var t = txtRichTextBox.Text;
txtRichTextBox.Text = t;

Related

How can I toggle the checkbox of an item in a CheckedListbox if CheckOnClick is True, but SelectionMode is None..?

How can I toggle the checkbox of an item in a CheckedListbox if CheckOnClick is True, but SelectionMode is None..?
If I set SelectionMode to One it works as expected, but I would like to prevent items in the list from being selected. The only purpose of the CheckedListbox is to use the checkboxes; having items selected is not desired.
I tried a bit of code with the various Click and Mouse events, but none of them seem to report the item in the list that was clicked. If I could determine that, it would be a simple matter to toggle the checkbox of the clicked item.
The MouseClick event will tell you when the control was clicked and where. Determine whether the click was on an item and which one, then toggle it:
Private Sub CheckedListBox1_MouseClick(sender As Object, e As MouseEventArgs) Handles CheckedListBox1.MouseClick
Dim clickedIndex = CheckedListBox1.IndexFromPoint(e.X, e.Y)
If clickedIndex <> ListBox.NoMatches Then
CheckedListBox1.SetItemChecked(clickedIndex, Not CheckedListBox1.GetItemChecked(clickedIndex))
End If
End Sub
After a little testing, I would say that you need to consider “how” you want to achieve the non-selection mode you describe. As you have noted, if you set the CheckedListBoxes SelectionMode to None… then basically the check boxes become unusable.
The user cannot check or un-check any check box, as you already know… and this is why you want “your” code to change the check boxes checked state. So, you have now inherited the job of changing the check boxes check state because you set its “SelectionMode” to “None" … ? …
In addition, when the selection mode is set to “None” … then many “typical” properties of the CheckedListBox will lose functionality and become useless. Example, the checked list boxes SelectedItem property will always be null and its SelectedIndex property will most likely always be -1. Meaning, most “selection” type events will need to find what item was clicked by using the mouse location as shown in another answer.
The main point here is that when you decided to set the checked list boxes selection mode to “None”, then you basically open the door for more coding/responsibility on your part as far as “managing” the checked list box. I am just saying that the out-of-the-box .Net CheckedListBox is not feature rich and is a basic control. I am guessing there “may” be a third-party CheckedListBox Control that may have this “non-selected” functionality built-in.
So… I suggest another approach… however… it also has some drawbacks… basically you have to create a new Class MyCheckedListBox type “Control” that inherits from the CheckedListBox and then override its draw method to paint the cell the way we want.
I tend to avoid creating new controls. However, this will allow us to “keep” the CheckedListBoxes “selection functionality” by keeping its SelectionMode set to One. In addition to removing the job of “your” code having to manage each check box’s check state… we can also use all the checked list boxes “selection” events and use them as we typically would using the list boxes “selection” properties.
Below is a crude example of how to override the CheckedListBox’s Draw method to keep the “selected” items back color to the same color of the non-selected items.
class CheckedListBox_NoSelect : CheckedListBox {
protected override void OnDrawItem(DrawItemEventArgs e) {
DrawItemEventArgs new_e_Args = new DrawItemEventArgs
(e.Graphics,
e.Font,
new Rectangle(e.Bounds.Location, e.Bounds.Size),
e.Index,
(e.State & DrawItemState.Focus) == DrawItemState.Focus ? DrawItemState.Focus : DrawItemState.None,
this.ForeColor,
this.BackColor);
base.OnDrawItem(new_e_Args);
}
}
The code above is a simplified version of this SO question… How change the color of SelectedItem in CheckedListBox in WindowsForms? …
As I started, you will have to decide which approach you need. I may tend to go the route of #user18387401‘s answer … simply to avoid creating a new User Control. However, if you want this functionality for all the CheckedListBoxes, then creating the control may be a better approach.
Below is a full example of what is described above.
The CheckedListBox on the left is a regular CheckedListBox and uses the approach from user18387401‘s answer. The CheckedListBox on the right is our new control class CheckedListBox_NoSelect above.
For each control, the SelectedIndexChanged event is wired up to demonstrate that the checked list box on the left with its SelectionMode set to None will always have its SelectedItem set to null and its SelectedIndex will always be set to -1. However, it is not difficult to figure out “which” item was selected using user18387401‘s approach. This index is also displayed in its SelectedIndexChanged event.
private void Form1_Load(object sender, EventArgs e) {
checkedListBox1.SelectionMode = SelectionMode.None;
checkedListBox1.Items.Add("Item 1");
checkedListBox1.Items.Add("Item 2");
checkedListBox1.Items.Add("Item 3");
checkedListBox1.Items.Add("Item 4");
checkedListBox1.Items.Add("Item 5");
checkedListBox1.CheckOnClick = true;
// Leave default selection mode to "One"
checkedListBox_NoSelect1.Items.Add("Item 1");
checkedListBox_NoSelect1.Items.Add("Item 2");
checkedListBox_NoSelect1.Items.Add("Item 3");
checkedListBox_NoSelect1.Items.Add("Item 4");
checkedListBox_NoSelect1.Items.Add("Item 5");
checkedListBox_NoSelect1.CheckOnClick = true;
}
private void checkedListBox1_MouseClick(object sender, MouseEventArgs e) {
int clickedIndex = checkedListBox1.IndexFromPoint(e.X, e.Y);
if (clickedIndex != -1) {
checkedListBox1.SetItemChecked(clickedIndex, !checkedListBox1.GetItemChecked(clickedIndex));
Debug.WriteLine("LEFT: MouseClick Selected Index: " + clickedIndex);
}
}
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e) {
Debug.WriteLine(" LEFT -> Item: " + (checkedListBox1.SelectedItem == null ? "Null" : checkedListBox1.SelectedItem));
Debug.WriteLine(" LEFT -> Index: " + checkedListBox1.SelectedIndex);
}
private void checkedListBox_NoSelect1_SelectedIndexChanged(object sender, EventArgs e) {
Debug.WriteLine("RIGHT -> Item: " + (checkedListBox_NoSelect1.SelectedItem == null ? "Null" : checkedListBox_NoSelect1.SelectedItem));
Debug.WriteLine("RIGHT -> Index: " + checkedListBox_NoSelect1.SelectedIndex);
}
I hope this makes sense and helps. Sorry that I did this in C#. If you can not convert the code to a VB version, then let me know and I will add a VB version.

Check if caret is visible on Screen View

I've been thinking all this while how to check if caret is visible on the present Screen view of a Richtextbox.
The issue is that the caret may be at the end of the richtextbox and the scroll is at the beginning of the box, how can I programmatically know if the caret is visible on the present screen view or not.
NB. the caret may not necessarily be at the bottom.
Assuming this is WinForms
One thing that I think you could use is RichTextBox1.GetPositionFromCharIndex(), and RichTextBox1.SelectionStart:
' Y pos of caret
Dim CaretYPos As Integer = RichTextBox1.GetPositionFromCharIndex(RichTextBox1.SelectionStart).Y
Dim CharHeight As Integer = 4 ' Height of each line
If CaretYPos >= RichTextBox1.Height - CharHeight Then
' Caret is hidden below screen view
ElseIf CaretYPos < -CharHeight Then
' Caret is hidden above screen view
Else
' Caret is visible
End If
Though you might have to account for higher dpi displays when it comes to the CharHeight
I adapted #jimi 's comment to my C# application and it worked fine in conjunction with the ScrollToCaret() method. Thanks.
private void CheckAndMakeSelectionVisible()
{
// other stuff
if(!IsCaretVisible()) {
richTextMsg.ScrollToCaret();
}
}
private bool IsCaretVisible()
{
if (!richTextMsg.ClientRectangle.Contains(richTextMsg.GetPositionFromCharIndex(richTextMsg.SelectionStart)))
{
// Outside visible bounds
return false;
}
else
return true;
}

Disable 'Ding' on enter [duplicate]

I have a very simple Windows Forms Application. And, in Windows (or, atleast Windows Forms Applications), when you press Enter while inside a Single-line TextBox Control, you hear a Ding. It's an unpleasent sound, that indicated you cannot enter a newline, because it is a single-line TextBox.
This is all fine. However, in my Form, I have 1 TextBox, and a Search Button. And I am allowing the user to Perform a search by pressing Enter after they've finished typing, so they don't have to use the mouse to click the Search Button.
But this Ding sound occurs. It's very annoying.
How can we make it so just that sound doesn't play at all in my Form?
#David H - Here's how I'm detecting the enter pressing:
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
// Perform search now.
}
}
It works for me:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
//Se apertou o enter
if (e.KeyCode == Keys.Enter)
{
//enter key is down
this.doSomething();
e.Handled = true;
e.SuppressKeyPress = true;
}
}
The SuppressKeyPress is the really trick. I hope that help you.
Check out the Form.AcceptButton property. You can use it to specify a default button for a form, in this case for pressing enter.
From the docs:
This property enables you to designate
a default action to occur when the
user presses the ENTER key in your
application. The button assigned to
this property must be an
IButtonControl that is on the current
form or located within a container on
the current form.
There is also a CancelButton property for when the user presses escape.
Try
textBox.KeyPress += new KeyPressEventHandler(keypressed);
private void keypressed(Object o, KeyPressEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.Handled = true; //this line will do the trick
}
}
Just add e.SuppressKeyPress = true; in your "if" statement.
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//If true, do not pass the key event to the underlying control.
e.SuppressKeyPress = true; //This will suppress the "ding" sound.*/
// Perform search now.
}
}
You can Use KeyPress instead of KeyUp or KeyDown its more efficient
and here's how to handle
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
e.Handled = true;
button1.PerformClick();
}
}
and say peace to the 'Ding'
Use SuppressKeyPress to stop continued processing of the keystroke after handling it.
public class EntryForm: Form
{
public EntryForm()
{
}
private void EntryTextBox_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
e.Handled = true;
e.SuppressKeyPress = true;
// do some stuff
}
else if(e.KeyCode == Keys.Escape)
{
e.Handled = true;
e.SuppressKeyPress = true;
// do some stuff
}
}
private void EntryTextBox_KeyUp(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
// do some stuff
}
else if(e.KeyCode == Keys.Escape)
{
// do some stuff
}
}
}
On WinForms the Enter key causes a Ding sound because the form property AcceptButton is not specified.
If you don't need an AcceptButton the ding sound can be suppressed by setting the form KeyPreview to true and enter the following KeyPress event:
private void Form_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
e.Handled = true;
}
No matter what control is active, there will be no more ding sound when pressing the Enter key. Since the key event proccessing order is KeyDown, KeyPress and KeyUp the Enter key will still work for the KeyDown events for the controls.
I stumbled on this post while trying to handle a KeyDown this worked for me.
If e.KeyCode = Keys.Enter Then
e.SuppressKeyPress = True
btnLogIn.PerformClick()
End If
Supressing the Key Press stops the event from being sent to the underlying control. This should work if you're manually handling everything that the enter key will be doing within that textbox. Sorry about the Visual Basic.
$("#txtSomething").keypress(function (e) {
if (e.which == 13) {
e.Handled = true; //This will prevent the "ding" sound
//Write the rest of your code
}
});
There is a very little chance anyone gets to this answer but some other answers are truly scary. Suppressing event on KeyDown kills 2 additional events in one strike. Setting e.Handled property to true is useless in this context.
The best way is to set Form.AcceptButton property to the actual Search Button.
There is also another way of utilizing Enter key - some people may want it to act as TAB button. To do that, add a new Button, set its Location property outside of the Form area (i.e. (-100, -100)) - setting Visible property to false may disable Button handlers in some cases. Set Form.AcceptButton property to your new button. In Click event handler add following code
this.SelectNextControl(ActiveControl, true, true, true, true)
Now, you may want to transfer focus only when focus it on TextBox you may want to either test ActiveControl type or use e.Supress property in event handlers of controls not meant to use Enter as TAB
That's it. You don't even need to capture e.KeyCode
Set your Search button's IsDefault property to true. This will make it a default button and it will be auto-clicked when Enter is pressed.
Well I lived with this problem long enough and looked it up here.
After thinking about this for quite some time and wanting the simplest way to fix it I came up with the easiest but not so elegant way to fix it.
Here is what I did.
Put 2 invisible buttons "Ok" and "Cancel" on the form.
Set the AcceptButton and CancelButton Property on the form to the invisible buttons.
Added no code to the buttons!
This solved all the secondary problems listed in this thread including the ToolStripMenu. My biggest complaint was the BindingNavigator, when I would enter a record number into the Current position to navigate to and pressed enter.
As per the original question in which the programmer wanted a search function when the enter button was pressed I simply put the search code in the invisible OK Button!
So far this seems to solve all problems but as we all know with Visual Studio, something will probably crop up.
The only other possible elegant way I could think of would be to write a new keystroke handling class which is way to much work for most of my projects.
You can set your textbox multi-line to true then handle the Enter key press.
private void yourForm_Load(object sender, EventArgs e)
{
textBox1.Multiline = true;
}
//then write your TextBox codes
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
// doSomething();
}
}
i changed the textbox properties for an multiline textbox and it works for me.
Concerning the e.SuppressKeyPress = true; solution, it works fine by itself. Setting SuppressKeyPress to true also sets Handled to true, so there's no need to use e.Handled= true;
void RTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
//do ...
bool temp = Multiline;
Multiline = true;
e.Handled = true;
Multiline = temp;
}
}

DataGridView editing cells on WinForms

I have a Windows Form VS2010 .NET 4 project with a standard DataGridView bound to a datasource on a form.
The grid has a text column that I want to be a point and edit at the character clicked to.
Like normal textbox/editors when you click on the character you want to adjust. If possible I would also like to use the UP/DOWN keys to move between rows but would like the cursor to move to the same character position obviously in the same column without selecting the entire text.
I have tried a few things:
DataGridView1.ClearSelection()
DataGridView1.BeginEdit(False)
The BeginEdit just puts the cursor at the end of the text, which means another click to point to the character position for editing.
I know a Commercial grid like DevExpress defaults to editing in which you can click to the correct character position with one click but obviously costs money.
I have tried in the DataGridView1_EditingControlShowing event
If TypeOf e.Control Is System.Windows.Forms.DataGridViewTextBoxEditingControl Then
Dim tb As TextBox = e.Control
tb.SelectionStart = 5
tb.SelectionLength = 5
End If
But this does nothing.
I am just trying to remove the two or three clicks to get to the character position that needs adjustment.
I haven't looked at a Custom DataColumn as yet.
Any suggestions would be greatly appreciated.
There is no good out of the box way of doing this. The closest there is is to set the EditMode of the grid to EditOnEnter but that means you only need two clicks, not three.
You will need to write your own column type.
Someone has done just that here.
I haven't checked if that example handles up and down - if it doesn't then you were on the right track with the SelectionStart and SelectionLength properties, just grab the caret position of the cell you are leaving and apply it to the new cell.
It turns out that setting these properties is a little bit more involved that I remembered (possibly because I was already using a MaskedTextBox custom column type last time I did this).
The code below (in c# but the principle holds for vb.Net and I can give the vb code if you can't convert it yourself) works happily - could be tidied up by putting it into a custom control but I'll leave that as an exercise :)
First I add a handler for the EditingControlShowing event:
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
DataGridViewTextBoxEditingControl t = e.Control as DataGridViewTextBoxEditingControl;
current_control = t;
t.Leave += new EventHandler(t_Leave);
}
In the method above current_control is a form level private variable. The event handler for t looks like this:
void t_Leave(object sender, EventArgs e)
{
cell_caret_pos = current_control.SelectionStart;
}
There again we have a class level private field - cell_caret_pos.
Then what I found was that to set SelectionStart and SelectionLength you need to work within the CellEnter event handler:
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.BeginEdit(false);
DataGridViewTextBoxEditingControl editControl =
(DataGridViewTextBoxEditingControl)dataGridView1.EditingControl;
if (cell_caret_pos != 0)
{
editControl.SelectionStart = cell_caret_pos;
editControl.SelectionLength = 0;
}
}

Eclipse RCP: Is it possible to shift the fields up without redrawing?

In my editor, I have a composite containing a label control right at the top which flashes informative message in red colour whenever the user enters erroneous inputs in any of the below lying fields. The text keeps changing dynamically depending on user's input. I am able to achieve the effect of displaying red coloured text on erroneous inputs and displaying nothing in the label for correct inputs.
But, I want that when there is no error to display in the label composite, the rest of the below fields shift up in display. And when there is error to display, the error should appear in it's place(at the top of all other fields) pushing the other fields down.
Is there a way to achieve this effect without redrawing all the controls again?
Yes, call layout (true) on the parent.
For example I have a view that has a search bar at the top who's visibility can be toggled. I have a method to create the search composite and one to remove it:
private void createNameSearchBar () {
mySearchControl = new CardSearchControl (myViewComposite, SWT.NONE);
mySearchControl.setSearchListener (this);
}
private void disposeNameSearchBar () {
mySearchControl.dispose ();
mySearchControl = null;
}
private CardSearchControl mySearchControl = null;
private Composite myViewComposite;
private boolean mySearchBarState;
To hide or show the search bar control I call this method (myViewComposite is the top level control that owns the search bar and all the other contorls):
public void setSearchBarState (boolean show) {
mySearchBarState = show;
if (myViewComposite == null || myViewComposite.isDisposed ())
return; // no work to do
if (mySearchBarState && mySearchControl == null) {
createNameSearchBar ();
mySearchControl.moveAbove (null);
myViewComposite.layout (true);
} else if (!mySearchBarState && mySearchControl != null) {
disposeNameSearchBar ();
myViewComposite.layout (true);
}
}