Restrict keypress in wpf textbox - c#

I have TextBox in WPF where i need to fill the box only by pasting (ctrl +v) not by typing. So i need to restrict entire key press except ctrl+v. Since WPF is not having keypress event i am facing the problem to restrict the keypress

Do it WPF style and use ApplicationCommands.Paste and make the textbox readonly.

you can add this Key_Down handler to the textBox:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Modifiers == Keys.Control && e.Key==Key.V)
{
//Logic here
}
else
e.handled=true;
}

Provided you don't allow Right Click + Paste, but only Ctrl + V, I would simply check for the Ctrl key modifier being pressed and prevent everything else.

So try this:
myTextBox.KeyDown += new KeyEventHandler(myTextBox_KeyDown);
private void myTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.V && e.Modifiers == Keys.Control)
{
input = myTextBox.Text;
}
else
{
input = "";
}
}

<TextBox IsReadOnly="True" Name="Policy_text">
<TextBox.CommandBindings>
<CommandBinding Command="ApplicationCommands.Paste" CanExecute="PasteCommand_CanExecute" Executed="PasteCommand_Executed" />
</TextBox.CommandBindings>
</Textbox>
and in code behind
private void PasteCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = Clipboard.ContainsText();
}
private void PasteCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
Policy_text.Paste();
}

Related

c# Starting events with hotkey?

private void MeretOK_Click(object sender, EventArgs e)
{
//code
}
private void MeretTB_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode==Keys.Enter)
{
MeretOK_Click();
}
}
How can I start an event with a hotkey?
(I know I can just copy the code there but that is ugly)
If I just copy the code and run it I hear a beep. Why?
First, you have to allow your form to handle key events globally (set the form KeyPreview property to true) and to mark your event as internally handled. Second, if the purpose of this code is to simulate the mouse click on a specific control (programmatic click), for example a button, there is an easier way to accomplish it:
private void MeretTB_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.Handled = true;
myButton.PerformClick();
}
}

Focus selected controls by pressing Enter Key

In C# windows application to navigate all control of a Form (using Enter Key) I am using the below code:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == System.Windows.Forms.Keys.Enter)
{
SendKeys.Send("{TAB}");
}
}
N.B.: Form Property KeyPreview = True;
The above code works fine but when I am going to navigate between two dateTimePicker (dateTimePicker1, dateTimePicker2) pressing Enter Key.
When Form open Focus on dateTimePicker1 and press Enter Key then Focus dateTimePicker2 and press Enter Key Focus dateTimePicker1.
The below code works fine without the above code. What is the best way to navigate the two dateTimePicker using the above code or any other way?
private void dateTimePicker1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter) {
dateTimePicker2.Focus();
}
}
private void dateTimePicker2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter) {
dateTimePicker1.Focus();
}
}
Anybody please help me.
You can subscribe your two DateTimePickers to the same event handler instead of using two events, and use the sender object:
private void dateTimePicker_KeyDown(object sender, KeyEventArgs e)
{
var dtp = sender as DateTimePicker;
if (e.KeyCode == Keys.Enter)
{
if (dtp?.Name[dtp.Name.Length - 1] == '1')
dateTimePicker2.Focus();
else dateTimePicker1.Focus();
}
}
Just don't forget to change the value of the KeyDown event in the properties window of the both DateTimePickrs to point to this event.

Why won't my Enter Key Events in WPF C# do anything?

My enter key events won't do anything...not even show a simple textbox when pressing Enter in a textbox.
I am new to c# and coding in general.
Interestingly, my visual studio won't let some things go through like MessageBox.Show... It makes me do System.Windows.MessageBox.Show. Just in case this is a clue to what the problem may be...
Here is what I have...
private void textBoxPartNumber_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == System.Windows.Forms.Keys.Enter)
{
//textBoxQuantity.Focus();
System.Windows.MessageBox.Show("Testing 123");
System.Windows.Forms.SendKeys.Send("{TAB}");
e.Handled = true;
e.SuppressKeyPress = true;
}
}
TextBox property AcceptsReturn
<TextBox AcceptsReturn="true"/>
Use
if (e.KeyCode == System.Windows.Forms.Keys.Return)
Instead :)
If you're using WPF then it appears your event signature is incorrect. Try something like this:
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
//Do something
}
}
The KeyEventArgs class and Key enum are in the System.Windows.Input namespace in the PresentationCore assembly.

how to disable ctrl+A and ctrl+c in richtextbox in wpf?

my code is :
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if ((e.Key==Key.LeftCtrl && e.Key==Key.C) || (e.Key==Key.RightCtrl && e.Key==Key.C))
{
MessageBox.Show("Copy not allowed !");
e.Handled = true;
}
}
or , another way , i have tried is :
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if ((e.Key==Key.C) && (Keyboard.Modifiers==ModifierKeys.Control))
{
MessageBox.Show("Copy not allowed !");
e.Handled = true;
}
}
But they do not work !
Please do not tell me to set Focusable="False" or IsHitTestVisible="False"
because after that , i can not use scrollbar !
Please help.
thanks.
You can handle the PreviewKeyDown event... you almost had it, you just needed to and (&) the Keyboard.Modifiers because it could contain more than just ModifierKeys.Control:
private void PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.C && (Keyboard.Modifiers & ModifierKeys.Control) ==
ModifierKeys.Control)
{
MessageBox.Show("CTRL + C was pressed");
}
}
I assume your problem is not really how to disable ctrl+A and ctrl+C, just because you don't want the user to do exactly that, but to prevent the user from copying the content of the text box. Problem is, Ctrl+A Ctrl+C is not the only way to copy data. The user could select the text, and right click.
So what you should do then is not to override the keystroke, but the actual command that is being fired. (You may want to read up on how Commands works in WPF.)
To do that, simply add the following method to your class
private void CancelCopyCommand(object sender, DataObjectEventArgs e)
{
MessageBox.Show("Copy not allowed !");
e.CancelCommand();
}
And in the Constructor, register the command as follow:
DataObject.AddCopyingHandler(richTextBox1, CancelCopyCommand);
A richTextbox is for entering Text, ok you can make it readonly, so why do you want to prevent copy?
Try using Images to prevent copy, or disable focusing/selecting. If the user selects Text, destroy the selection.
You have to subscribe for the PreviewKeyDown-Event and in the Handler you have to set e.Handled = true if your Key-Combination was pressed.

2 types of click events same code 2 results in winrt

Strange behavior! when I click on the button on the metro app all works well, but when i click enter(the button on the KB) the only thing that happens is everything is cleared!
This fails
private void TextBox_KeyDown_1(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Enter)
{
textBlock.Text = textBox1.Text;
// textBox1.Text = "";
}
}
This works as expected
private void Send_Click(object sender, RoutedEventArgs e)
{
textBlock.Text = textBox1.Text;
textBox1.Text = "";
}
What am I doing wrong ?
thanks
It would be best if when pressing Enter on the textbox, it will actualy simulate a click on the button, to assure that both actions are really the same.
private void TextBox_KeyDown_1(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Enter)
{
this.Send.PerformClick();
}
}
private void Send_Click(object sender, RoutedEventArgs e)
{
textBlock.Text = textBox1.Text;
textBox1.Text = "";
}
Also, as Chris mentioned, you shouldn't really handle the KeyDown even in this case: you can set the AcceptButton property of the form to be the Send button, which means that when pressing Enter, the button will be pressed - even if not focused. This sort of problem is a good example of why using the AcceptButton property.
I think you are trying to handle focus when the user hits Enter. This is a common requirement in applications. As for your strange behavior, I can't explain it. But I am also not sure explaining it is as important as solving the problem. So, I can show you the easy implementation of handling enter and focus:
<StackPanel>
<TextBlock FontSize="20" Foreground="White">One</TextBlock>
<TextBox x:Name="T1" Width="1000" Height="100" KeyDown="T1_KeyDown_1" />
<TextBlock FontSize="20" Foreground="White">Two</TextBlock>
<TextBox x:Name="T2" Width="1000" Height="100" KeyDown="T2_KeyDown_1" />
</StackPanel>
and
private void T1_KeyDown_1(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter)
T2.Focus(Windows.UI.Xaml.FocusState.Programmatic);
}
private void T2_KeyDown_1(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter)
T1.Focus(Windows.UI.Xaml.FocusState.Programmatic);
}
The result is perfect focus control.

Categories

Resources