Move focus to next control on enter key pressed with Silverlight - silverlight

I need to let the users change to "next" textbox, combobox or button in a form when the press the Enter key (they CAN'T press Tab...)
I've found a couple of solutions (set a new event for each textbox's key pressed and set focus to the next control...) but I want this to have an easy manteinance.
Is there any magic around??
I've had problems even with tab navigation in silverlight...

private void textBox3_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
if (Keyboard.Modifiers == ModifierKeys.Shift)
textBox4.Focus();
else
textBox5.Focus();
e.Handled = true;
}
}

Related

Windows Form - Override Enter with an other key

Is there a way in Windows Form to activate the currently focused button/control with the Q key? Or override the Enter, so that pressing Q activates the currently focused control like Enter does? I want the user to be able to control the application with just the left hand on Tab and Q to cycle controls and activate them.
private void Form2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Q)
{
e.Handled = true;
}
I already have this, but what do I need after the e.Handled to activate the current focus?
As an option you can override ProcessCmdKey and check if the key is Q then send Enter using SendKeys.Send and return true to indicate you that you processed the key:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Q)
{
SendKeys.Send("{Enter}");
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
You can limit the behavior to buttons by checking this.ActiveControl is Button if you need.

Detect when caret position changes in RichTextBox

I am trying to implement very simple text formatting functionality for a RichTextBox in WPF. This just consists of a few bold, italic, etc ToggleButtons just above the RichTextBox. See image below, but ignore the top TextBox - the RichTextBox is the bigger one at the bottom.
Toggling formatting for either a selection or at the caret position (for text that will be typed in) is not a problem, as I'm doing this:
private void BoldButton_Checked(object sender, RoutedEventArgs e)
{
this.SetSelectionBold(true);
}
private void BoldButton_Unchecked(object sender, RoutedEventArgs e)
{
this.SetSelectionBold(false);
}
private void SetSelectionBold(bool isBold)
{
var selection = this.RichText.Selection;
if (selection != null)
{
selection.ApplyPropertyValue(TextElement.FontWeightProperty, isBold ? FontWeights.Bold : FontWeights.Normal);
}
}
However, if the user moves the caret somewhere else (e.g. from bold text to normal text) then I'd like the ToggleButtons to reflect that state, in much the same way as it works in Word. Is it possible to detect when the caret position changes, and take action accordingly?
Hook yourself into SelectionChanged event and get current caret position, and test if the property exists on that selection?
In the event, probably you want something like:
var selection = richTextBox.Selection;
if(selection != null)
{
if(selection.GetPropertyValue(TextElement.FontWeightProperty) == FontWeights.Bold)
// todo; enable your button
}
If that event is not triggered by caret positioning(the document doesn't say anything about that),
you probably need to inherit from RichTextBox and override OnSelectionChanged, after that you need to actually generate your own Caret, eg:
var currentCaretPlusOne = new TextRange(richTextBox.CaretPosition,
richTextBox.CaretPosition+1);
if(currentCaretPlusOne != null)
{
if(currentCaretPlusOne.GetPropertyValue(TextElement.FontWeightProperty)
== FontWeights.Bold)
// todo; enable your button
}

Left button pressed in mouse move in wpf

On mouse move of a grid, left button pressed is not caught, but right button pressed is caught. Any one know the reason?
private void grid1_MouseMove(object sender, MouseEventArgs e)
{
if (e.RightButton == MouseButtonState.Pressed)
{
//Entered to the loop
}
}
private void grid1_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
//Not enter to the loop
}
}
There could be any number of reasons, but as you didn't provide a Minimal, Complete, and Verifiable example, we can't really tell you for sure. There is certainly nothing wrong with the following code, that works as expected in a new project:
private void grid1_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
//Entered the loop
}
if (e.RightButton == MouseButtonState.Pressed)
{
//Entered the loop
}
}
The most likely reasons why your code never entered your if statement are as follows:
You weren't moving the mouse when clicking the left mouse button.
You weren't over the Grid, when you clicked the left mouse button.
You are handling the left click in a tunnelling event (Preview... event) and setting e.Handled to true.
If these suggestions do not help, then please follow the advice in the linked help page and provide a Minimal, Complete, and Verifiable example that we can use to further help.

how can I know if backspace or delete was pressed in TextChanged event of textbox

I use the TextChanged event of a textbox.
for instance I have the text in the textbox:
"a b"- 2 spaces between a,b
I go in the middle of the 2 spaces, and hit backspace. I can not check by comparing the new text with the old which key was pressed (if backspace or delete was pressed, the newtext is the
same "a b"-1 space between letters.
How can I check which key was pressed?
Why do you care? If you're handling the TextChanged event, you're not supposed to care what about the text was changed, just the fact that it was.
If you need to care about what specifically was changed, you need to handle a lower-level event, like KeyDown:
private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
if (e.Key == Key.Back || e.Key == Key.Delete)
{
// The user deleted a character
}
}
I know this is an old question, but maybe someone will find this helpful.
I used an integer, that counts length of current characters. It updates with the event.
Every time, the event is triggered, I check, if the length of the counter is longer than the current length of the text. If it is, we know, there was something deleted.
private int counter = 0;
private void TextChangedEvent(object sender, TextChangedEventArgs e){
if(counter > textbox.text){
counter = text.Length;
//deleted
}
else{
counter = text.Length;
//added}

Why doesn't the MaxLength property on a RichTextBox work in WPF?

I am trying to set the MaxLength property on a RichTextBox but it does not seem to work.
Any ideas what might be happening?
The fundamental problem is that the WPF RichTextBox doesn't have a MaxLength property - unlike the Windows.Forms one.
Here's an improvement on #jhony's anwser. If you catch the PreviewKeyDown event and check the length, you also need to allow the user to press Delete and BackSpace after hitting the limit.
// In constructor
this.RichTextBox.PreviewKeyDown += this.EditBox_KeyDown;
private void EditBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key != Key.Delete && e.Key != Key.Back)
{
var range = new TextRange(this.RichTextBox.Document.ContentStart, this.RichTextBox.Document.ContentEnd);
if (range.Text.Length > this.MaxLength)
{
e.Handled = true;
return;
}
}
}
You should also allow the arrow keys, because you wouldn't expect them to be disabled.
To disable pasting, put this in the constructor DataObject.AddPastingHandler(this.RichTextBox, this.EditBox_Paste); and
private void EditBox_Paste(object sender, DataObjectPastingEventArgs e)
{
e.CancelCommand();
}
However, you might want to allow pasting unless it breaks the MaxLength, in which case you'll need to check the text being inserted, and the text it is replacing. At that point I decided not to implement a MaxLength in the control, but to handle it with the rest of the validation in the form.
Add this code in yout KeyDown Event.
private void rLetter_KeyDown(object sender, KeyEventArgs e)
{
TextRange tr= new TextRange(rLetter.Document.ContentStart, rLetter.Document.ContentEnd);
if (tr.Text.Length > 4000 || e.Key == Key.Space || e.Key == Key.Enter)
{
e.Handled = true;
return;
}
}
I have some problem, The bug is :
Test this code using copy and paste text max 4000.
Sorry for my English ..

Resources