Prevent images in rich text box - c#

I am working on a C# winForms application where I am using lots of RichTextBoxes. I found out that if I copied an image and pasted that in any RichTextBox, the image would be posted. Is there a way not to allow images to be pasted in the RichTextBox. In other words, to only allow keyboard characters.

The problem with the answer above is that it doesn't work in cases where there is mixed content. For example if you highlight a few rows from a spreadsheet and paste into a richtextbox you end up with more than just the raw text. I think the better solution is below:
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.V)
{
if (Clipboard.GetData("Text") != null)
Clipboard.SetText((string)Clipboard.GetData("Text"), TextDataFormat.Text);
else
e.Handled = true;
}
}
EDIT: The method below was shared by MrCC and is a more direct / better approach than my method above.
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.V)
{
if (Clipboard.ContainsText())
richTextBox1.Paste(DataFormats.GetFormat(DataFormats.Text));
e.Handled = true;
}
}

I was able to answer my question. Here it is in case someone else was looking for it.
private void InputExpressionRchTxt_KeyDown(object sender, KeyEventArgs e)
{
bool ctrlV = e.Modifiers == Keys.Control && e.KeyCode == Keys.V;
bool shiftIns = e.Modifiers == Keys.Shift && e.KeyCode == Keys.Insert;
if (ctrlV || shiftIns)
if (Clipboard.ContainsImage())
e.Handled = true;
}

Maybe, you can catch paste event and check what object copied to RichTextBox.
If it Image, just delete it.

Related

UWP Ctrl+F implementation

I'm creating an UWP app (C# .NET) where there is textbox. I want to implement a shortcut (Ctrl+F) to search texts in the textbox. I know how to find texts, but I don't know how to implement the shortcut.
I found this:
if ((e.Control && e.KeyCode == Keys.F) || (e.Control && e.KeyCode == Keys.S))
{
//do something
}
...but it isn't working for UWP. I tried this (textarea is name of textbox):
private void textarea_KeyDown(object sender, KeyRoutedEventArgs e)
{
if ((e.Key == Windows.System.VirtualKey.Control) && (e.Key == Windows.System.VirtualKey.F))
{
flayoutFind.ShowAt(appBarButtonFind as FrameworkElement);
}
}
but it isn't working too. How can I do it?
And for the future, is there any way, how to override default functionality and shortcut of textbox Ctrl+Z (undo)?
You should be using "Accelerators" and "Access keys" as described here:
https://learn.microsoft.com/en-us/windows/uwp/input-and-devices/keyboard-interactions
Basically, you will have to register for events
Window.Current.CoreWindow.Dispatcher.AcceleratorKeyActivated += Dispatcher_AcceleratorKeyActivated;
private void Dispatcher_AcceleratorKeyActivated(CoreDispatcher sender, AcceleratorKeyEventArgs args)
{
//Implementation
}
You can check the sample in detail here: https://github.com/Microsoft/DesktopBridgeToUWP-Samples/blob/master/Samples/SQLServer/BuildDemo/MainPage.xaml.cs

Ctrl+C on ListView recognises as "LButton | Shift Key"

How can I catch "ctrl+c" keys pressed on listview?
I'm trying like that
private void listviewLogger_KeyUp(object sender, KeyEventArgs e)
{
if (sender != listviewLogger) return;
//if (e.Control && e.KeyData == (Keys.Control | Keys.C))
if (e.Control && e.KeyCode == Keys.C)
CopySelectedValuesToClipboard();
}
but it shows me the combination of LButton | Sift Key when I press ctrl+C:
P.S.: have two languages installed in windows, system Win2012 R2
Update1: thank You for comment! If I log actions, I see this:
e.KeyData: ControlKey
e.KeyCode: ControlKey
e.KeyData: C
e.KeyCode: C
But still cannot catch this key sequence. Code:
private void listviewLogger_KeyUp(object sender, KeyEventArgs e)
{
if (sender != listviewLogger)
return;
Logger("e.KeyData: " + e.KeyData);
Logger("e.KeyCode: " + e.KeyCode);
}
Update2:
Resolved like this. Don't ask my how :-D
if (((e.KeyData & Keys.ControlKey) != Keys.ControlKey) && e.KeyCode == Keys.C)
CopyLogEntriesToClipboard();
Update3:
Previous works for KeyUp event. For KeyDown first code-snippet works
It is better to catch key down event (I've checked it on editor by holding Ctrl+C and switching to another up without relesing buttons).
Please try one more time your first construction. It is works for me!
private void listView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.C)
Text = "got it";
}

How to perform action when Shift+Enter is pressed?

I have a TextBox in a C#/XAML desktop app and I want to detect the Shift+Enter command. How can I do this?
So far I have only been able to find information on commands like Ctrl+A, etc.
ModifierKeys.Shift allows you to identify key pressed combinations which includes Shift:
private void HandleKeyDownEvent(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter && (Keyboard.Modifiers == ModifierKeys.Shift))
{
// Handle..
}
}
Another option is Keyboard.IsKeyDown static method (see Shoe's answer).
if (Keyboard.Modifiers == ModifierKeys.Shift && Keyboard.IsKeyDown(Key.Enter))
{
MessageBox.Show("test");
}
A good example can be found here.
public void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
{
MessageBox.Show("Pressed " + Keys.Shift);
}
}

How to get key code for shift+tab in windows application?

Hi have an windows application that should focus to previous text box with change in back color when shift+tab is pressed.
e.Modifiers == Keys.Shift && e.KeyCode == Keys.Tab
in an event handler method, using an if condition maybe.
Try this,
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && e.Shift)
{
}
}

WPF Keyboard Shortcut - Why is this not working?

I have the follwing code (which is not working):
private void Window_PreviewKeyDown(object sender, KeyEventArgs e) {
e.Handled = true;
if ((e.Key == Key.P) && (Keyboard.Modifiers == ModifierKeys.Alt)) {
MessageBox.Show("Thanks!");
}
}
Why doesn't this work? The event is firing, but
(e.Key == Key.P) && (Keyboard.Modifiers == ModifierKeys.Alt))
never evaluates to true.
My similar events using Ctrl instead of Alt in this way work. Also my events that include Ctrl and Alt work as well.
A better way to work with keys in WPF is Key Gestures
e.g. note that this is an example, not a solution
<Window.InputBindings>
<KeyBinding Command="ApplicationCommands.Open" Gesture="ALT+P" />
</Window.InputBindings>
There's more to it that that but you'll work it easily enough. That's the WPF way to handle keys!
PK :-)
You need to do a 'bitwise and' with the ModifierKeys as shown below...
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if ((e.Key == Key.P) && ((e.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt))
{
MessageBox.Show("Thanks!");
e.Handled = true;
}
}
Also, do not forget to set the Handled property of the e parameter...
MSDN gives us this example:
if(e.Key == Key.P && e.Modifiers == Keys.Alt)
does this work for you?

Categories