我知道類似的問題已經在這裏被問到過,但他們都導致same codeproject article不起作用。有人知道有圖標的工作列表框?如何在我的ListBox中包含圖標?
回答
請問ListView
是否適合您?這是我使用的。更容易,你可以使它看起來就像一個ListBox
。此外,大量的MSDN文檔可供您開始使用。
如何爲Windows顯示圖標窗體ListView控件
Windows窗體ListView控件可以從三個圖像 列表中顯示的圖標。 List,Details和SmallIcon視圖顯示來自SmallImageList屬性中指定的 圖像列表中的圖像。 LargeIcon 視圖顯示來自 LargeImageList屬性中指定的圖像列表中的圖像。列表視圖還可以顯示一組額外的圖標,在StateImageList屬性中設置,在大圖標或小圖標旁邊設置。有關圖像列表的詳細信息,請參閱ImageList 組件(Windows窗體)和如何:使用 Windows窗體ImageList組件添加或刪除圖像。
從How to: Display Icons for the Windows Forms ListView Control
這也可以工作。我也使用PNG圖形格式。 – James
如果你被困在WinForms中,那麼你必須自己繪製你的物品。
請參閱DrawItem event的示例。
插入少許不同的方法 - 不要使用一個列表框。 而不是使用該控件將我限制在其有限的一組屬性和方法中,我正在製作一個我自己的列表框。
這並不難,因爲它的聲音:
爲myListBox事件處理程序int yPos = 0;
Panel myListBox = new Panel();
foreach (Object object in YourObjectList)
{
Panel line = new Panel();
line.Location = new Point(0, Ypos);
line.Size = new Size(myListBox.Width, 20);
line.MouseClick += new MouseEventHandler(line_MouseClick);
myListBox.Controls.Add(line);
// Add and arrange the controls you want in the line
yPos += line.Height;
}
實施例 - 在選擇的行:
private void line_MouseClick(object sender, MouseEventArgs)
{
foreach (Control control in myListBox.Controls)
if (control is Panel)
if (control == sender)
control.BackColor = Color.DarkBlue;
else
control.BackColor = Color.Transparent;
}
的代碼示例以上未測試但是使用所描述的方法和發現非常方便和簡單。
如果您不想將ListBox更改爲ListView,則可以爲DrawItemEvent編寫一個處理函數。例如:
private void InitializeComponent()
{
...
this.listBox.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox_DrawItem);
...
}
private void listBox_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index == -1)
return;
// Draw the background of the ListBox control for each item.
e.DrawBackground();
var rect = new Rectangle(e.Bounds.X+10, e.Bounds.Y+8, 12, 14);
//assuming the icon is already added to project resources
e.Graphics.DrawIconUnstretched(YourProject.Properties.Resources.YouIcon, rect);
e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(),
e.Font, Brushes.Black, new Rectangle(e.Bounds.X + 25, e.Bounds.Y + 10, e.Bounds.Width, e.Bounds.Height), StringFormat.GenericDefault);
// If the ListBox has focus, draw a focus rectangle around the selected item.
e.DrawFocusRectangle();
}
你可以玩的矩形,設置圖標的位置正確
- 1. 如何在DataGrid控件中包含ListBox
- 2. 如何在我的iPhone應用程序中包含512X512圖標?
- 3. 如何在我的svg中包含字體棒圖標?
- 4. 如何在Installshield的AddFolderIcon()中包含圖標圖像
- 5. 如何在我的JSP中包含css?
- 6. 如何在列表視圖中包含json數據的標識
- 7. 如何在您的sass文件中包含Font Awesome圖標?
- 8. 如何在cmake中包含標題
- 9. 如何在NSString中包含上標?
- 10. 在WPF中,如何從包含ListBox的DataTemplate中將數據綁定到Window DataContext?
- 11. 如何在視圖中包含組合?
- 12. 如何在Odoo中包含視圖8
- 13. 如何在rails中包含flotr2圖表?
- 14. 我如何在標籤中包含jsp代碼?見例
- 15. 如何在React.js項目中包含FontAwesome圖標
- 16. 如何將默認警告圖標包含到圖片框中
- 17. 我無法在TCPDF標頭中包含圖像
- 18. 我如何將我的footer.html包含在我的base.html中
- 19. 包含標題,如何?
- 20. 我如何在WP主題中包含codeigniter視圖
- 21. 如何在html中的section標籤內包含div標籤
- 22. 如何在劇本點標籤的文本中包含下標
- 23. 如何將標題(包含文字和圖片)置於我的div中間?
- 24. 如何在類似城市的Web GUI中定位圖標? (包含圖像)
- 25. 如何在C#中創建包含多種尺寸/圖像的圖標文件
- 26. 我如何讓我的UITableViewCell包含兩個標籤?
- 27. 如何在我的JavaScript包中包含ext-searchbox.js
- 28. PyInstaller未能包含圖標
- 29. 在iPhone上的mailto:URL中包含HTML圖標標記
- 30. 如何在視圖中包含Ajax如果我不使用Web2py中的layout.html
你應該添加鏈接到CodeProject上的文章。 – Nasreddine
並提及什麼CodeProject文章不起作用。 – slugster