WinForm Control Menu controls, toolbar controls, and status bar controls

Source: Internet
Author: User

Menus are the main way for users to get the main features and utilities in an application, such as creating a new file, opening a file, and so on, which requires a menu control (MenuStrip). Toolbar another common way to get the main functionality of your application is to use a toolbar control (ToolStrip), which is more intuitive than a menu. The status bar is used to display brief information about the state of the user, which requires a status bar control (StatusStrip).

One, MenuStrip controls and ContenxMenuStrip controls

The MenuStrip control is provided by the System.Windows.Forms.MenuStrip class, replacing the previous MainMenu control, which is the container for the application menu structure. When you create a menu, you add a Toolstripmenu object to the MenuStrip control, which can be done at setup time or in code.

ContenxMenuStrip The control is provided by the System.Windows.Forms.ToolMenuStrip class, which, like the MainMenu control, is also a container for the Toolstripmenu object that creates a right-click menu for the form. The primary event for it and the MainMenu control is to respond to the Click event.

Example: Demonstrating the use of MenuStrip controls and ContenxMenuStrip controls

(1) Add a MenuStrip control to the form and a Contenxmenustrip control, click the MenuStrip control on the form, write "file", and a "file" menu is created.

(2) Double-click the "file" to generate the Click event, add the MessageBox in the event Show method prompts the user to respond to the click event, the code is:

  <span style= "FONT-SIZE:18PX;" >      private void File Toolstripmenuitem_click (object sender, EventArgs e)        {            MessageBox.Show ("I responded to the Click event" );        } </span>

(3) Select the Contenxmenustrip control, create three Toolstripmenu objects at design time, their Text property set to copy, Cut, paste, set the Mainstrip property of the form to MENUSTRIP1, The Contenxmenustrip property is set to ContenxMenuStrip1.
The complete Form code is:

<span style= "FONT-SIZE:18PX;" >using system;using system.collections.generic;using system.componentmodel;using System.Data;using System.drawing;using system.linq;using system.text;using system.threading.tasks;using System.Windows.Forms; namespace windowsformsapplication2{public    partial class Form1:form    {public        Form1 ()        {            InitializeComponent ();        }        private void file Toolstripmenuitem_click (object sender, EventArgs e)        {              MessageBox.Show ("I responded to the Click event");          }    }} </span>

The result of running the form is:

Right-clicking a form will appear with three Toolstripmenu objects Contenxmenustrip control

Two, ToolStrip Control

ToolStrip controls are provided by the System.Windows.Forms.ToolStrip class to create easy-to-customize, common toolbars that support advanced user interface and layout features such as docking, floating, buttons with text and images, and drop-down buttons.

The properties of the ToolStrip control govern the display and display of the control, and are the basis of the MenuStrip control, the properties commonly used by ToolStrip controls:

Many controls can be used in ToolStrip:

Example: Demonstrating the use of ToolStrip controls

(1) Add a ToolStrip control on the form, click the drop-down arrow behind the icon, select button, add a ToolStripButton on the ToolStrip control, repeat the above action Select Labelhe and DropDownButton respectively to add ToolStripLabel and Toolstripdropdownbutton to the ToolStrip control.

(2) Set the Text property of ToolStripButton to "save", and set ToolStripLabel's Text property to "font" to add three items below Toolstripdropdownbutton: Add, change, and delete.

(3) Select ToolStripButton, select the Click event from the event panel and add the hint code:

       <span style= "FONT-SIZE:18PX;" > private void Toolstripbutton1_click (object sender, EventArgs e)        {            MessageBox.Show ("You clicked Save");        } </span>

The complete form code is:

<span style= "FONT-SIZE:18PX;" >using system;using system.collections.generic;using system.componentmodel;using System.Data;using System.drawing;using system.linq;using system.text;using system.threading.tasks;using System.Windows.Forms; namespace windowsformsapplication1{public    partial class Form1:form    {public        Form1 ()        {            InitializeComponent ();        }        private void Toolstripbutton1_click (object sender, EventArgs e)        {            MessageBox.Show ("You clicked Save");}        }    } </span>

The result of running the form is:

Three, Statustrip controls

The Statustrip control is provided by the System.Windows.Forms.StatuStrip class, which is used to represent a column at the bottom of the dialog box in the application, typically to display simple information about the current state of the application.

The three Toolstripdropdownbutton in the controls described in ToolStrip can be used in StatusStrip,ToolStripProgressBar and Toolstripsplitbutton. There is also a control that is StatusStrip dedicated, that is, Statusstripstatuslabel, that is, text and images that display information to the user about the current state of the application.

Example: Demo StatusStrip control, which prompts the total number of rows in a TextBox control

(1) Add a TextBox control to the form, set its Multiline property to true, and set its Size property to 268*231.

(2) Add a StatusStrip control to the form, add the same Statusstripstatuslabel control, set its Text property to NULL, double-click the TextBox control to generate the TextChanged event, Add the code in the event that displays the cursor in the TextBox control line:

       <span style= "FONT-SIZE:18PX;" >private void textBox1_TextChanged (object sender, EventArgs e)        {            statusstrip1.items[0]. Text = "line:" +textbox1.lines.length.tostring ();        } </span>

The complete form code is:

<span style= "FONT-SIZE:18PX;" >using system;using system.collections.generic;using system.componentmodel;using System.Data;using System.drawing;using system.linq;using system.text;using system.threading.tasks;using System.Windows.Forms; namespace windowsformsapplication2{public    partial class Form1:form    {public        Form1 ()        {            InitializeComponent ();        }        private void textBox1_TextChanged (object sender, EventArgs e)        {            statusstrip1.items[0]. Text = "line:" +textbox1.lines.length.tostring ();}}}    </span>

Enter something into the TextBox control and run the result:






WinForm Control Menu controls, toolbar controls, and status bar controls

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.