Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
216 views
in Technique[技术] by (71.8m points)

c# - DropDown Menu with ScrollBar in .NET


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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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):

Windows 7 breadcrumb sample

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);
    }
}

EDIT :

To get the selected item, one way is the following:

// change some properties (for selection) and subscribe the ItemActivate 
// event of the listView
listView.HotTracking = true;
listView.Activation = ItemActivation.OneClick;
listView.ItemActivate += new EventHandler(listView_ItemActivate);


// the click on the item invokes this method
void listView_ItemActivate(object sender, EventArgs e)
{
    var listview = sender as ListView;
    var item = listview.SelectedItems[0].ToString();
    var dropdown = listview.Parent as ToolStripDropDown;
    // unsubscribe the event (to avoid memory leaks)
    listview.SelectedIndexChanged -= listView_ItemActivate;
    // close the dropdown (if you want)
    dropdown.Close();

    // do whatever you want with the item
    MessageBox.Show("Selected item is: " + item);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...