Add Method (StatusBarPanel)
Collapse the table of content
Expand the table of content

StatusBar.StatusBarPanelCollection.Add Method (StatusBarPanel)

 

Adds a StatusBarPanel to the collection.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public virtual int Add(
	StatusBarPanel value
)

Parameters

value
Type: System.Windows.Forms.StatusBarPanel

A StatusBarPanel that represents the panel to add to the collection.

Return Value

Type: System.Int32

The zero-based index of the item in the collection.

Exception Condition
ArgumentNullException

The StatusBarPanel being added to the collection was null.

ArgumentException

The parent of the StatusBarPanel specified in the value parameter is not null.

You can add panels to a StatusBar control to display more than one type of information. This version of the Add method adds the StatusBarPanel specified in the value parameter to the collection. The order in which panels are located in the StatusBar.StatusBarPanelCollection represents the order that panels are displayed within the StatusBar control. Panels are displayed from left to right starting with the first panel in the collection. The RightToLeft property of the StatusBar control does not change the order in which panels are displayed in the StatusBar. To insert a panel at a specific position in the collection, use the Insert method. To add a set of panels to the collection in a single operation, use the AddRange method.

The following code example creates a StatusBar control on a form and adds two StatusBarPanel objects. One of the StatusBarPanel objects, named panel1, displays status text for an application. The second StatusBarPanel, named panel2, displays the current date and uses the ToolTipText property of the StatusBarPanel class to display the current time. The example uses the ShowPanels property to ensure that the panels are displayed instead of a standard panel, and it uses and the Panels property to access the Add method of the StatusBar.StatusBarPanelCollection to add the panels to the StatusBar. The example also uses the AutoSize, BorderStyle, ToolTipText, and Text properties to initialize the StatusBarPanel objects. This example assumes that the method defined in the example is defined and called from the constructor of a Form.

private void CreateMyStatusBar()
{
	// Create a StatusBar control.
	StatusBar statusBar1 = new StatusBar();
	// Create two StatusBarPanel objects to display in the StatusBar.
	StatusBarPanel panel1 = new StatusBarPanel();
	StatusBarPanel panel2 = new StatusBarPanel();

	// Display the first panel with a sunken border style.
	panel1.BorderStyle = StatusBarPanelBorderStyle.Sunken;
	// Initialize the text of the panel.
	panel1.Text = "Ready...";
	// Set the AutoSize property to use all remaining space on the StatusBar.
	panel1.AutoSize = StatusBarPanelAutoSize.Spring;

	// Display the second panel with a raised border style.
	panel2.BorderStyle = StatusBarPanelBorderStyle.Raised;

	// Create ToolTip text that displays time the application was 
    			//started.
	panel2.ToolTipText = "Started: " + System.DateTime.Now.ToShortTimeString();
	// Set the text of the panel to the current date.
	panel2.Text = System.DateTime.Today.ToLongDateString();
	// Set the AutoSize property to size the panel to the size of the contents.
	panel2.AutoSize = StatusBarPanelAutoSize.Contents;

	// Display panels in the StatusBar control.
	statusBar1.ShowPanels = true;

	// Add both panels to the StatusBarPanelCollection of the StatusBar.			
	statusBar1.Panels.Add(panel1);
	statusBar1.Panels.Add(panel2);

	// Add the StatusBar to the form.
	this.Controls.Add(statusBar1);
}

.NET Framework
Available since 1.1
Return to top
Show:
© 2016 Microsoft