Question ToolStripDropDown renders at position 0,0 on first click

mark_dbg

New member
Joined
Apr 10, 2012
Messages
3
Programming Experience
10+
I'm trying to add a WebBrowser object into a ToolStripDropDownButton's drop-down. When I first click the button the drop-down shows at screen position 0,0. On subsequent clicks of the button the drop-down shows underneath the button like it should. Any idea on how to get the drop-down to show properly on the first click?

VB.NET:
        Dim browser As New WebBrowser
        browser.Url = New Uri("http://localhost/test.html")
        Dim host As New ToolStripControlHost(browser)
        Dim dropDown As New ToolStripDropDown
        dropDown.Items.Add(host)
        ToolStripDropDownButton1.DropDown = dropDown
 
Not sure why, but this code resolved the issue:
dropDown.Visible = True
 
Thanks JohnH. However, that doesn't solve what I need. That just seems to open the drop-down right off the bat.

I probably should have been a little more explicit in my original post, but what I'm trying to do is to create a toolbar for IE. I want to have certain drop-down buttons be populated dynamically with html. I've tried opening and then closing the drop-down via code on instantiation and that works somewhat. The problem then is that the user has to click on the drop-down button twice for it to show the drop-down.
 
That just seems to open the drop-down right off the bat.
That did not happen when I tested the code. The only effect adding that code for me was that the drop-down showed at correct location when I clicked the drop-down button first time.
 
hmmm... You're right. In the form application it does exactly what you said. In the IE toolbar app, it just opens the drop-down and it stays there.

I think I may have found a work-around though. If I do this:

VB.NET:
dropDown.Visible = true
dropDown.Close()

then it seems to work just fine. It's getting the flicker of the window opening in the top left and then closing right away, but it's better than nothing.

What I had tried before was:

VB.NET:
ToolStripDropDownButton1.ShowDropDown()
ToolStripDropDownButton1.HideDropDown()

which caused the drop down to show in the correct spot, but you had to hit the drop down button twice for it to activate.

Thanks for you help!
 
I tested in winforms, right. The first thought was to call CreateControl method, which is part of what Visible property setter also calls for controls that has not been created and assigned window handles yet, but that didn't have any effect in winforms.

The problem may be related to the drop-down not detecting any items, it is one of the cases where internal DropDownLocation returns Point.Empty. Again testing in winforms, if I add another empty element to the dropdown it shows at correct location first time:
dropDown.Items.Add(String.Empty)
 
Hide -> Populate -> Show

I had the same problem in my application.
What I did in my function was the following:

VB.NET:
private sub menu_click(ByVal sender as Object, ByVal e as EventArgs) Handles menu.click
menu.HideDropDown()
...
   [code for dynamically populate DropDown Items]
...
menu.ShowDropDown()
end sub

First line avoid that following clicks will draw items in 0,0 position.

I hope it will help you

;)
 
Back
Top