//This method is used to dynamically populate the form with buttons. //Reference on LayoutPanel: https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/walkthrough-arranging-controls-on-windows-forms-using-a-flowlayoutpanel#positioning-controls-using-docking-and-anchoring private void PopulateMenu(List <Type> xAcccessibleViews) { Special_View instance; Buttons = new List <Button>(); for (int i = 0; i < xAcccessibleViews.Count; i++) { //Creating an instance of a view to get its attributes. instance = (Special_View)(Activator.CreateInstance(xAcccessibleViews[i], new object[] { false })); Buttons.Add(new Button()); Buttons[i].Size = new System.Drawing.Size(layoutPanel.Width, 35); Buttons[i].Cursor = System.Windows.Forms.Cursors.Hand; Buttons[i].FlatStyle = System.Windows.Forms.FlatStyle.Flat; Buttons[i].FlatAppearance.BorderSize = 0; Buttons[i].Font = new System.Drawing.Font("Corbel", 8F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); Buttons[i].ForeColor = instance.GetColour(); Buttons[i].Name = "btn" + i.ToString(); Buttons[i].Tag = i; Buttons[i].TabIndex = i; Buttons[i].Text = instance.GetTitle(); Buttons[i].TextAlign = System.Drawing.ContentAlignment.MiddleLeft; //Spacing between buttons Buttons[i].Margin = new Padding(0, 0, 0, 0); layoutPanel.Controls.Add(Buttons[i]); //Special rules for the Logout button. if (i == (xAcccessibleViews.Count - 1)) { Buttons[i].Text = "Logout"; Buttons[i].BackColor = Color.WhiteSmoke; Buttons[i].Margin = new Padding(0, 10, 0, 0); } // Doing the funcitonal button setup //Uniform way of creating the click event Buttons[i].Click -= (sender, e) => { }; Buttons[i].Click += (sender, e) => { Special_View viewInstance = (Special_View)(Activator.CreateInstance(xAcccessibleViews[((Button)sender).TabIndex], new object[] { false })); MenuElement_Changed(new object[] { sender, viewInstance.GetColour() }, e); SetUpButton(e, xAcccessibleViews[((Button)sender).TabIndex], ((Button)sender)); }; } }
//Event for trashing out the Current View and going back to Login screen. private void Logout_Prepare(object sender, EventArgs e) { // Disposing all Views. while (false != true) { if (Current_View.GetPrevious_View() != null) { Special_View ViewTemp = Current_View; Current_View = Current_View.GetPrevious_View(); ViewTemp.Dispose(); } else { Current_View.Dispose(); break; } } // Preparing the login screen. Login_Prepare(sender, e); }
private void SetView(Type xSpecialView, Button xBtn, List <object> xAdditionalParam = null, Special_View CView = null) { List <object> Parameters = new List <object>(); // Use this if a view's constructor has more additional parameters (besides toDisplay). Parameters.Add(true); if (xAdditionalParam != null) { Parameters.AddRange(xAdditionalParam); } Special_View ViewTemp = (Special_View)(Activator.CreateInstance(xSpecialView, Parameters.ToArray())); // Update the status by invoking the Event inside the Special View. ViewTemp.StatusUpdate -= new EventHandler(StatusUpdate); ViewTemp.StatusUpdate += new EventHandler(StatusUpdate); if (CView != null) { ViewTemp.SetPrevious_View(CView); } if (xBtn != null) { ViewTemp.SetAssigned_Btn(ref xBtn); } // I almost broke my head over here. if (ViewTemp is IUserDependent) { ((IUserDependent)ViewTemp).SendUser(Current_User); } View_Switch(ViewTemp); }