MaterialSkin icon indicating copy to clipboard operation
MaterialSkin copied to clipboard

ComboBox Won't Display Items if Not Focused

Open dewdrinker19 opened this issue 4 years ago • 9 comments
trafficstars

Hello, great work here! When I use...

materialComboBox1.DroppedDown = true;

It drops down the ComboBox but does not show the items that were added by code unless the ComboBox is in focus.

I have text box that when I type in a word it searches an external api that fills the items in the ComboBox and then drops the ComboBox, the reason I don't want to give the combo box focus is so the user may continue typing in the TextBox if what they see doesn't show up on the list in the ComboBox without having to click back in the TextBox.

Thanks for any help.

dewdrinker19 avatar Nov 26 '20 10:11 dewdrinker19

Thanks for reporting. This is weird because I simply extend the combobox draw functions and override them with my own, can you try with the default win forms combobox to see if this still happens? Also try calling invalidate() or refresh() on the material combo to see if it helps.

leocb avatar Nov 26 '20 15:11 leocb

standard combobox works fine. I've been using it for quite a while. I just switched to Material so it looks way more modern. My code is pretty much the same. I just tried invalidate, refresh, and both before the droppeddown true. Neither of those three worked. It's very strange because if I do Focus then it works just fine but I run into the issue of not being able to continue typing. I'm wondering if it would be easier to just allow user to type whatever they want in the material combobox but I didn't see a way to enable that. Thank you very much for taking the time to reply, I wasn't expecting anything due to the holidays. I appreciate your time.

dewdrinker19 avatar Nov 27 '20 01:11 dewdrinker19

I have figured out it is something to to do with the draw mode. I just don't know what yet. HAHA

EDIT** I was able to get it to work but it doesn't look right at all. Formatting is all wrong. In the draw item behavior I put this....

e.DrawBackground(); if (e.Index > -1) e.Graphics.DrawString(materialComboBox1.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds);

It shows the items now but it is not the right format that goes along with when I click the materialComboBox and by clicking, it looks material design.

dewdrinker19 avatar Nov 27 '20 07:11 dewdrinker19

Thanks for reporting a fix, I will take a look when I have some time, but it won't be soon. The main reason why it probably doesn't look the same, is because I don't use the e.* parameters like e.Font, but instead I use the custom material design specific to this lib, take a look at other parts of the code to see how I draw the text and background.

leocb avatar Nov 30 '20 20:11 leocb

Thank you very much for pointing me in the right direction. I put the following code on DrawItem Event and works perfectly. I really appreciate the response. It is 100% worked as expected now.

    private void materialComboBox1_DrawItem(object sender, DrawItemEventArgs e)
    {
        e.DrawBackground();
        if (e.Index > -1)
        {
            e.Graphics.FillRectangle(SkinManager.BackgroundBrush, e.Bounds);
            if (e.State.HasFlag(DrawItemState.Focus))
            {
                e.Graphics.FillRectangle(SkinManager.BackgroundHoverBrush, e.Bounds);
            }
            using (NativeTextRenderer NativeText = new NativeTextRenderer(e.Graphics))
            {
                NativeText.DrawTransparentText(
                materialComboBox1.Items[e.Index].ToString(),
                SkinManager.getFontByType(MaterialSkinManager.fontType.Subtitle1),
                SkinManager.TextHighEmphasisColor,
                new Point(e.Bounds.Location.X + SkinManager.FORM_PADDING, e.Bounds.Location.Y),
                new Size(e.Bounds.Size.Width - SkinManager.FORM_PADDING * 2, e.Bounds.Size.Height),
                NativeTextRenderer.TextAlignFlags.Left | NativeTextRenderer.TextAlignFlags.Middle); ;
            }
        }
    }

dewdrinker19 avatar Dec 01 '20 06:12 dewdrinker19

If you'd be so kind to submit this as Pull Request, I will see to update the nugget package as soon as I can

leocb avatar Dec 01 '20 07:12 leocb

I'd do that but I'm not sure how to make it apply to every combBox without manually putting into into the event. (I'm very entry level with my knowledge in coding, figuring it out piece by piece through University of Google Search)

dewdrinker19 avatar Dec 02 '20 18:12 dewdrinker19

Hello from me. Great work leocb. I have an issue on combobox when I click to arrow for drop down, I get the following : image

Is there any suggestion to suspend this problem?

Kind Regards Angelos

akaiafas avatar Feb 17 '21 18:02 akaiafas

Seems like it's the same issue #122 fixed, try that one out, though, I haven't had the time to verify it yet.

leocb avatar Feb 17 '21 18:02 leocb