WPF Combobox lostfocus event become an Infinite lopp

JewelsInfosystem

please have look on my code, this become an infinite loop while calling lostfocus event of combo box i need some data from database and user can select data only form list with typing options.

mainwindow.xaml

<Grid>
    <TextBox x:Name="txt1" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" Margin="112,10,0,0"/>
    <ComboBox x:Name="cmb"  GotFocus="cmbgotfocus" LostKeyboardFocus="cmblost"  KeyDown="cmbkeydown" IsEditable="True"  HorizontalAlignment="Left" VerticalAlignment="Top" Width="238" Margin="112,50,0,0"  />
</Grid>

Class

    private void cmbkeydown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Return || e.Key == Key.Escape)
        {
            cmb.IsDropDownOpen = false;
        }
        else
        {
            cmb.IsDropDownOpen = true;
        }
    }

    private void cmblost(object sender, RoutedEventArgs e)
    {
        if (cmb.SelectedIndex < 0 && cmb.Text!="" )
        {
            MessageBox.Show("Please select a valid data from list only", "Warning");
            cmb.Focus();
         }
    }
AnjumSKhan

If I got you correctly, you want user to type some text in the ComboBox, and if user's entry doesn't match any item, focus should remain on the TextBox present in the ComboBox.

<ComboBox x:Name="Cmb1" IsEditable="True" 
          Control.PreviewLostKeyboardFocus="Control_PreviewLostKeyboardFocus" ...> 

Handler code :

private void Control_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
    {
        if (!(e.OriginalSource is TextBox)) return;

        TextBox tb = (TextBox)e.OriginalSource;
        if (Cmb1.SelectedIndex < 0)
        {
            Cmb1.Text = "";
            e.Handled = true;
        }
    }

Please tell if this solves your issue.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

WPF TextBox lostFocus event trigger

WPF image's gotfocus/lostfocus event does not fired

WPF ComboBox event for ItemSource changed

WPF LostFocus of UserControl not fired

Textbox LostFocus event handling

SelectionChanged event not firing for derived ComboBox control - WPF

WPF - How to fire MouseLeftButtonUp event for ComboBox?

WPF ComboBox prevent event on select item by code

WPF ComboBox KeyUp Event for all comboboxes

c# wpf is there a mouse hover event for ComboBox

How to use LostFocus as a Command in WPF

Is there a built-in Angular lostfocus event?

For loop seems to become infinite

WPF DatePicker LostFocus fires seven times

WPF UserControl detect LostFocus ignoring children

How to update target from a lostfocus of the source WPF?

How to delay the LostFocus Event in VB6

LostFocus() event not triggered after application installation

LostFocus event of an Image isn't firing

WinForm Hosted ComboBox Leave Event does not fire within WPF

Add items to WPF combobox dynamically and later reset the value on some event

WPF Combobox event does not fire (using MVVM and Expression.Blend)

WPF set ComboBox to -1 index after click event of ComboBoxItem issue

WPF SelectionChange Event for ComboBox with Binding from database crashes the Program

WPF Caliburn.Micro ComboBox IsEditable Text Changed Event not triggering until after Action Event

In WPF, how can I have binding on LostFocus but validation on PropertyChanged?

Textbox lostfocus event doesn't fire when using a default button

Fire LostFocus event only when IsKeyboardFocusWithin Property has a false value

How to call LostFocus event of another control from the other control

TOP Ranking

HotTag

Archive