Hi. I'm very new to .Net and WPF and have a problem. The code is a snippet. I have those TextBoxes to enter dates. I check on correct input using GotFocus and LostFocus events.
<TextBox Name="sdDay" Width="40" Text="Day" GotFocus="DateDay_GotFocus" LostFocus="DateDay_LostFocus" Padding="5,5,5,5" HorizontalContentAlignment="Center" Focusable="True"/>
<TextBox Name="sdMonth" Width="50" Text="Month" GotFocus="DateMonth_GotFocus" LostFocus="DateMonth_LostFocus" Padding="5,5,5,5" Margin="5,0,0,0" HorizontalContentAlignment="Center" Focusable="True"/>
<TextBox Name="sdYear" Width="50" Text="Year" GotFocus="DateYear_GotFocus" LostFocus="DateYear_LostFocus" Padding="5,5,5,5" Margin="5,0,0,0" HorizontalContentAlignment="Center" Focusable="True"/>
And the code:
private void DateDay_GotFocus(object sender, RoutedEventArgs e)
{
if (((TextBox)sender).Text == "Day")
((TextBox)sender).Text = string.Empty;
}
private void DateDay_LostFocus(object sender, RoutedEventArgs e)
{
if (((TextBox)sender).Text == string.Empty)
((TextBox)sender).Text = "Day";
else
CheckForCorrectDateDay((TextBox)sender);
}
private void CheckForCorrectDateDay(TextBox b)
{
int day = 0;
try
{
day = int.Parse(b.Text);
if (day < 0 || day > 31)
{
MessageBox.Show("Please enter a correct day.");
b.Text = string.Empty;
b.Focus();
}
}
catch (FormatException)
{
MessageBox.Show("Please enter a number.", "Incorrect Input", MessageBoxButton.OK, MessageBoxImage.Warning);
b.Text = string.Empty;
b.Focus();
}
catch (Exception)
{
throw;
}
}
Now what I want it to do is check for correct input, and if that fails, set the focus back to whatever TextBox had an incorrect entry.
It doesn't work though. After I enter a number outside the range (or letter), the MessageBox will show but the focus shifts to the next TextBox which is for entering the month.
What am I doing wrong?