text: Windows Forms C# - Forms and Dialogs
  TUTORIALS 2018/02/02 22:01
  Forms and dialogs in Windows Forms are windows that contain controls. They can be different types: to have or to not have a frame, to be modular or not to be, to be stretchable or not to be, to be above all other windows or not to be, and so on.
  The System.Windows.Forms.Form class
  The System.Windows.Forms.Form class is a base class for all forms in Windows Forms GUI applications. It is a graphical form - a window or a dialog box that contains controls and manages navigation between them.
  Most windows have a frame and special buttons for closing, moving, and other standard operations. The appearance of the windows and the standard controls on their frame depend on the operating system graphics environment settings. The developer has only partial control over the appearance of the windows.
  The Form class is the successor to the Control, ScrollableControl, and ContainerControl classes, and inherits from them all their functionality, all their properties, events and methods.
  Most important properties of the Form Class
  - FormBorderStyle - specifies the type of the shape frame. The more commonly used frame types are:
  - Sizable - standard expandable frame. The user can change the dimensions of such frames.
  - FixedDialog - a fixed-size dialog. Such frames cannot be resized by users.
  - None - no frame. The whole space of the form is used for its contents.
  - FixedToolWindow - a fixed-size toolbox. The frame cannot be resized by users and is slightly narrower than the standard one.
  - Controls - contains a list of controls located in the form. The order of the controls in this list depends on the order in which they are plotted on the screen (Z-order) and the order in which the control is moved from one control to the other (tab order). The order of focus shift can be further adjusted by the TabStop and TabIndex properties.
  - Text - Title of the window. Unicode is used. Cyrillic, Latin, Greek and other alphabets of the Unicode standards can be used.
  - Size - window dimensions (width and height). Includes all the space occupied by the shape.
  - ClientSize - Dimensions of the inside of the shape (without its frame).
  - AcceptButton - the default button. This button is pressed automatically when the user presses the [Enter] key, regardless of which form control the focus is at that moment. The aim is to facilitate the user in completing information forms.
  - ControlBox - Specifies whether the form should contain the standard closure controls, minimization, and so on.
  - Icon - sets a window icon.
  - MinimumSize, MaximumSize - sets form size limits - maximum and minimum width and height. When trying to resize, the user is not allowed to set a size that is not within these limits.
  - Modal - returns whether the form is modal. When a form is modal while it is active, the user cannot work with other forms of the same application. Any attempt to switch to another form fails until the user closes the modal form.
  If an application displays at the same time several non-modal forms, the user will be able to move freely between them without closing them. The Modal property is read-only. Modality can be set initially but cannot be changed once the form is already displayed.
  Most important Form Class methods
  - Show() - Shows the form and makes it active (focuses it). The form is displayed in non-modal mode. Invoking this method is equivalent to assigning Visible = true. The implementation of this method ends immediately.
  - ShowDialog() - shows the form in modal mode and after it is closed, it returns a value of DialogResult type. This value contains information about the reason for closing the form. The implementation of the ShowDialog() method ends only after the form is closed, i.e. the method is blocking.
  - Close() - closes the form. When a form is closed, it disappears and the resources it uses are released. Once a form is closed, it can no longer be displayed. To temporarily hide a form, use the Hide () method instead of Close ().
  - LayoutMdi(...) - in MDI mode this method rearranges the child forms contained in the current form. The way the reorder is set by the programmer. Several types of rearrangement are supported - cascade, horizontal, vertical, and ohers.
  Most important events in the Form Class
  - Activated / Deactivate - invokes when the form is activated / deactivated (when the form gets / lost focus).
  - Closing - is invoked when trying to close the form (for example, when the user presses the default closing button). Execution may cause a denial of closure. The event puts in its arguments an instance of the CancelEventArgs class, which contains Boolean property Cancel, which can be used to close the shutdown.
  - Load - invoked once before the first form display. It can be used to initialize control status.
  Sample tasks
  1) Create an application to show the different types of MessageBox messages. Create an InputBox message similar to VisualBasic. Use ColorDialog to change the color of the form, FontDialog to change the text font in label and OpenFileDialog to load image to PictureBox.
  Solution
  Crete new Windows Forms Application Project and design the form as shown:
  Name controls as follows:
  - TextBox 1 – tbName1
  - TextBox 2 – tbName2
  - TextBox 3 – tbFromInputBox
  - Button 1 – bInputBox
  - Button 2 – bMessageBox
  - Button 3 – bAbortRetryIgnore
  - Button 4 – bOkCancel
  - Button 5 – bYesNoCancel
  - Button 6 – bRetryCancel
  - Button 7 – bQuestionIcon
  - Button 8 – bCriticalIcon
  - Button 9 – bInformationIcon
  - Button 10 – bExclamationIcon
  - Button „Color“ – bColor
  - Button „Font“ – bFont
  - Button „Picture” – bPicture
  - Label „Greetings!” – lbMessage
  - PictureBox – pbPicture
  Add new form to the project (Project > Add New Item... > Windows Form) and design it as shown in the picture:
  Name the text box – tbInputBox.
  Select the button, find its DialogResult property and set it to OK.
  In Form1.cs file replace the code with the following:
  using System; using System.Drawing; using System.Windows.Forms; namespace Dialogs { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void tbName1_TextChanged(object sender, EventArgs e) { lbName1.Text = "Hello, " + tbName1.Text; } private void tbName2_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)Keys.Enter) lbName2.Text = "Hello, " + tbName2.Text; else lbName2.Text="Hello!"; } private void InputBox() { // Create instance to your InputBox form. Form2 frm2 = new Form2(); // Show your InputBox form as modal dialog // and check if the result returned from it is OK. if (frm2.ShowDialog() == DialogResult.OK) { // Here get the value of the TextBox on you InputBox form // and assign it to the TextBox on your main form. tbFromInputBox.Text = frm2.tbInputBox.Text; } } private void bInputBox_Click(object sender, EventArgs e) { InputBox(); } private void bMessageBox_Click(object sender, EventArgs e) { DialogResult dialogResult; dialogResult = MessageBox.Show("Do you like RED?", "RED", MessageBoxButtons.YesNo); if (dialogResult == DialogResult.Yes) this.BackColor=Color.Red; } private void bAbortRetryIgnore_Click(object sender, EventArgs e) { MessageBox.Show("Would you like to ABORT, RETRY or IGNORE?", "ARI", MessageBoxButtons.AbortRetryIgnore); } private void bOkCancel_Click(object sender, EventArgs e) { MessageBox.Show("Delete Windows folder permanently?", "OC", MessageBoxButtons.OKCancel); } private void bYesNoCancel_Click(object sender, EventArgs e) { MessageBox.Show("Do you want to get an Excelent score on yout Program technologies exam", "YNC", MessageBoxButtons.YesNoCancel); } private void bRetryCancel_Click(object sender, EventArgs e) { MessageBox.Show("You fail hard on your Program technologies exam...", "RC", MessageBoxButtons.RetryCancel); } private void bQuestionIcon_Click(object sender, EventArgs e) { MessageBox.Show("Question", "Q", MessageBoxButtons.YesNo, MessageBoxIcon.Question); } private void bCriticalIcon_Click(object sender, EventArgs e) { MessageBox.Show("Error", "Er", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error); } private void bInformationIcon_Click(object sender, EventArgs e) { MessageBox.Show("Information", "I", MessageBoxButtons.OK, MessageBoxIcon.Information); } private void bExclamationIcon_Click(object sender, EventArgs e ) { MessageBox.Show("Exclamation", "Ex", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation); } private void bColor_Click(object sender, EventArgs e) { colorDialog1.ShowDialog(); this.BackColor = colorDialog1.Color; } private void bFont_Click(object sender, EventArgs e) { fontDialog1.ShowDialog(); lbMessage.Font = fontDialog1.Font; } private void bPicture_Click(object sender, EventArgs e) { openFileDialog1.Filter = "Bitmaps|*.bmp|GIFS|*.gif|All files|*.*"; if(openFileDialog1.ShowDialog() == DialogResult.OK) pbPicture.Image = Image.FromFile(openFileDialog1.FileName); } } }
  2) Create an application which demonstrates the use of modal and modeless forms.
  - Modal form: open new form with the days of the week. Add option to it of of selecting several days of the week and then transferring selected days from the open form to the main form.
  - Modeless form: example consists of 4 buttons each of them represent a season. Set the appropriate background image for each button. When button is clicked a new form must be opened displaying the image of the button enlarged and for title to use the button text.
  Solution
  Create new Windows Forms Application project and design the form as shown on the picture:
  Name the elements as follows:
  GroupBox 1 – Example 1
  GroupBox 2 – Example 2
  In Example 1 place one label and one button. Name them as follows label1 – lDays, Get Days – bDays.
  In Example 2 place four buttons – name them as follows: Clouds – bClouds; Snow – bSnow; Sun – bSun; Rain – bRain. Use the following images for background.
  Add new form to the project, name it DatsForm and design it as shown:
  Add 7 check boxes to it and name them as follows:
  Monday – chbMonday
  Tuesday - chbTuesday
  Wednesday - chbWednesday
  Thursday - chbThursday
  Friday - chbFriday
  Saturday - chbSaturday
  Sunday - chbSunday
  On click event of the button OK add the following code:
  private void button1_Click(object sender, EventArgs e) { this.Hide(); }
  In the main form, on the click event of the button Get Days add the following code:
  private void bDays_Click(object sender, EventArgs e) { CheckBox chb = new CheckBox(); frmDays frmDays = new frmDays(); string sTemp = ""; frmDays.ShowDialog(); foreach (Control ctrl in frmDays.Controls) { if (ctrl is CheckBox) { chb = ctrl as CheckBox; if (chb.CheckState == CheckState.Checked) sTemp = ctrl.Text + " " + sTemp + " "; } } lDays.Text = sTemp; }
  Add another form to the project with name WeatherForm and design it as shown:
  Name the elements as follows:
  Label: Name – lInfo, AutoSize – False, resize it to take up 2 rows.
  PictureBox – pbModal.
  In the source code of the main form add the following function:
  private new void Modal(object sender, EventArgs e) { Button btn = sender as Button; string[] sArrayInfo = new string[4]; sArrayInfo[0] = "Clouds: lots and lots of clouds."; sArrayInfo[1] = "It snowed for days on end."; sArrayInfo[2] = "A beautiful day to work on the computer."; sArrayInfo[3] = "Take your umbrella and wear your boots!"; int iIndex = Convert.ToInt32(btn.Tag); frmWeather frmWeather = new frmWeather(); frmWeather.BackColor = btn.BackColor; frmWeather.Text = btn.Text; frmWeather.lInfo.Text = sArrayInfo[iIndex]; frmWeather.pbModal.Image = btn.Image; frmWeather.Show(); }
  Foreach of the season buttons add the following to theirs click event.
  Modal(sender, e);
  Self-assignments
  1) Create quiz application. The user sees up to 10 questions in roll, each with 4 responses (radio buttons). A button is used to submit the answer. The number of correct answers is counted in text box. When successfully completed the quiz a picture is loaded in pictureBox.