Click here to Skip to main content
15,902,635 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a ListBox in a UserControl and it is a little small. I want when the user hovers the mouse over it to make it longer so they can see more items in the list.

So when the mouse is over the ListBox I want it to grow to 3 times the current length, I also don't want it to stretch out the rest of the control it is sitting in.
Posted
Comments
Sergey Alexandrovich Kryukov 13-Dec-13 19:08pm    
I don't think it's going to be a good design, but what's the problem? Can't you handle the mouse events, such as MouseEnter and MouseLeave and do whatever you want when they are invoked?
The problem is: if you make a list box bigger, some other elements should become smaller, of you should enlarge your window, to give more room, which may not be available.
—SA
bowlturner 16-Dec-13 9:13am    
What I want is on mouseover for the ListBox to expand 'on top' of all the other controls, Kind of like how a dropdown list box expands when you click on it.

C#
listBox1.MouseEnter += new System.EventHandler(listBox1_MouseEnter);
listBox1.MouseLeave += new System.EventHandler(listBox1_MouseLeave);

private void listBox1_MouseEnter(object sender, System.EventArgs e) 
{
    listBox1.Size = new Size(100, 100) // change this to the larger size you want
}

private void listBox1_MouseLeave(object sender, System.EventArgs e) 
{
    listBox1.Size = new Size(100, 100) // change this to the original size
}
 
Share this answer
 
Please see my comment to the question. All the action you need can be done by handling these two events by the list box:
http://msdn.microsoft.com/en-us/library/system.windows.uielement.mouseenter(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.uielement.mouseleave(v=vs.110).aspx[^].

The question is (see the second statement of my comment to the question explaining the problem): what to do? You can do different things. You can temporarily reduce the room for some other controls (such as TextBox) and give more room to your list box. One of the simple ways to do this would be using the open-source control SplitContainer:
http://wpfsplitcontainer.codeplex.com/[^].

You could put your list box as a child of one of the split parts, and leave another parts for some other control(s). When your mouse enters the list box, you can change the split ratio to give more room to your list box.

—SA
 
Share this answer
 
Unless:

1. your layout provides room for the ListBox to expand and contract without any problems of overlapping, or being overlapped by, other Controls or Windows:

... or ...

2. you re-size the UserControl the ListBox is in to display the ListBox as you wish, and possibly adjust the positions of other controls, inside and outside, the UserControl:

You may have the ListBox either:

1. clipped where it goes outside the bounds of its UserControl

2. overlapping other controls

3. being overlapped by other controls
 
Share this answer
 
Comments
bowlturner 16-Dec-13 9:15am    
right, I want it to overlap the other controls and then on mouseout it will shrink back down unhiding the covered controls.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900