This is a migrated thread and some comments may be shown as answers.

ComboBox LostFocus event fires before GotFocus

5 Answers 197 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Brian Scott
Top achievements
Rank 1
Brian Scott asked on 27 Feb 2013, 07:05 PM
I have a number of WPF RadComboBox controls in a WPF window and am handling the LostFocus event for each one so that I know which field has just been left. However, when a RadComboBox control gets focus via a mouse click it seems to fire a LostFocus event before its GotFocus event, which is causing my code to think it's left a field before it's even got there! This appears to be different behaviour from the other Rad input controls I'm using (e.g. RadWatermarkTextBox, RadDateTimePicker, etc).

Note that this problem only occurs when changing focus between the fields using mouse clicks. It doesn't appear to occur when navigating between the fields using the Tab key.

Is this a known bug or am I doing something wrong? Either way, is there a fix or workaround available.

The following is the xaml and code behind from a sample WPF window I created to highlight the problem...

<Window x:Class="MainWindow"
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" 
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
         
        <StackPanel Grid.Row="0"  Height="Auto" HorizontalAlignment="Stretch" Name="StackPanel1" VerticalAlignment="Stretch" Width="Auto">
            <telerik:RadComboBox Name="Combo1" Text="One"
                                 Margin="2" IsEditable="True"
                                 GotFocus="RadComboBox_GotFocus"
                                 LostFocus="RadComboBox_LostFocus">
                <telerik:RadComboBox.Items>
                    <telerik:RadComboBoxItem>One</telerik:RadComboBoxItem>
                    <telerik:RadComboBoxItem>Two</telerik:RadComboBoxItem>
                    <telerik:RadComboBoxItem>Three</telerik:RadComboBoxItem>
                    <telerik:RadComboBoxItem>Four</telerik:RadComboBoxItem>
                </telerik:RadComboBox.Items>
            </telerik:RadComboBox>
            <telerik:RadComboBox Name="Combo2" Text="Six"
                                 Margin="2" IsEditable="True"
                                 GotFocus="RadComboBox_GotFocus"
                                 LostFocus="RadComboBox_LostFocus">
                <telerik:RadComboBox.Items>
                    <telerik:RadComboBoxItem>Five</telerik:RadComboBoxItem>
                    <telerik:RadComboBoxItem>Six</telerik:RadComboBoxItem>
                    <telerik:RadComboBoxItem>Seven</telerik:RadComboBoxItem>
                    <telerik:RadComboBoxItem>Eight</telerik:RadComboBoxItem>
                </telerik:RadComboBox.Items>
            </telerik:RadComboBox>
            <telerik:RadComboBox Name="Combo3" Text="Eleven"
                                 Margin="2" IsEditable="True"
                                 GotFocus="RadComboBox_GotFocus"
                                 LostFocus="RadComboBox_LostFocus">
                <telerik:RadComboBox.Items>
                    <telerik:RadComboBoxItem>Nine</telerik:RadComboBoxItem>
                    <telerik:RadComboBoxItem>Ten</telerik:RadComboBoxItem>
                    <telerik:RadComboBoxItem>Eleven</telerik:RadComboBoxItem>
                    <telerik:RadComboBoxItem>Twelve</telerik:RadComboBoxItem>
                </telerik:RadComboBox.Items>
            </telerik:RadComboBox>
        </StackPanel>
         
        <ListBox Grid.Row="1" Height="Auto" HorizontalAlignment="Stretch" Name="EventList" VerticalAlignment="Stretch" Width="Auto" />
    </Grid>
</Window>

Imports Telerik.Windows.Controls
 
Class MainWindow
 
    Private Sub RadComboBox_GotFocus(sender As System.Object, e As System.Windows.RoutedEventArgs)
        Dim cbo = CType(sender, RadComboBox)
        AddEventToList(cbo.Name, "GotFocus")
    End Sub
 
    Private Sub RadComboBox_LostFocus(sender As System.Object, e As System.Windows.RoutedEventArgs)
        Dim cbo = CType(sender, RadComboBox)
        AddEventToList(cbo.Name, "LostFocus")
    End Sub
 
    Private Sub AddEventToList(ByVal ctrlName As String, ByVal eventName As String)
        EventList.Items.Add(String.Format("{0} : {1} : {2}", EventList.Items.Count, ctrlName, eventName))
        EventList.ScrollIntoView(EventList.Items(EventList.Items.Count - 1))
    End Sub
 
End Class

5 Answers, 1 is accepted

Sort by
0
Ventzi
Telerik team
answered on 05 Mar 2013, 11:21 AM
Hello Brian,

I've created a sample project with your code, but I wasn't able recreate the issue that you've described. The first RadComboBox on which I click, fires GotFocus event first. When I then click on the second RadComboBox, the first RadComboBox fires LostFocus and then the second fires GotFocus which is normal behavior in WPF. If you need more info about the focus, please visit this site.

If you still have this issue, please open a support ticket and provide us a sample project or short video in order to be able to assist you accordingly.


Greetings,
Ventzi
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
John
Top achievements
Rank 1
answered on 09 Dec 2013, 07:32 PM
**primary issue resolved by changing mousebuttondown to mousebuttonup event. The item that could be clicked is a rectangle and thus does not have a click event. Not my code initially and I wonder why there is not an image instead - as it is a hierarchical tree for traffic lights and the previous developer used a simple colored rectangle which is fine but not very pretty. Oh well. Good to know that the Lost Focus of one control is NOT fired immediately after losing focus - it was only being fired after the mouse down got taken care of.

I still would like to know why the LostFocus is fired when the control receives focus....?

Thanks

Hello - I am having a very similar problem as this, along with other issues related to the LostFocus property and how it's event handler is fired. First, it is clear that by stepping through code with the VS debugger (VS 2012 Ultimate), that the LostFocus event handler is fired when the control in question RECEIVES focus - and it appears to happen two times!!! In my case this is a RadRichTextBox.

Additionally, there in another control on the view that has an event handler that gets called before the LostFocus - so my simple question is this: is there a way to prioritize handler calls in WPF?

I think our application would be unwieldly to try and get you to recreate the situation. If I don't fix it by day's end I will video it and send that.

But the main question - why does LostFocus fire when the control receives focus, and why does the control I am in (the RTB)'s LostFocus NOT fire before another control's MouseLeftButtonDown event when I leave it?

(I may have answered part of this: why did the previous developer use MLBD instead of OnClick?) I will try changing that and see what happens....thanks for any tips/help.

Sincerely,

John Marno
Software Developer
Trihydro Corporation


0
Rosen Vladimirov
Telerik team
answered on 12 Dec 2013, 10:44 AM
Hi John,

I'm a little bit confused what is the exact issue you are facing which are the controls that you are using. Could you clarify the issue and the problems you have faced? I'm looking forward to hearing from you.

Regards,
Rosen Vladimirov
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
John
Top achievements
Rank 1
answered on 12 Dec 2013, 11:44 PM
Hi - here is the RTB XAML that you can assume I have just typed some text in to...

<local:InspectionRichTextbox x:Name="ChildMaintenanceItemDescriptionRTB" HorizontalAlignment="Left" Height="75" Width="600" VerticalAlignment="Stretch" LostFocus="ChildMaintenanceItemDescriptionRTB_LostFocus" RichText="{Binding Description, Mode=TwoWay}" Grid.Row

 

 

 

 

 

 

 

 

 

="1" />

And here is the XAML for the traffic light which the User will sometimes click on before clicking anywhere else. Note that I have already fixed the initial question - I changed the MouseDown to MouseUp and now the LostFocus on the RTB gets called first. However I don't like that initially Leaving the RTB still took a back seat to the MouseDown on the rectangle. FYI the RTB here is behaving strangely. Note the extra lines after the Grid.Row above but when I press delete it adds additional lines? Anyways my main question - how to control precisely the events is still murky, and second question is why does LostFocus get called when the RTB receives focus? I can demonstrate that in the debugger....


<Rectangle Cursor="Hand" Width="16" ToolTip="Click to set inspection topic to 'Not Inspected' (1)" Height="16" Fill="{Binding BoolCollection, Converter={StaticResource ResourceKey=BrushSelection}}" Stroke="Black" Margin="5,0,0,0" MouseLeftButtonUp="OutOfComplianceRect_MouseLeftButtonUp" x:Name="OutOfComplianceRect"/>

 

 

 

0
Petya
Telerik team
answered on 17 Dec 2013, 04:49 PM
Hi,

We are still a bit confused by your overall scenario. Generally, rectangles are not elements that are focusable, so I am not sure how the LostFocus event is raised in the first place.

As to why it is raised several times, this is a known issue in RadRichTextBox. It is related to the fact that the caret in the control is represented by a TextBox which receives the focus and gives it to the editor after that. At this point this behavior is not scheduled for resolution and I cannot tell when it might be changed.

Could you please provide more details on the overall scenario you are trying to achieve? I understand that you are trying to handle some events, but please specify what the end goal is. Maybe we will be able to suggest another approach to satisfy the requirements.

I'm looking forward to your reply.

Regards,
Petya
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
ComboBox
Asked by
Brian Scott
Top achievements
Rank 1
Answers by
Ventzi
Telerik team
John
Top achievements
Rank 1
Rosen Vladimirov
Telerik team
Petya
Telerik team
Share this question
or