Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is it possible, in C#, to add buttons to each item in a list box?
If yes, how?

Chris
Posted
Updated 11-Feb-14 8:24am
v2
Comments
Kornfeld Eliyahu Peter 11-Feb-14 14:04pm    
Have you done anything so far? Show some effort (code or searching)! As is it ain't a question...
Sergey Alexandrovich Kryukov 11-Feb-14 17:04pm    
It really needs some help, for a beginner. I gave him the idea, please see Solution 1.
—SA

With a question like this, I think it's good to consider whether "you really need this:" since a ListBox gives you the equivalent of a 'Click Event for each of its Items in its 'SelectedIndexChanged EventHandler, why do you need Buttons ?

But, the simple answer to your question is "no." The WinForms ListBox is not able to accept internal WinForms Controls in a useful way. It is true that code like this:

for (int i = 0; i < 10; i++)
{
    string istr = i.ToString();

    Button newButton = new Button();
    newButton.Text = istr;
    newButton.Name = "btn" + istr;
    newButton.Visible = true;
    newButton.BackColor = Color.AliceBlue;
    newButton.ForeColor = Color.Black;

    listBox1.Controls.Add(newButton);
    newButton.Dock = DockStyle.Top;
    newButton.BringToFront();
}
Would fill a ListBox with a bunch of Buttons; but all you would have done is created a mess that simulated a Panel full of Buttons ! You'd never be able to select any of the ListBox Items.

However, using a ListView, there are several work-arounds to inserting Controls; and CodeProject has several articles, and other resources on this if you do a little research; such as: [^], [^].

The best article, imho, on extending the ListView is Philip Piper's classic, ObjectListView: [^].

A simpler solution would be to create a UserControl in which a ListBox, and a Panel (filled with Buttons) were combined. If you want to explore how to do this using a UserControl, you can ask for further details, and I will reply.

But, before you proceed further, I strongly suggest you re-evaluate if you really need both a ListBox and Buttons.
 
Share this answer
 
Comments
Running Fool 12-Feb-14 14:42pm    
I agree with re-evaluating what is needed.

What I am trying do is to mimic the Most Recent Files menu in Word 2013 / 365.
An OwnerDraw list box should provide 2 lines of text in 2 different fonts on each item.
But how are the PushPin controls on each item (file) done?

Some one suggested a DataGrid but I have not been able to try it yet.
There probably is a better way than adding buttons to every item in a listbox.

Thanks
Chris
No, why? Instead, use scrollable control filled with those buttons:
http://msdn.microsoft.com/en-us/library/system.windows.forms.scrollablecontrol%28v=vs.110%29.aspx[^].

[EDIT]

As Bill correctly pointed out in his comment below, the class ScrollableControl should be used not directly. The runtime type to be used could be, say, Panel which is derived from ScrollableControl, or your own derived class.

[END EDIT]

Better yet (actually, much better), use a single-columned DataGridView with System.Windows.Forms.DataGridViewButtonColumn. It will be the closest to the list you want, will also navigate by Up/Down keys, not Tab. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn%28v=vs.110%29.aspx[^].
 
Share this answer
 
v2
Comments
BillWoodruff 12-Feb-14 4:07am    
+4 The idea of using a DataGridView with a single column of Buttons is very interesting, but I think using 'ScrollableControl is questionable, because there is a caution in the MSDN docs stating that it is not "normally" used "directly" ("normally" one would use one of ScrollableControl's descendants, like Panel). But, I question both ideas in terms of their suitability for a beginner in .NET, since mastering DataGridView is quite difficult for many students.

Here is another case where if the OP had supplied more information, our responses could be much more focused.
Sergey Alexandrovich Kryukov 12-Feb-14 11:00am    
I agree. I actually meant to use it as Panel and do understand that it would not be obvious. After realizing that DataGridView would be much better, lost interest to it. Maybe I'll fix the answer by adding a note; will credit your comment.
Done. Than you, Bill.
—SA
Running Fool 12-Feb-14 14:54pm    
Sorry for the lack of details.
The idea is to mimic the Most Recent Files menu in Word 2013 / 356. I tried asking that directly and got no response so I asked a different way.
The hard part as I see it is the PushPin control for each file (hover over a file) that lets you pin it to the top of the list.
So: How does one mimic the Most Recent Files menu in Word 2013 / 356?
Thanks
Chris
Sergey Alexandrovich Kryukov 12-Feb-14 16:28pm    
My using regular Menu class. Dynamically add items to some "Recent Files" menu items as you use them. Or, rather, insert at the index 0, to have most recent on top. That's it, very easy.
And where you need a stack of buttons, I think single-columned DataGridView will be the best.
So, will you accept the answer formally now (green "Accept" button)?
—SA
I had worked out dynamically adding the recent file items to a regular menu. I wanted to do better because:
Showing the menu bar all the way across the top of the screen wastes a lot of space.
The regular menu is not touch screen friendly.
I don’t see any way in a regular menu to pin a most recent file adding it to the top of the recent file menu until it is unpinned. How would the buttons in a DataGridView would be visually linked to the menu items.
And last (I can’t believe I’m saying this!) the regular menu looks old fashioned.

I have been working on the scrollable panel idea. It is showing promise but seems overly complex so I was wondering if there was a better way.
Sorry, I guess I am not explaining this very well. If you take a look at the recent files feature in Microsoft Office Word 2013 or 365 (File / Open) you will see what I am trying to do. The new version of Word has a large scrollable area for selecting recent file with a large control of some type for each file. The control for each file has an icon for the file type, the file name in large font with the path below in smaller font to facilitate showing a longer path and a pin button on the right side to move the file from the dynamic most recent list to a static list of recent files.
How do they get that look? It really does strike me as a worthy question.


Thanks Again
Chris
 
Share this answer
 

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