{"id":5833,"date":"2007-07-03T19:28:00","date_gmt":"2007-07-03T19:28:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vbteam\/2007\/07\/03\/linq-cookbook-recipe-5-concatenating-the-selected-strings-from-a-checkedlistbox-kit-george\/"},"modified":"2024-07-05T14:44:19","modified_gmt":"2024-07-05T21:44:19","slug":"linq-cookbook-recipe-5-concatenating-the-selected-strings-from-a-checkedlistbox-kit-george","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/vbteam\/linq-cookbook-recipe-5-concatenating-the-selected-strings-from-a-checkedlistbox-kit-george\/","title":{"rendered":"LINQ Cookbook, Recipe 5: Concatenating the selected strings from a CheckedListBox (Kit George)"},"content":{"rendered":"<dd>\n<h1 class=\"entryviewheading\"><font size=\"3\"><font face=\"Calibri\">Ingredients:<\/p>\n<p><\/font><\/font><\/h2>\n<p class=\"MsoListParagraph\"><span><span><font face=\"Calibri\" size=\"3\">&#8211;<\/font><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><font size=\"3\"><font face=\"Calibri\">Visual Studio 2008 (Beta2 or Higher)<\/font><\/font><\/p>\n<p class=\"MsoListParagraph\"><font face=\"Calibri\" size=\"3\"><\/font>&nbsp;<\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">Categories: LINQ-To-Objects, LINQ and WinForms<\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\"><\/font><\/font>&nbsp;<\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">Instructions:<\/p>\n<p><\/font><\/font><\/p>\n<ul>\n<li>\n<div class=\"MsoListParagraph\"><font size=\"3\"><font face=\"Calibri\">Open Visual Studio 2008, and Click &lsquo;File\/New Project&rsquo;. Create a new Windows Forms Application project<\/font><\/font><\/div>\n<\/li>\n<li>\n<div class=\"MsoListParagraph\"><span><span><font face=\"Calibri\" size=\"3\">From the toolbox, <\/font><\/span><\/span><font size=\"3\"><font face=\"Calibri\">drag a CheckedListBox to the main form, and a button. Ensure the listbox is large enough to view multiple items<\/font><\/font><\/div>\n<\/li>\n<li>\n<div class=\"MsoListParagraph\"><font face=\"Calibri\" size=\"3\">DoubleClick the form to creat the load event. Add this code to the load event (this simply populates the CheckedListBox with entries)<\/font><\/div>\n<\/li>\n<\/ul>\n<p class=\"MsoListParagraph\"><span><span><span><\/p>\n<p><font size=\"2\">CheckedListBox1.Items.AddRange( _<\/font><\/p>\n<p><\/span><\/span><\/span><\/p>\n<dd><span><span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp; <font color=\"#0000ff\" size=\"2\">New<\/font><font size=\"2\"> <\/font><font color=\"#0000ff\" size=\"2\">String<\/font><font size=\"2\">() {<\/font><font color=\"#a31515\" size=\"2\">&#8220;Apple&#8221;<\/font><font size=\"2\">, <\/font><font color=\"#a31515\" size=\"2\">&#8220;Orange&#8221;<\/font><font size=\"2\">, <\/font><font color=\"#a31515\" size=\"2\">&#8220;Banana&#8221;<\/font><font size=\"2\">, _<\/font><\/p>\n<p><\/span><\/p>\n<p><\/span><\/span><\/dd>\n<dd><span><span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp; <font color=\"#a31515\" size=\"2\">&#8220;Avocado&#8221;<\/font><font size=\"2\">, <\/font><font color=\"#a31515\" size=\"2\">&#8220;Tomato&#8221;<\/font><font size=\"2\">, <\/font><font color=\"#a31515\" size=\"2\">&#8220;Tamarillo&#8221;<\/font><font size=\"2\">, _<\/font><\/p>\n<p><\/span><\/p>\n<p><\/span><\/span><\/dd>\n<dd><span><span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp; <font color=\"#a31515\" size=\"2\">&#8220;Kiwifruit&#8221;<\/font><font size=\"2\">, <\/font><font color=\"#a31515\" size=\"2\">&#8220;Cherry&#8221;<\/font><font size=\"2\">})<\/font><\/p>\n<p><\/span><\/p>\n<p><\/span><\/span><\/dd>\n<dd><span><span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p><\/span>&nbsp;<span>&nbsp;&nbsp;&nbsp; <\/span><\/p>\n<li>\n<div class=\"MsoListParagraph\"><font size=\"3\"><font face=\"Calibri\">Create the click event for the button (in the code editors dropdowns, in the left drop down, select Button1) and add this code<\/font><\/font><\/div>\n<\/li>\n<p><\/span><\/span><\/dd>\n<p class=\"MsoListParagraph\">&nbsp;<\/p>\n<p class=\"MsoListParagraph\"><span><span><span><font size=\"2\">MsgBox( _<\/font><\/span><\/span><\/span><\/p>\n<dd>\n<p class=\"MsoNormal\"><span><span><span>&nbsp; <font color=\"#0000ff\" size=\"2\">Aggregate<\/font><font size=\"2\"> Box <\/font><font color=\"#0000ff\" size=\"2\">In<\/font><font size=\"2\"> CheckedListBox1.CheckedItems _<\/font><\/span><\/span><\/span><\/p>\n<\/dd>\n<dd>\n<p class=\"MsoNormal\"><span><span><span>&nbsp;&nbsp;<font color=\"#0000ff\" size=\"2\">Into<\/font><font size=\"2\"> Concat())<\/font><\/span><\/span><\/span><\/p>\n<\/dd>\n<ul>\n<li>\n<div class=\"MsoNormal\"><span><span><span><font size=\"2\"><font face=\"Calibri\" size=\"3\">At the very bottom of the code (after End Class) add this code. This code creates&nbsp;the&nbsp;aggregate &#8216;Concat&#8217; function which enables this code to be used on the &#8216;Into&#8217; above<\/font><\/font><\/span><\/span><\/span><span><span><span>&nbsp;<\/span><\/span><\/span><\/div>\n<\/li>\n<\/ul>\n<p class=\"MsoNormal\"><span><span><span><font color=\"#0000ff\" size=\"2\">Public<\/font><font size=\"2\"> <\/font><font color=\"#0000ff\" size=\"2\">Module<\/font><font size=\"2\"> AggregateModule<\/font><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span><span><font size=\"2\">&nbsp; &lt;Extension()&gt; <\/font><font color=\"#0000ff\" size=\"2\">Public<\/font><font size=\"2\"> <\/font><font color=\"#0000ff\" size=\"2\">Function<\/font><font size=\"2\"> Concat(<font color=\"#0000ff\">Of<\/font> Type)<\/font><font size=\"2\">( _<\/font><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span><span><\/span><span><font color=\"#0000ff\" size=\"2\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ByVal<\/font><font size=\"2\">&nbsp;ie <\/font><font color=\"#0000ff\" size=\"2\">As<\/font><font size=\"2\"> IEnumerable(<\/font><font color=\"#0000ff\" size=\"2\">Of<\/font><font size=\"2\"> Type)) <\/font><font color=\"#0000ff\" size=\"2\">As<\/font><font size=\"2\"> <\/font><font color=\"#0000ff\" size=\"2\">String<\/font><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span><span><\/span><span><font color=\"#0000ff\"><font size=\"2\">&nbsp;&nbsp;&nbsp; Dim<\/font><\/font><font size=\"2\"> str <\/font><font color=\"#0000ff\" size=\"2\">As<\/font><font size=\"2\"> <\/font><font color=\"#0000ff\" size=\"2\">String<\/font><font size=\"2\"> = <\/font><font color=\"#a31515\" size=\"2\">&#8220;&#8221;<\/font><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span><span><font color=\"#a31515\">&nbsp;&nbsp;&nbsp; <\/font><\/span><span><font color=\"#0000ff\" size=\"2\">For<\/font><font size=\"2\"> <\/font><font color=\"#0000ff\" size=\"2\">Each<\/font><font size=\"2\"> item <\/font><font color=\"#0000ff\" size=\"2\">In<\/font><font size=\"2\"> ie<\/font><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span><span>&nbsp;&nbsp;&nbsp; <\/span><span><font color=\"#0000ff\" size=\"2\">If<\/font><font size=\"2\"> str &lt;&gt; <\/font><font color=\"#a31515\" size=\"2\">&#8220;&#8221;<\/font><font size=\"2\"> <\/font><font color=\"#0000ff\" size=\"2\">Then<\/font><font size=\"2\"> str &amp;= <\/font><font color=\"#a31515\" size=\"2\">&#8220;,&#8221;<\/font><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span><span><\/span><span><font color=\"#a31515\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/font><font size=\"2\">str &amp;= item.ToString()<\/font><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span><span><\/span><span>&nbsp;&nbsp;&nbsp; <font color=\"#0000ff\" size=\"2\">Next<\/font><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span><span><\/span><span><font color=\"#0000ff\">&nbsp;&nbsp;&nbsp; <font size=\"2\">Return<\/font><\/font><font size=\"2\"> str<\/font><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span><span><\/span><span>&nbsp; <font color=\"#0000ff\" size=\"2\">End<\/font><font size=\"2\"> <\/font><font color=\"#0000ff\" size=\"2\">Function<\/font><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span><span><\/span><span><font color=\"#0000ff\" size=\"2\">End<\/font><font size=\"2\"> <\/font><font color=\"#0000ff\" size=\"2\">Module<\/font><\/span><\/span><\/span><\/p>\n<p><span><\/p>\n<ul>\n<li>\n<div class=\"MsoNormal\"><span><span><span><font size=\"2\"><font face=\"Calibri\" size=\"3\">Alternatively,&nbsp;change the code in the button to be the following and remove the function we created (this is less code, but if you&#8217;re going to&nbsp;use this code a lot, then writing the Concat function is preferred)<\/font><\/font><\/span><\/span><\/span><span><span><span>&nbsp;<\/span><\/span><\/span><\/div>\n<\/li>\n<\/ul>\n<p class=\"MsoNormal\"><span><span><span><\/span><\/span><\/span><span><span><span><font size=\"2\">MsgBox((<\/font><font color=\"#0000ff\" size=\"2\">From<\/font><font size=\"2\"> c <\/font><font color=\"#0000ff\" size=\"2\">In<\/font><font size=\"2\">&nbsp;_<\/font><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span><span><font size=\"2\">&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; CheckedListBox1.CheckedItems).<\/font><font color=\"#0000ff\" size=\"2\">Aggregate<\/font><font size=\"2\">( _<\/font><\/span><\/span><\/span><\/p>\n<dd>\n<p class=\"MsoNormal\"><span><span><span>&nbsp;&nbsp; <font color=\"#0000ff\" size=\"2\">Function<\/font><font size=\"2\">(<\/font><font color=\"#0000ff\" size=\"2\">ByVal<\/font><font size=\"2\"> x, <\/font><font color=\"#0000ff\" size=\"2\">ByVal<\/font><font size=\"2\"> y) x + <\/font><font color=\"#a31515\" size=\"2\">&#8220;,&#8221;<\/font><font size=\"2\"> + y))<\/font><\/span><\/span><\/span><\/p>\n<\/dd>\n<p><\/span><\/dd>\n","protected":false},"excerpt":{"rendered":"<p>Ingredients: &#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Visual Studio 2008 (Beta2 or Higher) &nbsp; Categories: LINQ-To-Objects, LINQ and WinForms &nbsp; Instructions: Open Visual Studio 2008, and Click &lsquo;File\/New Project&rsquo;. Create a new Windows Forms Application project From the toolbox, drag a CheckedListBox to the main form, and a button. Ensure the listbox is large enough to view multiple items DoubleClick [&hellip;]<\/p>\n","protected":false},"author":260,"featured_media":8818,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[192,195],"tags":[90,93,94,166],"class_list":["post-5833","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-featured","category-visual-basic","tag-kit-george","tag-linq-cookbook","tag-linqvb9","tag-vb2008"],"acf":[],"blog_post_summary":"<p>Ingredients: &#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Visual Studio 2008 (Beta2 or Higher) &nbsp; Categories: LINQ-To-Objects, LINQ and WinForms &nbsp; Instructions: Open Visual Studio 2008, and Click &lsquo;File\/New Project&rsquo;. Create a new Windows Forms Application project From the toolbox, drag a CheckedListBox to the main form, and a button. Ensure the listbox is large enough to view multiple items DoubleClick [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/5833","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/users\/260"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/comments?post=5833"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/5833\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media\/8818"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media?parent=5833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/categories?post=5833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/tags?post=5833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}