简体   繁体   中英

Setting focus on Passwordbox

so I have an C# WPF Application with an Login Window. In this Window I also have a Checkbox, which safes the Username for the next times he starts this Application (Its like a "Remember me").

The normal Focus for this Window is obviousely the Username. But If the User selected the Remember me Checkbox, the passwordbox should be in Focus (because Username is already filled).

So I Wrote this Code:

in Xaml:

<PasswordBox
    Name="PW"
    Grid.Row="2"
    Grid.Column="1"
    Margin="10 0 10 0"
    VerticalContentAlignment="Center"
    Height="45"/>

<CheckBox
    Name="CB"
    Grid.Row="1"
    Grid.Column="1"
    VerticalAlignment="Bottom"
    Margin="10"
    Content="Remember me"/>

<TextBox
    Name="Tbx"
    Grid.Row="1"
    Grid.Column="1"
    Margin="10 0 10 0"
    Text="{Binding Path=User}"
    VerticalContentAlignment="Center"
    Height="45"/>

<Button
    Name="Btn"
    Grid.Row="3"
    Grid.Column="1"
    Content="Login"
    Height="40"
    Width="100"
    VerticalAlignment="Center"
    HorizontalAlignment="Center"
    Command="{Binding Path=LoginUser}"
    Click="Btn_Click" 
    IsDefault="True"/>

The Binding in the Textbox is just a Property, which gets the entered Value.

The Binding in the Button is also just a Property, which checks for Username and Password, and then opens another Window (So I dont think, thats nessecary to show).

In xaml.cs

private void Btn_Click(object sender, RoutedEventArgs e)
    {
        if (Cb.IsChecked == true)
        {
        Properties.Settings.Default.Username = Tbx.Text;
        }
    }

So with this Button_Click I safe the entered Username, so the Application can Remember it for the next time. This works as it should.

And now the interesting part, the Window_Loaded event:

try
    {
    IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForAssembly();
    StreamReader srReader = new StreamReader(new IsolatedStorageFileStream("isotest", FileMode.OpenOrCreate, isolatedStorage));

    string Input = srReader.ReadLine().TrimStart();
    string InputCut = Input.Substring(0, Input.Length - 25);
    Tbx.Text = InputCut;
    Cb.IsChecked = true;
    Pb.Focus()
    }
    catch(Exception ex)
    {}

So If The CheckBox is Checked, he will go through the try part, and is going for Pb.Focus() (I´ve stepped it through), but somehow the passwordbox wont get the focus. I then thought, it may be because of the Try - Catch, so I tried to set the Focus outside of that, but the same problem occours. The Passwortbox wont get Focused.

I have already searched Google for that exact problem, but I couldnt find anything (Only normal Questions on how to set the Focus on a Passwordbox)

Also I have checked here on stackoverflow, but the only thing I found was this: Set focus on PasswordBox when application starts And this obviousely isnt my problem at all, I already have the Focus In The Window_Loaded.

The expected outcome is, that the passwordbox is getting the Focus, but somehow it wont. I am not even getting any Error Messages, so I cant provide any.

If you need any more Information just hit me up, I will edit the Question then. I hope anyone of you knows this Problem, and can help me here. Thanks!

I guess your problem might be hidden by your empty catch.

IE you have an error but that catch just "eats" it so you never see it.

It seems odd that you can step through and not notice it jump to the catch though.

Of course stepping through will mess up any focussing.

Here is working markup and code:

    <StackPanel>
        <TextBox
            Name="Tbx" Height="45"/>
        <PasswordBox
            Name="PW" Height="45"/>
        <CheckBox
           Name="CB" Content="Remember me"/>
        <Button
          Name="Btn"  Height="40"  Width="100" IsDefault="True"/>
    </StackPanel>


    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        string InputCut = "Blaa";
        Tbx.Text = InputCut;
        CB.IsChecked = true;
        PW.Focus();
    }

Create a new solution.

NOT WHAT YOU HAVE NOW.

Because whatever is wrong might well lie somewhere in code you've not shown us.

Paste the above markup and code into the mainwindow.

Make sure you hook up the loaded event.

Watch it work.

My advice would be:

Remove your try catch and never use an empty catch block again.

Fix your code reading that string from isolated storage so it's more robust.

For wpf it's more usual to use

 Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) 

Which is the user's appdata roaming root.

If your code still isn't working or erroring then add pieces of your app to the new solution you created until you reproduce your issue in that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM