Skip to content

Commit 9b376b8

Browse files
committed
Improve ListBox
1 parent b422dca commit 9b376b8

6 files changed

+56
-5
lines changed

Properties/Resources.Designer.cs

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Properties/Resources.resx

+4-1
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,7 @@
6262
<data name="banner" type="System.Resources.ResXFileRef, System.Windows.Forms">
6363
<value>..\Resources\banner.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
6464
</data>
65-
</root>
65+
<data name="icon" type="System.Resources.ResXFileRef, System.Windows.Forms">
66+
<value>..\Resources\icon.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
67+
</data>
68+
</root>

rdp-portal.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,14 @@
201201
<DependentUpon>Settings.settings</DependentUpon>
202202
<DesignTimeSharedInput>True</DesignTimeSharedInput>
203203
</Compile>
204-
<None Include="Resources\icon.ico" />
205204
</ItemGroup>
206205
<ItemGroup>
207206
<None Include="App.config" />
208207
</ItemGroup>
209208
<ItemGroup>
210209
<Content Include="app.manifest" />
211210
<Content Include="Resources\banner.png" />
211+
<Content Include="Resources\icon.ico" />
212212
</ItemGroup>
213213
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
214214
<Import Project="packages\Fody.6.6.4\build\Fody.targets" Condition="Exists('packages\Fody.6.6.4\build\Fody.targets')" />

rdp-portal.sln.DotSettings.user

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
22
<s:Boolean x:Key="/Default/ResxEditorPersonal/CheckedGroups/=rdp_002Dportal_002FMainForm/@EntryIndexedValue">False</s:Boolean>
33
<s:Boolean x:Key="/Default/ResxEditorPersonal/CheckedGroups/=rdp_002Dportal_002FProperties_002FResources/@EntryIndexedValue">True</s:Boolean>
4-
<s:Boolean x:Key="/Default/ResxEditorPersonal/Initialized/@EntryValue">True</s:Boolean></wpf:ResourceDictionary>
4+
<s:Boolean x:Key="/Default/ResxEditorPersonal/Initialized/@EntryValue">True</s:Boolean>
5+
<s:Boolean x:Key="/Default/ResxEditorPersonal/ShowOnlyErrors/@EntryValue">False</s:Boolean></wpf:ResourceDictionary>

src/MainForm.Designer.cs

+5-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/MainForm.cs

+34
Original file line numberDiff line numberDiff line change
@@ -205,5 +205,39 @@ private void buttonAbout_Click(object sender, EventArgs e) {
205205
private void listBox_MouseDoubleClick(object sender, MouseEventArgs e) {
206206
buttonConnect_Click(sender, e);
207207
}
208+
209+
/**
210+
* From https://stackoverflow.com/questions/8333282/how-can-i-include-icons-in-my-listbox
211+
*/
212+
private void listBox_DrawItem(object sender, DrawItemEventArgs e) {
213+
if (e.Index == -1)
214+
return;
215+
216+
e.DrawBackground();
217+
Brush myBrush = Brushes.Black;
218+
219+
220+
var iconWidth = listBox.ItemHeight;
221+
var iconMargin = 4;
222+
var textMargin = (iconWidth - 18) / 2;
223+
var rect = new Rectangle(e.Bounds.X + iconMargin, e.Bounds.Y, iconWidth, iconWidth);
224+
//assuming the icon is already added to project resources
225+
226+
e.Graphics.DrawIcon(rdp_portal.Properties.Resources.icon, rect);
227+
228+
var profile = (Profile)listBox.Items[e.Index];
229+
230+
e.Graphics.DrawString(
231+
profile.Name,
232+
e.Font,
233+
myBrush,
234+
new Rectangle(e.Bounds.X + iconMargin * 2 + iconWidth, e.Bounds.Y + textMargin, e.Bounds.Width, e.Bounds.Height),
235+
StringFormat.GenericDefault
236+
);
237+
238+
// If the ListBox has focus, draw a focus rectangle around the selected item.
239+
e.DrawFocusRectangle();
240+
}
241+
208242
}
209243
}

0 commit comments

Comments
 (0)