views:

55

answers:

3

Hi,
I'm trying to make a user control similar to the Windows Vista/7 breadcrumb bar used in windows explorer.

However, when I show the drop down menu for a breadcrumb with many sub items, I get a very long list that sometimes exceeds the screen size.
In the Windows Vista/7 example however, there are a maximum of 18 items displayed at a time and a scrollbar appears at the right when the number of sub items exceeds this number (18).

I wanted to know if anyone out there knows a way to replicate what Microsoft does.
[That is, how to place a drop down menu in a control with auto-scroll capability.]



Thanks.
Alex

+1  A: 

I recommend taking a look at it with Spy++. Everything is composed from standard Windows controls, heavily nested. The drop-down is implemented as, drumroll, a combo box. It is a largely forgotten customized one, named ComboBoxEx. I've never seen a .NET wrapper for it, probably because it does a job that's easily implemented by the plain old ComboBox wrapper in Windows Forms.

Just set its DrawMode property to OwnerDrawFixed and implement the DrawItem event to show the icon and the text. There's a very good example available in the MSDN Library article for it.

Hans Passant
+1  A: 

Windows 7/Vista breadcrumb looks similar to a list view. The following picture gives an example (on windows xp) of what I mean (the list appears clicking on the button):

alt text

and here's the code to get it:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        var button = sender as Button;

        // create fake items list
        List<string> strings = new List<string>();
        for (int i = 0; i < 36; i++)
            strings.Add("ITEM " + (i+1));
        var listViewItems = strings.Select(x => new ListViewItem(x, 0)).ToArray();

        // create a new list view
        ListView listView = new ListView();
        listView.View = View.SmallIcon;
        listView.SmallImageList = imageList1;
        listView.MultiSelect = false;

        // add items to listview
        listView.Items.AddRange(listViewItems);

        // calculate size of list from the listViewItems' height
        int itemToShow = 18;
        var lastItemToShow = listViewItems.Take(itemToShow).Last();
        int height = lastItemToShow.Bounds.Bottom + listView.Margin.Top;
        listView.Height = height;

        // create a new popup and add the list view to it
        var popup = new ToolStripDropDown();
        popup.AutoSize = false;
        popup.Margin = Padding.Empty;
        popup.Padding = Padding.Empty;
        ToolStripControlHost host = new ToolStripControlHost(listView);
        host.Margin = Padding.Empty;
        host.Padding = Padding.Empty;
        host.AutoSize = false;
        host.Size = listView.Size;
        popup.Size = listView.Size;
        popup.Items.Add(host);

        // show the popup
        popup.Show(this, button.Left, button.Bottom);
    }
}
digEmAll
Thanks! I never knew you could embed a Control [or its derivative] in a ToolStripDropDown so easily. You've been really helpful.
Alex Essilfie
+1  A: 

If you want to access the Vista API to render the bar, check out the Vista Bridge library. You can find a sample of a Bread Crumb bar control in one of the samples.

I am not sure if it will render on WinXP, however.

Groo
<pre>Your answer provided me with the Microsoft-built breadcrumb and I thank you for that.The solution, however did not take into account the scrollbar issue so I'll have to combine the your suggestion with digEmAll'sThanks.</pre>
Alex Essilfie