Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset MouseIsOver when the control loses focus #12598

Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2747,6 +2747,12 @@ protected override void OnLostFocus(EventArgs e)

base.OnLostFocus(e);
_canFireLostFocus = false;

// When the control loses focus, reset the MouseIsOver to False.
if (DropDownStyle == ComboBoxStyle.Simple || !DroppedDown)
{
MouseIsOver = false;
}
}
}

Expand Down Expand Up @@ -3764,7 +3770,10 @@ protected override unsafe void WndProc(ref Message m)
}

using Graphics g = Graphics.FromHdcInternal((IntPtr)dc);
if ((!Enabled || FlatStyle == FlatStyle.Popup) && MouseIsOver)

// The pop up border needs to be drawn only when the control gets the focus or the mouse hovers over it.
bool isPopupAndActive = FlatStyle == FlatStyle.Popup && (Focused || MouseIsOver);
LeafShi1 marked this conversation as resolved.
Show resolved Hide resolved
if (!Enabled || isPopupAndActive)
{
FlatComboBoxAdapter.DrawPopUpCombo(this, g);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2750,6 +2750,46 @@ private void InitializeItems(ComboBox comboBox, int numItems)
Assert.Equal(numItems, comboBox.Items.Count);
}

[WinFormsFact]
public void ComboBox_OnLostFocus_AutoCompleteModeNone_DoesNotClearMatchingText()
LeafShi1 marked this conversation as resolved.
Show resolved Hide resolved
{
using ComboBox comboBox = new();
var comboBoxAccessor = comboBox.TestAccessor().Dynamic;
comboBoxAccessor._canFireLostFocus= true;
comboBox.AutoCompleteMode = AutoCompleteMode.None;
comboBox.AutoCompleteSource = AutoCompleteSource.ListItems;
comboBoxAccessor.MatchingText = "Test";
comboBox.DropDownStyle = ComboBoxStyle.Simple;
comboBoxAccessor.OnLostFocus(EventArgs.Empty);

string matchingText = comboBoxAccessor.MatchingText;
matchingText.Should().Be("Test");

// Verifies the fix for issue "https://github.com/dotnet/winforms/issues/12590"
// related to ComboBox dropdown button disappear when switching RightToLeft property.
comboBox.MouseIsOver.Should().BeFalse();
}

[WinFormsFact]
public void ComboBox_OnLostFocus_AutoCompleteModeNotNone_ClearsMatchingText()
{
using ComboBox comboBox = new();
var comboBoxAccessor = comboBox.TestAccessor().Dynamic;
comboBoxAccessor._canFireLostFocus = true;
comboBox.AutoCompleteMode = AutoCompleteMode.Suggest;
comboBox.AutoCompleteSource = AutoCompleteSource.ListItems;
comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxAccessor.MatchingText = "Test";
comboBoxAccessor.OnLostFocus(EventArgs.Empty);

string matchingText = comboBoxAccessor.MatchingText;
matchingText.Should().BeEmpty();

// Verifies the fix for issue "https://github.com/dotnet/winforms/issues/12590"
// related to ComboBox dropdown button disappear when switching RightToLeft property.
comboBox.MouseIsOver.Should().BeFalse();
}

private class OwnerDrawComboBox : ComboBox
{
public OwnerDrawComboBox()
Expand Down
Loading