none
Textbox Focus in LostFocus Event

    Question

  • Hi Every One,

     

      I am Using WPF 3.5 (C#). In a Page (Register) When a user enters email in textbox, a validation that the email already exists in db or not.

      Now the problem is that I have written the validation in textbox lost focus( ) event  like the following

    private void txtemail_LostFocus(object sender, RoutedEventArgs e)

    {

    string que = "Select email from Account where email = '" + txtemail.Text + "'";

    SqlDataAdapter sada = new SqlDataAdapter(que,sc);

    SqlCommandBuilder sbuild = new SqlCommandBuilder(sada);

    DataSet ds = new DataSet();

    sada.Fill(ds, "Account");

    DataView dv = new DataView();

    dv.Table = ds.Tables["Account"];

    //txtemail.Focus();                (Loop Doesn't end when it is in not comment)

    if (dv.Count > 0)

    {

    MessageBox.Show("The Email Already Exists, Please Enter Different", "Email Exists", MessageBoxButton.OK, MessageBoxImage.Information);

    txtemail.Text = " ";

    txtemail.Focus();

    return;

    }

    }

    My Problem is that the validation is working and when the email already exists the focus is not working it automatically focused to the next button.

     

    If I put the txtemail.focus() (commented one), then the loop doesn't end when I see by using Breakpoint. Without using breakpoint the error is coming  like An unhandled exception of type System.StackOverflowException' occurred in PresentationFramework.dll.

     

    Can anyone handled such type of situation?

     

    Thank You 

     

    Thursday, February 07, 2008 7:10 AM

Answers

  • Well, I don't know if the tooltip is not present because of your VS beeing the express edition. But I don't think that is the case. Look for the tool tip under All Controls category. If you still can't find the tool tip, right click on the tool bar and select "Choose Items...", from there you can find the ToolTip Control. I think it would be a better solution than a MessageBox.

    Monday, February 11, 2008 2:03 PM

All replies

  • First I would like to understand why you are using this code on the LostFocus event if you don't want it to loose the focus? Second, the same thing is happening with or without the breakpoint, the difference is that without the breakpoint, the loop is running infinitely therefore causing the StackOverflow, with the breakpoint, it happens once at a time.

     

    Third and last, this shouldn't be the cause of the exception, I would guess the there is something in the GotFocus event, that is making this happen, or (I'm not sure about this in WPF but in WinForms it won't happen) the MessageBox is causing the LostFocus event to be thrown again and this way would cause the infinite loop.

     

    Check if you got any GotFocus event that might be causing this.

    Regards,

    Fábio

    Thursday, February 07, 2008 12:05 PM
  • First Thank You for Your kind reply

     

    I would like to validate in client side while user enter the email id.

    The basic reason is to avoid a big message box shows to user ie., the list of unentered or missed mandatory values so that only I have planned to validate in lostfocus.

     

    And next I can't understand and clear what you are saying?

    Where should I give the txtemail.focus();

     

    Can u able to give some example?

     

    Thank You

    Friday, February 08, 2008 6:14 AM
  • Hi, I got what you want here, so I will provide you with what I think is a better way to handle the situation. First I think you can use the lostfocus event, but not like this, you shouldn't use the focus method in case it is not a valid e-mail adress. Why? Well, if the user doesn't enter a valid e-mail address he will not be able to move to another control, using txtemail.Focus() on the GotFocus event will make the txtemail never loose the focus, so think about the user, what if he wants to type other kind information before the e-mail (maybe the e-mail message?), this will be very annoying as he won't be able to move the focus to another control.

     

    So I have a suggestion for you to put in the lostfocus event:

     

    1 - You don't want a textbox right? Because it is very invasive. Ok, there is a nice alternative to it, that is the tooltip control. If the e-mail is not valid, just display the tooltip control (that has a balloon shape available)

    2 - Instead of setting the focus back the e-mail textbox, let the focus go to another control and set lets say, the Save button (or Send?) Enabled property to false. And if the e-mail is valid, set the Enabled property back to true, so he will only be allowed to "Save" if he entered a valid e-mail address. And you tell him that it is not valid through the tooltip.

     

    I hope this makes sence to you and if you really want to mantain the focus on the e-mail text we will have to track down where the problem is, as of your code I don't see why you are entering the loop. Do you have a GotFocus event in txtemail?

    If so, please post the code in it.

     

    Regards,

    Fábio

    Friday, February 08, 2008 10:44 AM
  •  

    No I haven't use any GotFocus event in txtemail. and also there is no tooltip is available from the toolbar. I am using  Microsoft Visual Studio WPF 3.5 Express Edition (C#).

     

    And also my form is the Registration form, having to enter the details of the user, I am checking whether the entered email address already presented in the database.

     

    Is there is any alternatives?

     

    Thank You 

    "Time and Tide Waits for None"

     

     

    Monday, February 11, 2008 6:18 AM
  • Well, I don't know if the tooltip is not present because of your VS beeing the express edition. But I don't think that is the case. Look for the tool tip under All Controls category. If you still can't find the tool tip, right click on the tool bar and select "Choose Items...", from there you can find the ToolTip Control. I think it would be a better solution than a MessageBox.

    Monday, February 11, 2008 2:03 PM
  • Hi,
    did you get solution for the issue u were facing....

    now i am also facing the same problem.. can you please cjst me what step you have taken to solve it...

    waiting for your reply
    Sunday, October 05, 2008 11:52 AM
  • Thank you much. Your answer solved my problem. I had looked for EditEnded Event handler. It seems not to exist.

    I spent a couple of days for this problem. I believe that LostFocus, PreviewKeyUp can solve it.


    PreviewKeyUp use for checking `Enter` KeyUp in TextBox.


    CellEditEnding Event handler can get focusing cell position.


    Capturing the Enter Key in the WPF textbox control

    http://www.netboffin.com/pages/WPFCaptureEnter.php


    My WPF C# code.


    ...

            <DataGrid Name="inputDataGrid" CellEditEnding="InputDataGrid_CellEditEnding" ... >
                <DataGrid.CellStyle>
                    <Style TargetType="DataGridCell">
                        <EventSetter Event="PreviewKeyUp" Handler="InputDataGrid_PreviewKeyUp"/>
                        <EventSetter Event="LostFocus" Handler="InputDataGrid_LostFocus"/>
                    </Style>
                </DataGrid.CellStyle>
                <DataGrid.ColumnHeaderStyle>


    ...


           private void inputDataGrid_PreviewKeyUp(object sender, KeyEventArgs e)
            {
                if (e.Key != Key.Enter) return;


     

    And ``<EventSetter Event="LostFocus" Handler="InputDataGrid_LostFocus"/>`` can't work well. This line makes DataGrid readonly.

    So I suggest next XAML and C# codes. That works well. Because of this issue, I spend about half-day.

     

    ...

    <DataGrid Name="inputDataGrid" CellEditEnding="InputDataGrid_CellEditEnding" ... >
    <DataGrid.CellStyle>
    <Style TargetType="DataGridCell">
    <EventSetter Event="PreviewKeyUp" Handler="InputDataGrid_PreviewKeyUp"/>
                        </Style>
    </DataGrid.CellStyle>
    <DataGrid.ColumnHeaderStyle>

    ...

            private void InputDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
            {
                inputDataGrid.AddHandler(Control.LostFocusEvent, new RoutedEventHandler(InputDataGrid_LostFocus));

    ...
            private void InputDataGrid_LostFocus(object sender, RoutedEventArgs e)
            {
                inputDataGrid.RemoveHandler(Control.LostFocusEvent, new RoutedEventHandler(InputDataGrid_LostFocus));
    ...

            private void inputDataGrid_PreviewKeyUp(object sender, KeyEventArgs e)
            {
                if (e.Key != Key.Enter) return;

    ...

    Thursday, July 07, 2011 2:09 AM