{"id":1279,"date":"2015-09-01T12:56:01","date_gmt":"2015-09-01T12:56:01","guid":{"rendered":"http:\/\/mindfusion.eu\/blog\/?p=1279"},"modified":"2021-01-20T16:51:25","modified_gmt":"2021-01-20T16:51:25","slug":"layout-management-with-the-winforms-dock-control","status":"publish","type":"post","link":"https:\/\/mindfusion.dev\/blog\/layout-management-with-the-winforms-dock-control\/","title":{"rendered":"Layout Management with the WinForms Dock Control"},"content":{"rendered":"<p>In this post we will show you how to build a sample application with customizable layout based on the WinForms Dock control, which is part of MindFusion WinForms pack. You can find further details about the control at its web page.<\/p>\n<p>For the purpose of the demonstration we&#8217;ve chosen an entertaining topic &#8211; a cooking recipes electronic book. Our book so far has only three recipes, all for sweets. Lets start by looking at the<\/p>\n<p><strong>I. Architecture of the sample.<\/strong><\/p>\n<p>The sample has two custom classes &#8211; Ingredient and Recipe. Each Ingredient has quantity, name and an image, which illustrates it. The Recipe class holds a strongly typed list with Ingredient objects, the title for each recipe, an image for the recipe, an icon for the recipe and preparation instructions.<\/p>\n<p>The application consists of the dock control, which contains four DockItem-s : for the recipes, for their images, for their ingredients and with the preparation instructions. Any dock item can be dragged, dropped, aligned, minimized, hidden etc.<\/p>\n<div id=\"attachment_1283\" style=\"width: 282px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2015\/09\/cook_book_api.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1283\" class=\"size-full wp-image-1283\" src=\"http:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2015\/09\/cook_book_api.png\" alt=\"The API of the cook book application\" width=\"272\" height=\"376\" srcset=\"https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2015\/09\/cook_book_api.png 272w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2015\/09\/cook_book_api-217x300.png 217w\" sizes=\"auto, (max-width: 272px) 100vw, 272px\" \/><\/a><p id=\"caption-attachment-1283\" class=\"wp-caption-text\">The API of the cook book application<\/p><\/div>\n<p><strong>II. The Dock Control.<\/strong><\/p>\n<p>We create an empty WinForms project and add the Dock control from the toolbox. The dock control fills the entire client area:<\/p>\n<pre>this.dockControl1.Dock = System.Windows.Forms.DockStyle.Fill;         \nthis.dockControl1.Location = new System.Drawing.Point(0, 0);           \nthis.dockControl1.Size = new System.Drawing.Size(784, 562);\n<\/pre>\n<p>These properties are set in the Property grid.<\/p>\n<p>Each dock item is created with a title and id, which helps us identify it.<\/p>\n<pre>titleItem = new DockItem() { Header = \"Recipe Name\", Id = \"Name\" };\ntitleItem.DockStyle = DockStyle.Left;      \ntitleItem.Content = GetContent(\"Name\");\n<\/pre>\n<p>The DockStyle property is responsible for the initial layout of the dock item. You can choose among various dock styles &#8211; fill, bottom, top and more.<\/p>\n<p>The GetContent method is very important. It prepares the controls that will be placed into each dock item and returns the appropriate one according to the parameter.<\/p>\n<p>Once the dock item is created, let&#8217;s not forget to add it:<\/p>\n<pre>dockControl1.Items.Add(titleItem);\n<\/pre>\n<p><strong>III. Creating the Content.<\/strong><\/p>\n<p>Let&#8217;s see how the content for a dock item is created. Let&#8217;s take the grid with the ingredients. It is identified with the &#8220;Ingredients&#8221; id:<\/p>\n<pre>private Control GetContent(string contentType)\n{\n  Control control = null;\n  ....\n  else if (contentType == \"Ingredients\")\n         {                \n             grid = new DataGridView();\n             grid.DefaultCellStyle.BackColor = Color.FromArgb(247, 226, 189);\n             grid.Dock = DockStyle.Fill;\n             grid.MultiSelect = false;\n             grid.DataSource = selectedRecipe.Ingredients;\n             grid.RowTemplate.MinimumHeight = 35;\n            \n             grid.RowHeadersWidthSizeMode =\n            DataGridViewRowHeadersWidthSizeMode.DisableResizing;\n             grid.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;\n             grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;\n             grid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;\n             grid.BackgroundColor = Color.FromArgb(253, 244, 247);\n\n             control = grid; \n         }\n...\nreturn control;\n<\/pre>\n<p>The idea is clear: you create the control, which will be rendered in the dock item and assign it to DockItem.Content. Here we make different customization for the grid &#8211; we change the color of table rows, adjust the height, turn off multiple row select and more.<\/p>\n<p><strong>IV. Changing the Content.<\/strong><\/p>\n<p>We change the content by handling the SelectedIndexChanged event for the ListView, which lists our recipes. When the user selects a new list item, we extract its Recipe and change the content of the other DockItem-s:<\/p>\n<pre>selectedRecipe = recipes[recipesListView.SelectedIndices[0]];\ntb.Text = selectedRecipe.Preparation;\ngrid.DataSource = selectedRecipe.Ingredients;\npictureBox.Image = Image.FromFile(selectedRecipe.ImageUrl);\n<\/pre>\n<p>Here is the final output of the sample application:<\/p>\n<div id=\"attachment_1284\" style=\"width: 809px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2015\/09\/winforms_dock_control.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1284\" class=\"size-full wp-image-1284\" src=\"http:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2015\/09\/winforms_dock_control.png\" alt=\"MindFusion WinForms Dock COntrol\" width=\"799\" height=\"592\" srcset=\"https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2015\/09\/winforms_dock_control.png 799w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2015\/09\/winforms_dock_control-300x222.png 300w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2015\/09\/winforms_dock_control-405x300.png 405w\" sizes=\"auto, (max-width: 799px) 100vw, 799px\" \/><\/a><p id=\"caption-attachment-1284\" class=\"wp-caption-text\">A Sample electronic cook book based on MindFusion WinForms Dock Control<\/p><\/div>\n<p>You can download the sample directly from this link:<\/p>\n<p align=\"center\"><a href=\"https:\/\/mindfusion.dev\/samples\/winforms\/ui\/CookBook.CS.zip\">MindFusion WinForms Layout Control Sample: Electronic Cook Book in C#<\/a><\/p>\n<p align=\"center\"><a href=\"https:\/\/mindfusion.dev\/samples\/winforms\/ui\/CookBook.VB.zip\">MindFusion WinForms Layout Control Sample: Electronic Cook Book in VB.NET<\/a><\/p>\n<p>Enjoy the fast, easy and straight-forwarded manner in which you can create a WinForms application with a flexible layout and multiple panels.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post we will show you how to build a sample application with customizable layout based on the WinForms Dock control, which is part of MindFusion WinForms pack. You can find further details about the control at its web &hellip; <a href=\"https:\/\/mindfusion.dev\/blog\/layout-management-with-the-winforms-dock-control\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[74,343],"tags":[264,228,342,265,284,29],"class_list":["post-1279","post","type-post","status-publish","format-standard","hentry","category-sample-code","category-ui","tag-c","tag-dock-control","tag-layout-manager","tag-sample","tag-vb-net","tag-winforms"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3RlKs-kD","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1279","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/comments?post=1279"}],"version-history":[{"count":7,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1279\/revisions"}],"predecessor-version":[{"id":2556,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1279\/revisions\/2556"}],"wp:attachment":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/media?parent=1279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/categories?post=1279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/tags?post=1279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}