set cursor after button content click in password box wpf

Kran2022 406 Reputation points
2022-11-10T15:17:16.74+00:00

Hi All:
I have a password control box and there is small grid keybaord to enter password, issue is that the moment when i click on the button to enter the password, the cursor deosn't move after password box content
.
How can i set the cursor in the password box after the content got enterted by button click? in my case the cursor is set before the content.

259086-image.png

  private void bkletter_Click(object sender, RoutedEventArgs e)  
        {  
            
             ppassword.Password = ppassword.Password + "K";  
             FocusManager.SetFocusedElement(this, ppassword);  
         }  
  
        <PasswordBox Name="ppassword" HorizontalAlignment="Left" Margin="111,50,0,0" FocusManager.IsFocusScope="True"  
                     VerticalAlignment="Top" Height="33" Width="249" PasswordChar="*" FontSize="25" KeyDown="ppassword_KeyDown"   />  
  
  
<Button Content="K" Name="bkletter" Grid.Row="0" Grid.Column="0"   
                        VerticalAlignment="Top" Width="81" Height="44" IsCancel="True" FontSize="28" Background="White" Click="bkletter_Click" >  
                    <Button.Effect>  
                        <DropShadowEffect/>  
                    </Button.Effect>  
                </Button>  
Developer technologies Windows Presentation Foundation
Developer technologies .NET Other
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.5K Reputation points
    2022-11-10T15:37:25.66+00:00

    Some articles suggests this code:

    var a = new KeyEventArgs( Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, Key.End )  
    {  
        RoutedEvent = Keyboard.KeyDownEvent  
    };  
    InputManager.Current.ProcessInput( a );  
    

    Add it after FocusManager.SetFocusedElement.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.