mirror of
https://github.com/Stone-Red-Code/docs-desktop.git
synced 2026-09-06 07:56:06 +02:00
Initial winforms content migrated (#18)
* Merge winforms framework content to working branch (#14) * Breadcrumb / TOC / Move net5 to net folder (#15) * change path from net5 to net * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Add .net 5 winforms placeholder article (#16) * added some metadata and adjusted net5 placeholder * corrections * corrections * corrections * Test1 * Swapping landing page vs concept * fix links * Fix links * Fix desc
This commit is contained in:
+78
@@ -0,0 +1,78 @@
|
||||
---
|
||||
title: Access Objects in DataGridViewComboBoxCell Drop-Down List
|
||||
ms.date: "03/30/2017"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
helpviewer_keywords:
|
||||
- "DataGridView control [Windows Forms], accessing objects in combo box cells"
|
||||
- "combo boxes [Windows Forms], in DataGridView control"
|
||||
- "combo boxes [Windows Forms], accessing objects in DataGridViewComboBoxCell drop-down lists"
|
||||
ms.assetid: bcbe794a-d1fa-47f8-b5a3-5f085b32097d
|
||||
---
|
||||
# How to: Access Objects in a Windows Forms DataGridViewComboBoxCell Drop-Down List
|
||||
Like the <xref:System.Windows.Forms.ComboBox> control, the <xref:System.Windows.Forms.DataGridViewComboBoxColumn> and <xref:System.Windows.Forms.DataGridViewComboBoxCell> types enable you to add arbitrary objects to their drop-down lists. With this feature, you can represent complex states in a drop-down list without having to store corresponding objects in a separate collection.
|
||||
|
||||
Unlike the <xref:System.Windows.Forms.ComboBox> control, the <xref:System.Windows.Forms.DataGridView> types do not have a <xref:System.Windows.Forms.ComboBox.SelectedItem%2A> property for retrieving the currently selected object. Instead, you must set the <xref:System.Windows.Forms.DataGridViewComboBoxColumn.ValueMember%2A?displayProperty=nameWithType> or <xref:System.Windows.Forms.DataGridViewComboBoxCell.ValueMember%2A?displayProperty=nameWithType> property to the name of a property on your business object. When the user makes a selection, the indicated property of the business object sets the cell <xref:System.Windows.Forms.DataGridViewCell.Value%2A> property.
|
||||
|
||||
To retrieve the business object through the cell value, the `ValueMember` property must indicate a property that returns a reference to the business object itself. Therefore, if the type of the business object is not under your control, you must add such a property by extending the type through inheritance.
|
||||
|
||||
The following procedures demonstrate how to populate a drop-down list with business objects and retrieve the objects through the cell <xref:System.Windows.Forms.DataGridViewCell.Value%2A> property.
|
||||
|
||||
### To add business objects to the drop-down list
|
||||
|
||||
1. Create a new <xref:System.Windows.Forms.DataGridViewComboBoxColumn> and populate its <xref:System.Windows.Forms.DataGridViewComboBoxColumn.Items%2A> collection. Alternatively, you can set the column <xref:System.Windows.Forms.DataGridViewComboBoxColumn.DataSource%2A> property to the collection of business objects. In that case, however, you cannot add "unassigned" to the drop-down list without creating a corresponding business object in your collection.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.DataGridViewComboBoxObjectBinding#110](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewComboBoxObjectBinding/CS/form1.cs#110)]
|
||||
[!code-vb[System.Windows.Forms.DataGridViewComboBoxObjectBinding#110](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewComboBoxObjectBinding/vb/form1.vb#110)]
|
||||
|
||||
2. Set the <xref:System.Windows.Forms.DataGridViewComboBoxColumn.DisplayMember%2A> and <xref:System.Windows.Forms.DataGridViewComboBoxColumn.ValueMember%2A> properties. <xref:System.Windows.Forms.DataGridViewComboBoxColumn.DisplayMember%2A> indicates the property of the business object to display in the drop-down list. <xref:System.Windows.Forms.DataGridViewComboBoxColumn.ValueMember%2A> indicates the property that returns a reference to the business object.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.DataGridViewComboBoxObjectBinding#115](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewComboBoxObjectBinding/CS/form1.cs#115)]
|
||||
[!code-vb[System.Windows.Forms.DataGridViewComboBoxObjectBinding#115](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewComboBoxObjectBinding/vb/form1.vb#115)]
|
||||
|
||||
3. Make sure that your business object type contains a property that returns a reference to the current instance. This property must be named with the value assigned to <xref:System.Windows.Forms.DataGridViewComboBoxColumn.ValueMember%2A> in the previous step.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.DataGridViewComboBoxObjectBinding#310](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewComboBoxObjectBinding/CS/form1.cs#310)]
|
||||
[!code-vb[System.Windows.Forms.DataGridViewComboBoxObjectBinding#310](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewComboBoxObjectBinding/vb/form1.vb#310)]
|
||||
|
||||
### To retrieve the currently selected business object
|
||||
|
||||
- Get the cell <xref:System.Windows.Forms.DataGridViewCell.Value%2A> property and cast it to the business object type.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.DataGridViewComboBoxObjectBinding#120](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewComboBoxObjectBinding/CS/form1.cs#120)]
|
||||
[!code-vb[System.Windows.Forms.DataGridViewComboBoxObjectBinding#120](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewComboBoxObjectBinding/vb/form1.vb#120)]
|
||||
|
||||
## Example
|
||||
The complete example demonstrates the use of business objects in a drop-down list. In the example, a <xref:System.Windows.Forms.DataGridView> control is bound to a collection of `Task` objects. Each `Task` object has an `AssignedTo` property that indicates the `Employee` object currently assigned to that task. The `Assigned To` column displays the `Name` property value for each assigned employee, or "unassigned" if the `Task.AssignedTo` property value is `null`.
|
||||
|
||||
To view the behavior of this example, perform the following steps:
|
||||
|
||||
1. Change assignments in the `Assigned To` column by selecting different values from the drop-down lists or pressing CTRL+0 in a combo-box cell.
|
||||
|
||||
2. Click `Generate Report` to display the current assignments. This demonstrates that a change in the `Assigned To` column automatically updates the `tasks` collection.
|
||||
|
||||
3. Click a `Request Status` button to call the `RequestStatus` method of the current `Employee` object for that row. This demonstrates that the selected object has been successfully retrieved.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.DataGridViewComboBoxObjectBinding#000](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewComboBoxObjectBinding/CS/form1.cs#000)]
|
||||
[!code-vb[System.Windows.Forms.DataGridViewComboBoxObjectBinding#000](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewComboBoxObjectBinding/vb/form1.vb#000)]
|
||||
|
||||
## Compiling the Code
|
||||
This example requires:
|
||||
|
||||
- References to the System and System.Windows.Forms assemblies.
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridView>
|
||||
- <xref:System.Windows.Forms.DataGridViewComboBoxColumn>
|
||||
- <xref:System.Windows.Forms.DataGridViewComboBoxColumn.Items%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridViewComboBoxColumn.DataSource%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridViewComboBoxColumn.ValueMember%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridViewComboBoxCell>
|
||||
- <xref:System.Windows.Forms.DataGridViewComboBoxCell.Items%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridViewComboBoxCell.DataSource%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridViewComboBoxCell.ValueMember%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridViewCell.Value%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.ComboBox>
|
||||
- [Displaying Data in the Windows Forms DataGridView Control](displaying-data-in-the-windows-forms-datagridview-control.md)
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
---
|
||||
title: Access Specific Items in ComboBox, ListBox, or CheckedListBox Control
|
||||
ms.date: "03/30/2017"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
- "cpp"
|
||||
helpviewer_keywords:
|
||||
- "ComboBox control [Windows Forms], accessing items"
|
||||
- "ListBox control [Windows Forms], returning item information"
|
||||
- "list boxes [Windows Forms], accessing items"
|
||||
- "ListBox control [Windows Forms], accessing items"
|
||||
- "combo boxes [Windows Forms], accessing items"
|
||||
- "CheckedListBox control [Windows Forms], accessing items"
|
||||
ms.assetid: 1216742f-bcf9-4ff8-8a62-d7c9053c2b96
|
||||
---
|
||||
# How to: Access Specific Items in a Windows Forms ComboBox, ListBox, or CheckedListBox Control
|
||||
Accessing specific items in a Windows Forms combo box, list box, or checked list box is an essential task. It enables you to programmatically determine what is in a list, at any given position.
|
||||
|
||||
### To access a specific item
|
||||
|
||||
1. Query the `Items` collection using the index of the specific item:
|
||||
|
||||
```vb
|
||||
Private Function GetItemText(i As Integer) As String
|
||||
' Return the text of the item using the index:
|
||||
Return ComboBox1.Items(i).ToString
|
||||
End Function
|
||||
```
|
||||
|
||||
```csharp
|
||||
private string GetItemText(int i)
|
||||
{
|
||||
// Return the text of the item using the index:
|
||||
return (comboBox1.Items[i].ToString());
|
||||
}
|
||||
```
|
||||
|
||||
```cpp
|
||||
private:
|
||||
String^ GetItemText(int i)
|
||||
{
|
||||
// Return the text of the item using the index:
|
||||
return (comboBox1->Items->Item[i]->ToString());
|
||||
}
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.ComboBox>
|
||||
- <xref:System.Windows.Forms.ListBox>
|
||||
- <xref:System.Windows.Forms.CheckedListBox>
|
||||
- [Windows Forms Controls Used to List Options](windows-forms-controls-used-to-list-options.md)
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
---
|
||||
title: "Accessing Frames in the Managed HTML Document Object Model"
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "HTML [Windows Forms], dOM"
|
||||
- "managed HTML DOM"
|
||||
- "HTML [Windows Forms], managed"
|
||||
- "HTML DOM [Windows Forms], managed"
|
||||
- "frames [Windows Forms], accessing"
|
||||
- "DOM [Windows Forms], accessing frames in managed HTML"
|
||||
ms.assetid: cdeeaa22-0be4-4bbf-9a75-4ddc79199f8d
|
||||
---
|
||||
# Accessing Frames in the Managed HTML Document Object Model
|
||||
Some HTML documents are composed out of *frames*, or windows that can hold their own distinct HTML documents. Using frames makes it easy to create HTML pages in which one or more pieces of the page remain static, such as a navigation bar, while other frames constantly change their content.
|
||||
|
||||
HTML authors can create frames in one of two ways:
|
||||
|
||||
- Using the `FRAMESET` and `FRAME` tags, which create fixed windows.
|
||||
|
||||
-or-
|
||||
|
||||
- Using the `IFRAME` tag, which creates a floating window that can be repositioned at run time.
|
||||
|
||||
1. Because frames contain HTML documents, they are represented in the Document Object Model (DOM) as both window elements and frame elements.
|
||||
|
||||
2. When you access a `FRAME` or `IFRAME` tag by using the Frames collection of <xref:System.Windows.Forms.HtmlWindow>, you are retrieving the window element corresponding to the frame. This represents all of the frame's dynamic properties, such as its current URL, document, and size.
|
||||
|
||||
3. When you access a `FRAME` or `IFRAME` tag by using the <xref:System.Windows.Forms.HtmlWindow.WindowFrameElement%2A> property of <xref:System.Windows.Forms.HtmlWindow>, the <xref:System.Windows.Forms.HtmlElement.Children%2A> collection, or methods such as <xref:System.Windows.Forms.HtmlElementCollection.GetElementsByName%2A> or <xref:System.Windows.Forms.HtmlDocument.GetElementById%2A>, you are retrieving the frame element. This represents the static properties of the frame, including the URL specified in the original HTML file.
|
||||
|
||||
## Frames and Security
|
||||
Access to frames is complicated by the fact that the managed HTML DOM implements a security measure known as *cross-frame scripting security*. If a document contains a `FRAMESET` with two or more `FRAME`s in different domains, these `FRAME`s cannot interact with one another. In other words, a `FRAME` that displays content from your Web site cannot access information in a `FRAME` that hosts a third-party site such as `http://www.adatum.com/`. This security is implemented at the level of the <xref:System.Windows.Forms.HtmlWindow> class. You can obtain general information about a `FRAME` hosting another Web site, such as its URL, but you will be unable to access its <xref:System.Windows.Forms.HtmlWindow.Document%2A> or change the size or location of its hosting `FRAME` or `IFRAME`.
|
||||
|
||||
This rule also applies to windows that you open using the <xref:System.Windows.Forms.HtmlWindow.Open%2A> and <xref:System.Windows.Forms.HtmlWindow.OpenNew%2A> methods. If the window you open is in a different domain from the page hosted in the <xref:System.Windows.Forms.WebBrowser> control, you will not be able to move that window or examine its contents. These restrictions are also enforced if you use the <xref:System.Windows.Forms.WebBrowser> control to display a Web site that is different from the Web site used to deploy your Windows Forms-based application. If you use ClickOnce deployment technology to install your application from Web site A, and you use the <xref:System.Windows.Forms.WebBrowser> to display Web site B, you will not be able to access Web site B's data.
|
||||
|
||||
## See also
|
||||
|
||||
- [\<frame> element](https://developer.mozilla.org/docs/Web/HTML/Element/frame)
|
||||
- [Using the Managed HTML Document Object Model](using-the-managed-html-document-object-model.md)
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
---
|
||||
title: "Accessing Unexposed Members on the Managed HTML Document Object Model"
|
||||
ms.date: "03/30/2017"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
helpviewer_keywords:
|
||||
- "unexposed members"
|
||||
- "managed HTML DOM [Windows Forms], accessing unexposed members"
|
||||
ms.assetid: 762295bd-2355-4aa7-b43c-5bff997a33e6
|
||||
---
|
||||
# Accessing Unexposed Members on the Managed HTML Document Object Model
|
||||
The managed HTML Document Object Model (DOM) contains a class called <xref:System.Windows.Forms.HtmlElement> that exposes the properties, methods, and events that all HTML elements have in common. Sometimes, however, you will need to access members that the managed interface does not directly expose. This topic examines two ways for accessing unexposed members, including JScript and VBScript functions defined inside of a Web page.
|
||||
|
||||
## Accessing Unexposed Members through Managed Interfaces
|
||||
<xref:System.Windows.Forms.HtmlDocument> and <xref:System.Windows.Forms.HtmlElement> provide four methods that enable access to unexposed members. The following table shows the types and their corresponding methods.
|
||||
|
||||
|Member Type|Method(s)|
|
||||
|-----------------|-----------------|
|
||||
|Properties (<xref:System.Windows.Forms.HtmlElement>)|<xref:System.Windows.Forms.HtmlElement.GetAttribute%2A><br /><br /> <xref:System.Windows.Forms.HtmlElement.SetAttribute%2A>|
|
||||
|Methods|<xref:System.Windows.Forms.HtmlElement.InvokeMember%2A>|
|
||||
|Events (<xref:System.Windows.Forms.HtmlDocument>)|<xref:System.Windows.Forms.HtmlDocument.AttachEventHandler%2A><br /><br /> <xref:System.Windows.Forms.HtmlDocument.DetachEventHandler%2A>|
|
||||
|Events (<xref:System.Windows.Forms.HtmlElement>)|<xref:System.Windows.Forms.HtmlElement.AttachEventHandler%2A><br /><br /> <xref:System.Windows.Forms.HtmlElement.DetachEventHandler%2A>|
|
||||
|Events (<xref:System.Windows.Forms.HtmlWindow>)|<xref:System.Windows.Forms.HtmlWindow.AttachEventHandler%2A><br /><br /> <xref:System.Windows.Forms.HtmlWindow.DetachEventHandler%2A>|
|
||||
|
||||
When you use these methods, it is assumed that you have an element of the correct underlying type. Suppose that you want to listen to the `Submit` event of a `FORM` element on an HTML page, so that you can perform some pre-processing on the `FORM`'s values before the user submits them to the server. Ideally, if you have control over the HTML, you would define the `FORM` to have a unique `ID` attribute.
|
||||
|
||||
```html
|
||||
<HTML>
|
||||
|
||||
<HEAD>
|
||||
<TITLE>Form Page</TITLE>
|
||||
</HEAD>
|
||||
|
||||
<BODY>
|
||||
<FORM ID="form1">
|
||||
... form fields defined here ...
|
||||
</FORM>
|
||||
</BODY>
|
||||
|
||||
</HTML>
|
||||
```
|
||||
|
||||
After you load this page into the <xref:System.Windows.Forms.WebBrowser> control, you can use the <xref:System.Windows.Forms.HtmlDocument.GetElementById%2A> method to retrieve the `FORM` at run time using `form1` as the argument.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.HtmlElement#10](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.HtmlElement/CS/Form1.cs#10)]
|
||||
[!code-vb[System.Windows.Forms.HtmlElement#10](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.HtmlElement/VB/Form1.vb#10)]
|
||||
|
||||
## Accessing Unmanaged Interfaces
|
||||
You can also access unexposed members on the managed HTML DOM by using the unmanaged Component Object Model (COM) interfaces exposed by each DOM class. This is recommended if you have to make several calls against unexposed members, or if the unexposed members return other unmanaged interfaces not wrapped by the managed HTML DOM.
|
||||
|
||||
The following table shows all of the unmanaged interfaces exposed through the managed HTML DOM. Click on each link for an explanation of its usage and for example code.
|
||||
|
||||
|Type|Unmanaged Interface|
|
||||
|----------|-------------------------|
|
||||
|<xref:System.Windows.Forms.HtmlDocument>|<xref:System.Windows.Forms.HtmlDocument.DomDocument%2A>|
|
||||
|<xref:System.Windows.Forms.HtmlElement>|<xref:System.Windows.Forms.HtmlElement.DomElement%2A>|
|
||||
|<xref:System.Windows.Forms.HtmlWindow>|<xref:System.Windows.Forms.HtmlWindow.DomWindow%2A>|
|
||||
|<xref:System.Windows.Forms.HtmlHistory>|<xref:System.Windows.Forms.HtmlHistory.DomHistory%2A>|
|
||||
|
||||
The easiest way to use the COM interfaces is to add a reference to the unmanaged HTML DOM library (MSHTML.dll) from your application, although this is unsupported. For more information, see [Knowledge Base Article 934368](https://support.microsoft.com/kb/934368).
|
||||
|
||||
## Accessing Script Functions
|
||||
An HTML page can define one or more functions by using a scripting language such as JScript or VBScript. These functions are placed inside of a `SCRIPT` page in the page, and can be run on demand or in response to an event on the DOM.
|
||||
|
||||
You can call any script functions you define in an HTML page using the <xref:System.Windows.Forms.HtmlDocument.InvokeScript%2A> method. If the script method returns an HTML element, you can use a cast to convert this return result to an <xref:System.Windows.Forms.HtmlElement>. For details and example code, see <xref:System.Windows.Forms.HtmlDocument.InvokeScript%2A>.
|
||||
|
||||
## See also
|
||||
|
||||
- [Using the Managed HTML Document Object Model](using-the-managed-html-document-object-model.md)
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
---
|
||||
title: Add and Remove Columns in DataGridView Control Using the Designer
|
||||
ms.date: "03/30/2017"
|
||||
f1_keywords:
|
||||
- "vs.DataGridViewAddColumnDialog"
|
||||
helpviewer_keywords:
|
||||
- "DataGridView control [Windows Forms], adding columns"
|
||||
- "DataGridView control [Windows Forms], removing columns"
|
||||
ms.assetid: 9e709f35-0a8c-4e7e-b4c4-bacb7a834077
|
||||
---
|
||||
# How to: Add and Remove Columns in the Windows Forms DataGridView Control Using the Designer
|
||||
The Windows Forms <xref:System.Windows.Forms.DataGridView> control must contain columns in order to display data. If you plan to populate the control manually, you must add the columns yourself. Alternately, you can bind the control to a data source, which generates and populates the columns automatically. If the data source contains more columns than you want to display, you can remove the unwanted columns.
|
||||
|
||||
The following procedures require a **Windows Application** project with a form containing a <xref:System.Windows.Forms.DataGridView> control. For information about setting up such a project, see [How to: Create a Windows Forms application project](/visualstudio/ide/step-1-create-a-windows-forms-application-project) and [How to: Add Controls to Windows Forms](how-to-add-controls-to-windows-forms.md).
|
||||
|
||||
## To add a column using the designer
|
||||
|
||||
1. Click the designer actions glyph () on the upper-right corner of the <xref:System.Windows.Forms.DataGridView> control, and then select **Add Column**.
|
||||
|
||||
2. In the **Add Column** dialog box, choose the **Databound Column** option and select a column from the data source, or choose the **Unbound Column** option and define the column using the fields provided.
|
||||
|
||||
3. Click the **Add** button to add the column, causing it to appear in the designer if the existing columns do not already fill the control display area.
|
||||
|
||||
> [!NOTE]
|
||||
> You can modify column properties in the **Edit Columns** dialog box, which you can access from the control's smart tag.
|
||||
|
||||
## To remove a column using the designer
|
||||
|
||||
1. Choose **Edit Columns** from the control's smart tag.
|
||||
|
||||
2. Select a column from the **Selected Columns** list.
|
||||
|
||||
3. Click the **Remove** button to delete the column, causing it to disappear from the designer.
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridView>
|
||||
- [How to: Create a Windows Forms application project](/visualstudio/ide/step-1-create-a-windows-forms-application-project)
|
||||
- [How to: Add Controls to Windows Forms](how-to-add-controls-to-windows-forms.md)
|
||||
+141
@@ -0,0 +1,141 @@
|
||||
---
|
||||
title: Add and Remove Items from ComboBox, ListBox, or CheckedListBox Control
|
||||
ms.date: "03/30/2017"
|
||||
description: Learn how to add and remove a Windows Forms ComboBox, ListBox, and CheckedListBox controls simply and with no data binding.
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
- "cpp"
|
||||
helpviewer_keywords:
|
||||
- "combo boxes [Windows Forms], adding items"
|
||||
- "list boxes [Windows Forms], removing items"
|
||||
- "ComboBox control [Windows Forms], adding and removing items"
|
||||
- "ListBox control [Windows Forms], adding and removing items"
|
||||
- "list boxes [Windows Forms], adding items"
|
||||
- "combo boxes [Windows Forms], removing items"
|
||||
- "CheckedListBox control [Windows Forms], adding and removing items"
|
||||
ms.assetid: 7224c8d2-4118-443e-ae1e-d7c17d1e69ee
|
||||
---
|
||||
# How to: Add and Remove Items from a Windows Forms ComboBox, ListBox, or CheckedListBox Control
|
||||
Items can be added to a Windows Forms combo box, list box, or checked list box in a variety of ways, because these controls can be bound to a variety of data sources. However, this topic demonstrates the simplest method and requires no data binding. The items displayed are usually strings; however, any object can be used. The text that is displayed in the control is the value returned by the object's `ToString` method.
|
||||
|
||||
### To add items
|
||||
|
||||
1. Add the string or object to the list by using the `Add` method of the `ObjectCollection` class. The collection is referenced using the `Items` property:
|
||||
|
||||
```vb
|
||||
ComboBox1.Items.Add("Tokyo")
|
||||
```
|
||||
|
||||
```csharp
|
||||
comboBox1.Items.Add("Tokyo");
|
||||
```
|
||||
|
||||
```cpp
|
||||
comboBox1->Items->Add("Tokyo");
|
||||
```
|
||||
|
||||
- or -
|
||||
|
||||
2. Insert the string or object at the desired point in the list with the `Insert` method:
|
||||
|
||||
```vb
|
||||
CheckedListBox1.Items.Insert(0, "Copenhagen")
|
||||
```
|
||||
|
||||
```csharp
|
||||
checkedListBox1.Items.Insert(0, "Copenhagen");
|
||||
```
|
||||
|
||||
```cpp
|
||||
checkedListBox1->Items->Insert(0, "Copenhagen");
|
||||
```
|
||||
|
||||
- or -
|
||||
|
||||
3. Assign an entire array to the `Items` collection:
|
||||
|
||||
```vb
|
||||
Dim ItemObject(9) As System.Object
|
||||
Dim i As Integer
|
||||
For i = 0 To 9
|
||||
ItemObject(i) = "Item" & i
|
||||
Next i
|
||||
ListBox1.Items.AddRange(ItemObject)
|
||||
```
|
||||
|
||||
```csharp
|
||||
System.Object[] ItemObject = new System.Object[10];
|
||||
for (int i = 0; i <= 9; i++)
|
||||
{
|
||||
ItemObject[i] = "Item" + i;
|
||||
}
|
||||
listBox1.Items.AddRange(ItemObject);
|
||||
```
|
||||
|
||||
```cpp
|
||||
Array<System::Object^>^ ItemObject = gcnew Array<System::Object^>(10);
|
||||
for (int i = 0; i <= 9; i++)
|
||||
{
|
||||
ItemObject[i] = String::Concat("Item", i.ToString());
|
||||
}
|
||||
listBox1->Items->AddRange(ItemObject);
|
||||
```
|
||||
|
||||
### To remove an item
|
||||
|
||||
1. Call the `Remove` or `RemoveAt` method to delete items.
|
||||
|
||||
`Remove` has one argument that specifies the item to remove.`RemoveAt` removes the item with the specified index number.
|
||||
|
||||
```vb
|
||||
' To remove item with index 0:
|
||||
ComboBox1.Items.RemoveAt(0)
|
||||
' To remove currently selected item:
|
||||
ComboBox1.Items.Remove(ComboBox1.SelectedItem)
|
||||
' To remove "Tokyo" item:
|
||||
ComboBox1.Items.Remove("Tokyo")
|
||||
```
|
||||
|
||||
```csharp
|
||||
// To remove item with index 0:
|
||||
comboBox1.Items.RemoveAt(0);
|
||||
// To remove currently selected item:
|
||||
comboBox1.Items.Remove(comboBox1.SelectedItem);
|
||||
// To remove "Tokyo" item:
|
||||
comboBox1.Items.Remove("Tokyo");
|
||||
```
|
||||
|
||||
```cpp
|
||||
// To remove item with index 0:
|
||||
comboBox1->Items->RemoveAt(0);
|
||||
// To remove currently selected item:
|
||||
comboBox1->Items->Remove(comboBox1->SelectedItem);
|
||||
// To remove "Tokyo" item:
|
||||
comboBox1->Items->Remove("Tokyo");
|
||||
```
|
||||
|
||||
### To remove all items
|
||||
|
||||
1. Call the `Clear` method to remove all items from the collection:
|
||||
|
||||
```vb
|
||||
ListBox1.Items.Clear()
|
||||
```
|
||||
|
||||
```csharp
|
||||
listBox1.Items.Clear();
|
||||
```
|
||||
|
||||
```cpp
|
||||
listBox1->Items->Clear();
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.ComboBox>
|
||||
- <xref:System.Windows.Forms.ListBox>
|
||||
- <xref:System.Windows.Forms.CheckedListBox>
|
||||
- [How to: Sort the Contents of a Windows Forms ComboBox, ListBox, or CheckedListBox Control](sort-the-contents-of-a-wf-combobox-listbox-or-checkedlistbox-control.md)
|
||||
- [When to Use a Windows Forms ComboBox Instead of a ListBox](when-to-use-a-windows-forms-combobox-instead-of-a-listbox.md)
|
||||
- [Windows Forms Controls Used to List Options](windows-forms-controls-used-to-list-options.md)
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
---
|
||||
title: Add and Remove Items with ListView Control Using the Designer
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "ListView control [Windows Forms], populating"
|
||||
- "ListView control [Windows Forms], adding list items"
|
||||
ms.assetid: 217611ee-fd11-4d39-9a54-a37c3e781be1
|
||||
---
|
||||
# How to: Add and Remove Items with the Windows Forms ListView Control Using the Designer
|
||||
|
||||
The process of adding an item to a Windows Forms <xref:System.Windows.Forms.ListView> control consists primarily of specifying the item and assigning properties to it. Adding or removing list items can be done at any time.
|
||||
|
||||
The following procedure requires a **Windows Application** project with a form containing a <xref:System.Windows.Forms.ListView> control. For information about setting up such a project, see [How to: Create a Windows Forms application project](/visualstudio/ide/step-1-create-a-windows-forms-application-project) and [How to: Add Controls to Windows Forms](how-to-add-controls-to-windows-forms.md).
|
||||
|
||||
### To add or remove items using the designer
|
||||
|
||||
1. Select the <xref:System.Windows.Forms.ListView> control.
|
||||
|
||||
2. In the **Properties** window, click the **Ellipsis** () button next to the <xref:System.Windows.Forms.ListView.Items%2A> property.
|
||||
|
||||
The **ListViewItem Collection Editor** appears.
|
||||
|
||||
3. To add an item, click the **Add** button. You can then set properties of the new item, such as the <xref:System.Windows.Forms.ListView.Text%2A> and <xref:System.Windows.Forms.ListViewItem.ImageIndex%2A> properties.
|
||||
|
||||
4. To remove an item, select it and click the **Remove** button.
|
||||
|
||||
## See also
|
||||
|
||||
- [ListView Control Overview](listview-control-overview-windows-forms.md)
|
||||
- [How to: Add Columns to the Windows Forms ListView Control](how-to-add-columns-to-the-windows-forms-listview-control.md)
|
||||
- [How to: Display Subitems in Columns with the Windows Forms ListView Control](how-to-display-subitems-in-columns-with-the-windows-forms-listview-control.md)
|
||||
- [How to: Display Icons for the Windows Forms ListView Control](how-to-display-icons-for-the-windows-forms-listview-control.md)
|
||||
- [How to: Add Custom Information to a TreeView or ListView Control (Windows Forms)](add-custom-information-to-a-treeview-or-listview-control-wf.md)
|
||||
- [How to: Group Items in a Windows Forms ListView Control](how-to-group-items-in-a-windows-forms-listview-control.md)
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
---
|
||||
title: Add and Remove Menu Items with ContextMenu Component
|
||||
ms.date: "03/30/2017"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
- "cpp"
|
||||
helpviewer_keywords:
|
||||
- "context menus [Windows Forms], removing items"
|
||||
- "ContextMenu component [Windows Forms], adding items"
|
||||
- "shortcut menus [Windows Forms], removing items"
|
||||
- "shortcut menus [Windows Forms], examples"
|
||||
- "context menus [Windows Forms], adding items"
|
||||
- "shortcut menus [Windows Forms], adding items"
|
||||
- "ContextMenu component [Windows Forms], removing items"
|
||||
- "context menus [Windows Forms], examples"
|
||||
- "examples [Windows Forms], context menus"
|
||||
ms.assetid: 426d1eaf-7fb8-4b0b-8a33-5e8721786ea4
|
||||
---
|
||||
# How to: Add and Remove Menu Items with the Windows Forms ContextMenu Component
|
||||
Explains how to add and remove shortcut menu items in Windows Forms.
|
||||
|
||||
The Windows Forms <xref:System.Windows.Forms.ContextMenu> component provides a menu of frequently used commands that are relevant to the selected object. You can add items to the shortcut menu by adding <xref:System.Windows.Forms.MenuItem> objects to the <xref:System.Windows.Forms.Menu.MenuItems%2A> collection.
|
||||
|
||||
You can remove items from a shortcut menu permanently; however, at run time it may be more appropriate to hide or disable the items instead.
|
||||
|
||||
> [!IMPORTANT]
|
||||
> Although <xref:System.Windows.Forms.MenuStrip> and <xref:System.Windows.Forms.ContextMenuStrip> replace and add functionality to the <xref:System.Windows.Forms.MainMenu> and <xref:System.Windows.Forms.ContextMenu> controls of previous versions, <xref:System.Windows.Forms.MainMenu> and <xref:System.Windows.Forms.ContextMenu> are retained for both backward compatibility and future use if you choose.
|
||||
|
||||
### To remove items from a shortcut menu
|
||||
|
||||
1. Use the <xref:System.Windows.Forms.Menu.MenuItemCollection.Remove%2A> or <xref:System.Windows.Forms.Menu.MenuItemCollection.RemoveAt%2A> method of the <xref:System.Windows.Forms.Menu.MenuItems%2A> collection of the <xref:System.Windows.Forms.ContextMenu> component to remove a particular menu item.
|
||||
|
||||
```vb
|
||||
' Removes the first item in the shortcut menu.
|
||||
ContextMenu1.MenuItems.RemoveAt(0)
|
||||
' Removes a particular object from the shortcut menu.
|
||||
ContextMenu1.MenuItems.Remove(mnuItemNew)
|
||||
```
|
||||
|
||||
```csharp
|
||||
// Removes the first item in the shortcut menu.
|
||||
contextMenu1.MenuItems.RemoveAt(0);
|
||||
// Removes a particular object from the shortcut menu.
|
||||
contextMenu1.MenuItems.Remove(mnuItemNew);
|
||||
```
|
||||
|
||||
```cpp
|
||||
// Removes the first item in the shortcut menu.
|
||||
contextMenu1->MenuItems->RemoveAt(0);
|
||||
// Removes a particular object from the shortcut menu.
|
||||
contextMenu1->MenuItems->Remove(mnuItemNew);
|
||||
```
|
||||
|
||||
-or-
|
||||
|
||||
2. Use the `Clear` method of the `MenuItems` collection of the <xref:System.Windows.Forms.ContextMenu> component to remove all items from the menu.
|
||||
|
||||
```vb
|
||||
ContextMenu1.MenuItems.Clear()
|
||||
```
|
||||
|
||||
```csharp
|
||||
contextMenu1.MenuItems.Clear();
|
||||
```
|
||||
|
||||
```cpp
|
||||
contextMenu1->MenuItems->Clear();
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.ContextMenu>
|
||||
- [ContextMenu Component](contextmenu-component-windows-forms.md)
|
||||
- [ContextMenu Component Overview](contextmenu-component-overview-windows-forms.md)
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
---
|
||||
title: Add and Remove Nodes with TreeView Control Using the Designer
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "examples [Windows Forms], TreeView control"
|
||||
- "TreeView control [Windows Forms], removing nodes"
|
||||
- "tree nodes in TreeView control"
|
||||
- "TreeView control [Windows Forms], adding nodes"
|
||||
ms.assetid: 35bf1750-045e-4ec5-97cb-b47b0dbdaa2c
|
||||
---
|
||||
# How to: Add and Remove Nodes with the Windows Forms TreeView Control Using the Designer
|
||||
|
||||
Because the Windows Forms <xref:System.Windows.Forms.TreeView> control displays nodes in a hierarchical manner, when adding a node you must pay attention to what its parent node is.
|
||||
|
||||
The following procedure requires a **Windows Application** project with a form containing a <xref:System.Windows.Forms.TreeView> control. For information about setting up such a project, see [How to: Create a Windows Forms application project](/visualstudio/ide/step-1-create-a-windows-forms-application-project) and [How to: Add Controls to Windows Forms](how-to-add-controls-to-windows-forms.md).
|
||||
|
||||
### To add or remove nodes in the designer
|
||||
|
||||
1. Select the <xref:System.Windows.Forms.TreeView> control.
|
||||
|
||||
2. In the **Properties** window, click the **Ellipsis** () button next to the <xref:System.Windows.Forms.TreeView.Nodes%2A> property.
|
||||
|
||||
The **TreeNode Editor** appears.
|
||||
|
||||
3. To add nodes, a root node must exist; if one does not exist, you must first add a root by clicking the **Add Root** button. You can then add child nodes by selecting the root or any other node and clicking the **Add Child** button.
|
||||
|
||||
4. To delete nodes, select the node to delete and then click the **Delete** button.
|
||||
|
||||
## See also
|
||||
|
||||
- [TreeView Control](treeview-control-windows-forms.md)
|
||||
- [TreeView Control Overview](treeview-control-overview-windows-forms.md)
|
||||
- [How to: Set Icons for the Windows Forms TreeView Control](how-to-set-icons-for-the-windows-forms-treeview-control.md)
|
||||
- [How to: Iterate Through All Nodes of a Windows Forms TreeView Control](how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control.md)
|
||||
- [How to: Determine Which TreeView Node Was Clicked](how-to-determine-which-treeview-node-was-clicked-windows-forms.md)
|
||||
- [How to: Add Custom Information to a TreeView or ListView Control (Windows Forms)](add-custom-information-to-a-treeview-or-listview-control-wf.md)
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
---
|
||||
title: Add and Remove Tabs with TabControl Using the Designer
|
||||
description: Learn how to add and remove tabs with the Windows Forms TabControl control by using the designer.
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "tabs [Windows Forms], removing from pages"
|
||||
- "TabPage control"
|
||||
- "TabPage control [Windows Forms], adding and removing tabs"
|
||||
- "tabs [Windows Forms], adding to pages"
|
||||
- "tab pages"
|
||||
ms.assetid: 480633db-413a-45d2-9c8f-0427cc13adbe
|
||||
---
|
||||
# How to: Add and Remove Tabs with the Windows Forms TabControl Using the Designer
|
||||
When you place a <xref:System.Windows.Forms.TabControl> control on your form, it contains two tabs by default. You can add or remove tabs using the designer.
|
||||
|
||||
The following procedure requires a **Windows Application** project with a form containing a <xref:System.Windows.Forms.TabControl> control. For information about setting up such a project, see [How to: Create a Windows Forms application project](/visualstudio/ide/step-1-create-a-windows-forms-application-project) and [How to: Add Controls to Windows Forms](how-to-add-controls-to-windows-forms.md).
|
||||
|
||||
## To add or remove a tab using the designer
|
||||
|
||||
- On the control's smart tag, click **Add Tab** or **Remove Tab**
|
||||
|
||||
-or-
|
||||
|
||||
In the **Properties** window, click the **Ellipsis** button () next to the <xref:System.Windows.Forms.TabControl.TabPages%2A> property to open the **TabPage Collection Editor**. Click the **Add** or **Remove** button.
|
||||
|
||||
## See also
|
||||
|
||||
- [TabControl Control](tabcontrol-control-windows-forms.md)
|
||||
- [TabControl Control Overview](tabcontrol-control-overview-windows-forms.md)
|
||||
- [How to: Add a Control to a Tab Page](how-to-add-a-control-to-a-tab-page.md)
|
||||
- [How to: Disable Tab Pages](how-to-disable-tab-pages.md)
|
||||
- [How to: Change the Appearance of the Windows Forms TabControl](how-to-change-the-appearance-of-the-windows-forms-tabcontrol.md)
|
||||
+130
@@ -0,0 +1,130 @@
|
||||
---
|
||||
title: "How to: Add Custom Information to a TreeView or ListView Control"
|
||||
ms.date: "03/30/2017"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
- "cpp"
|
||||
f1_keywords:
|
||||
- "ListItem"
|
||||
helpviewer_keywords:
|
||||
- "examples [Windows Forms], TreeView control"
|
||||
- "examples [Windows Forms], ListView control"
|
||||
- "ListView control [Windows Forms], adding custom information"
|
||||
- "TreeView control [Windows Forms], adding custom information"
|
||||
ms.assetid: 68be11de-1d5b-430e-901f-cfbe48d14b19
|
||||
---
|
||||
# How to: Add Custom Information to a TreeView or ListView Control (Windows Forms)
|
||||
You can create a derived node in a Windows Forms <xref:System.Windows.Forms.TreeView> control or a derived item in a <xref:System.Windows.Forms.ListView> control. Derivation allows you to add any fields you require, as well as custom methods and constructors for handling them. One use of this feature is to attach a Customer object to each tree node or list item. The examples here are for a <xref:System.Windows.Forms.TreeView> control, but the same approach can be used for a <xref:System.Windows.Forms.ListView> control.
|
||||
|
||||
### To derive a tree node
|
||||
|
||||
- Create a new node class, derived from the <xref:System.Windows.Forms.TreeNode> class, which has a custom field to record a file path.
|
||||
|
||||
```vb
|
||||
Class myTreeNode
|
||||
Inherits TreeNode
|
||||
|
||||
Public FilePath As String
|
||||
|
||||
Sub New(ByVal fp As String)
|
||||
MyBase.New()
|
||||
FilePath = fp
|
||||
Me.Text = fp.Substring(fp.LastIndexOf("\"))
|
||||
End Sub
|
||||
End Class
|
||||
```
|
||||
|
||||
```csharp
|
||||
class myTreeNode : TreeNode
|
||||
{
|
||||
public string FilePath;
|
||||
|
||||
public myTreeNode(string fp)
|
||||
{
|
||||
FilePath = fp;
|
||||
this.Text = fp.Substring(fp.LastIndexOf("\\"));
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```cpp
|
||||
ref class myTreeNode : public TreeNode
|
||||
{
|
||||
public:
|
||||
System::String ^ FilePath;
|
||||
|
||||
myTreeNode(System::String ^ fp)
|
||||
{
|
||||
FilePath = fp;
|
||||
this->Text = fp->Substring(fp->LastIndexOf("\\"));
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
### To use a derived tree node
|
||||
|
||||
1. You can use the new derived tree node as a parameter to function calls.
|
||||
|
||||
In the example below, the path set for the location of the text file is the My Documents folder. This is done because you can assume that most computers running the Windows operating system will include this directory. This also allows users with minimal system access levels to safely run the application.
|
||||
|
||||
```vb
|
||||
' You should replace the bold text file
|
||||
' in the sample below with a text file of your own choosing.
|
||||
TreeView1.Nodes.Add(New myTreeNode (System.Environment.GetFolderPath _
|
||||
(System.Environment.SpecialFolder.Personal) _
|
||||
& "\ TextFile.txt ") )
|
||||
```
|
||||
|
||||
```csharp
|
||||
// You should replace the bold text file
|
||||
// in the sample below with a text file of your own choosing.
|
||||
// Note the escape character used (@) when specifying the path.
|
||||
treeView1.Nodes.Add(new myTreeNode(System.Environment.GetFolderPath
|
||||
(System.Environment.SpecialFolder.Personal)
|
||||
+ @"\TextFile.txt") );
|
||||
```
|
||||
|
||||
```cpp
|
||||
// You should replace the bold text file
|
||||
// in the sample below with a text file of your own choosing.
|
||||
treeView1->Nodes->Add(new myTreeNode(String::Concat(
|
||||
System::Environment::GetFolderPath
|
||||
(System::Environment::SpecialFolder::Personal),
|
||||
"\\TextFile.txt")));
|
||||
```
|
||||
|
||||
2. If you are passed the tree node and it is typed as a <xref:System.Windows.Forms.TreeNode> class, then you will need to cast to your derived class. Casting is an explicit conversion from one type of object to another. For more information on casting, see [Implicit and Explicit Conversions](https://docs.microsoft.com/dotnet/visual-basic/programming-guide/language-features/data-types/implicit-and-explicit-conversions) (Visual Basic), [Casting and type conversions](https://docs.microsoft.com/dotnet/csharp/programming-guide/types/casting-and-type-conversions) (Visual C#), or [Cast Operator: ()](/cpp/cpp/cast-operator-parens) (Visual C++).
|
||||
|
||||
```vb
|
||||
Public Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
|
||||
Dim mynode As myTreeNode
|
||||
mynode = CType(e.node, myTreeNode)
|
||||
MessageBox.Show("Node selected is " & mynode.filepath)
|
||||
End Sub
|
||||
```
|
||||
|
||||
```csharp
|
||||
protected void treeView1_AfterSelect (object sender,
|
||||
System.Windows.Forms.TreeViewEventArgs e)
|
||||
{
|
||||
myTreeNode myNode = (myTreeNode)e.Node;
|
||||
MessageBox.Show("Node selected is " + myNode.FilePath);
|
||||
}
|
||||
```
|
||||
|
||||
```cpp
|
||||
private:
|
||||
System::Void treeView1_AfterSelect(System::Object ^ sender,
|
||||
System::Windows::Forms::TreeViewEventArgs ^ e)
|
||||
{
|
||||
myTreeNode ^ myNode = safe_cast<myTreeNode^>(e->Node);
|
||||
MessageBox::Show(String::Concat("Node selected is ",
|
||||
myNode->FilePath));
|
||||
}
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [TreeView Control](treeview-control-windows-forms.md)
|
||||
- [ListView Control](listview-control-windows-forms.md)
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
---
|
||||
title: Add Tables and Columns to DataGrid Control Using the Designer
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "columns [Windows Forms], adding to DataGrid control"
|
||||
- "tables [Windows Forms], adding to DataGrid control"
|
||||
- "DataGrid control [Windows Forms], adding tables and columns"
|
||||
ms.assetid: 4a6d1b34-b696-476b-bf8a-57c6230aa9e1
|
||||
---
|
||||
# How to: Add Tables and Columns to the Windows Forms DataGrid Control Using the Designer
|
||||
|
||||
> [!NOTE]
|
||||
> The <xref:System.Windows.Forms.DataGridView> control replaces and adds functionality to the <xref:System.Windows.Forms.DataGrid> control; however, the <xref:System.Windows.Forms.DataGrid> control is retained for both backward compatibility and future use, if you choose. For more information, see [Differences Between the Windows Forms DataGridView and DataGrid Controls](differences-between-the-windows-forms-datagridview-and-datagrid-controls.md).
|
||||
|
||||
You can display data in the Windows Forms <xref:System.Windows.Forms.DataGrid> control in tables and columns by creating <xref:System.Windows.Forms.DataGridTableStyle> objects and adding them to the <xref:System.Windows.Forms.GridTableStylesCollection> object, which is accessed through the <xref:System.Windows.Forms.DataGrid> control's <xref:System.Windows.Forms.DataGrid.TableStyles%2A> property. Each table style displays the contents of whatever data table is specified in the <xref:System.Windows.Forms.DataGridTableStyle.MappingName%2A> property of the <xref:System.Windows.Forms.DataGridTableStyle>. By default, a table style without column styles specified will display all the columns within that data table. You can restrict which columns from the table appear by adding <xref:System.Windows.Forms.DataGridColumnStyle> objects to the <xref:System.Windows.Forms.GridColumnStylesCollection>, which is accessed through the <xref:System.Windows.Forms.DataGridTableStyle.GridColumnStyles%2A> property of each <xref:System.Windows.Forms.DataGridTableStyle>.
|
||||
|
||||
The following procedures require a **Windows Application** project with a form that contains a <xref:System.Windows.Forms.DataGrid> control. For information about how to set up such a project, see [How to: Create a Windows Forms application project](/visualstudio/ide/step-1-create-a-windows-forms-application-project) and [How to: Add Controls to Windows Forms](how-to-add-controls-to-windows-forms.md). By default in Visual Studio 2005, the <xref:System.Windows.Forms.DataGrid> control is not in the **Toolbox**. For information about adding it, see [How to: Add Items to the Toolbox](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2010/ms165355(v=vs.100)).
|
||||
|
||||
### To add a table to the DataGrid control in the designer
|
||||
|
||||
1. In order to display data in the table, you must first bind the <xref:System.Windows.Forms.DataGrid> control to a dataset. For more information, see [How to: Bind the Windows Forms DataGrid Control to a Data Source Using the Designer](bind-wf-datagrid-control-to-a-data-source-using-the-designer.md).
|
||||
|
||||
2. Select the <xref:System.Windows.Forms.DataGrid> control's <xref:System.Windows.Forms.DataGrid.TableStyles%2A> property in the Properties window, and then click the ellipsis button () next to the property to display the **DataGridTableStyle Collection Editor**.
|
||||
|
||||
3. In the collection editor, click **Add** to insert a table style.
|
||||
|
||||
4. Click **OK** to close the collection editor, and then reopen it by clicking the ellipsis button next to the <xref:System.Windows.Forms.DataGrid.TableStyles%2A> property.
|
||||
|
||||
When you reopen the collection editor, any data tables bound to the control will appear in the drop-down list for the <xref:System.Windows.Forms.DataGridTableStyle.MappingName%2A> property of the table style.
|
||||
|
||||
5. In the **Members** box of the collection editor, click the table style.
|
||||
|
||||
6. In the **Properties** box of the collection editor, select the <xref:System.Windows.Forms.DataGridTableStyle.MappingName%2A> value for the table you want to display.
|
||||
|
||||
### To add a column to the DataGrid control in the designer
|
||||
|
||||
1. In the **Members** box of the **DataGridTableStyle Collection Editor**, select the appropriate table style. In the **Properties** box of the collection editor, select the <xref:System.Windows.Forms.DataGridTableStyle.GridColumnStyles%2A> collection, and then click the ellipsis button () next to the property to display the **DataGridColumnStyle Collection Editor**.
|
||||
|
||||
2. In the collection editor, click **Add** to insert a column style or click the down arrow next to **Add** to specify a column type.
|
||||
|
||||
In the drop-down box, you can select either the <xref:System.Windows.Forms.DataGridTextBoxColumn> or <xref:System.Windows.Forms.DataGridBoolColumn> type.
|
||||
|
||||
3. Click OK to close the **DataGridColumnStyle Collection Editor**, and then reopen it by clicking the ellipsis button next to the <xref:System.Windows.Forms.DataGridTableStyle.GridColumnStyles%2A> property.
|
||||
|
||||
When you reopen the collection editor, any data columns in the bound data table will appear in the drop-down list for the <xref:System.Windows.Forms.DataGridColumnStyle.MappingName%2A> property of the column style.
|
||||
|
||||
4. In the **Members** box of the collection editor, click the column style.
|
||||
|
||||
5. In the **Properties** box of the collection editor, select the <xref:System.Windows.Forms.DataGridColumnStyle.MappingName%2A> value for the column you want to display.
|
||||
|
||||
## See also
|
||||
|
||||
- [DataGrid Control](datagrid-control-windows-forms.md)
|
||||
- [How to: Delete or Hide Columns in the Windows Forms DataGrid Control](how-to-delete-or-hide-columns-in-the-windows-forms-datagrid-control.md)
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
---
|
||||
title: Add ToolTips to Individual Cells in DataGridView Control
|
||||
ms.date: "03/30/2017"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
- "cpp"
|
||||
helpviewer_keywords:
|
||||
- "tooltips [Windows Forms], adding to data grids"
|
||||
- "DataGridView control [Windows Forms], adding tooltips"
|
||||
- "data grids [Windows Forms], adding tooltips"
|
||||
ms.assetid: 2a81f9de-d58b-4ea8-bc0b-8d93c2f4cf78
|
||||
---
|
||||
# How to: Add ToolTips to Individual Cells in a Windows Forms DataGridView Control
|
||||
By default, ToolTips are used to display the values of <xref:System.Windows.Forms.DataGridView> cells that are too small to show their entire contents. You can override this behavior, however, to set ToolTip-text values for individual cells. This is useful to display to users additional information about a cell or to provide to users an alternate description of the cell contents. For example, if you have a row that displays status icons, you may want to provide text explanations using ToolTips.
|
||||
|
||||
You can also disable the display of cell-level ToolTips by setting the <xref:System.Windows.Forms.DataGridView.ShowCellToolTips%2A?displayProperty=nameWithType> property to `false`.
|
||||
|
||||
### To add a ToolTip to a cell
|
||||
|
||||
- Set the <xref:System.Windows.Forms.DataGridViewCell.ToolTipText%2A?displayProperty=nameWithType> property.
|
||||
|
||||
[!code-cpp[System.Windows.Forms.DataGridViewCell.ToolTipText#1](~/samples/snippets/cpp/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewCell.ToolTipText/cpp/datagridviewcell.tooltiptext.cpp#1)]
|
||||
[!code-csharp[System.Windows.Forms.DataGridViewCell.ToolTipText#1](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewCell.ToolTipText/CS/datagridviewcell.tooltiptext.cs#1)]
|
||||
[!code-vb[System.Windows.Forms.DataGridViewCell.ToolTipText#1](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewCell.ToolTipText/VB/datagridviewcell.tooltiptext.vb#1)]
|
||||
|
||||
## Compiling the Code
|
||||
|
||||
- This example requires:
|
||||
|
||||
- A <xref:System.Windows.Forms.DataGridView> control named `dataGridView1` that contains a column named `Rating` for displaying string values of one through four asterisk ("*") symbols. The <xref:System.Windows.Forms.DataGridView.CellFormatting> event of the control must be associated with the event handler method shown in the example.
|
||||
|
||||
- References to the <xref:System?displayProperty=nameWithType> and <xref:System.Windows.Forms?displayProperty=nameWithType> assemblies.
|
||||
|
||||
## Robust Programming
|
||||
When you bind the <xref:System.Windows.Forms.DataGridView> control to an external data source or provide your own data source by implementing virtual mode, you might encounter performance issues. To avoid a performance penalty when working with large amounts of data, handle the <xref:System.Windows.Forms.DataGridView.CellToolTipTextNeeded> event rather than setting the <xref:System.Windows.Forms.DataGridViewCell.ToolTipText%2A> property of multiple cells. When you handle this event, getting the value of a cell <xref:System.Windows.Forms.DataGridViewCell.ToolTipText%2A> property raises the event and returns the value of the <xref:System.Windows.Forms.DataGridViewCellToolTipTextNeededEventArgs.ToolTipText%2A?displayProperty=nameWithType> property as specified in the event handler.
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridView>
|
||||
- <xref:System.Windows.Forms.DataGridView.ShowCellToolTips%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridView.CellToolTipTextNeeded?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridViewCell>
|
||||
- <xref:System.Windows.Forms.DataGridViewCell.ToolTipText%2A?displayProperty=nameWithType>
|
||||
- [Programming with Cells, Rows, and Columns in the Windows Forms DataGridView Control](programming-with-cells-rows-and-columns-in-the-datagrid.md)
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
---
|
||||
title: Add Application Icons to the TaskBar with NotifyIcon Component
|
||||
ms.date: "03/30/2017"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
- "cpp"
|
||||
f1_keywords:
|
||||
- "TrayIcon"
|
||||
helpviewer_keywords:
|
||||
- "status area icons"
|
||||
- "icons [Windows Forms], adding to taskbar"
|
||||
- "NotifyIcon component"
|
||||
- "taskbar [Windows Forms], adding icons"
|
||||
ms.assetid: d28c0fe6-aaf2-4df7-ad74-928d861a8510
|
||||
---
|
||||
# How to: Add Application Icons to the TaskBar with the Windows Forms NotifyIcon Component
|
||||
|
||||
The Windows Forms <xref:System.Windows.Forms.NotifyIcon> component displays a single icon in the status notification area of the taskbar. To display multiple icons in the status area, you must have multiple <xref:System.Windows.Forms.NotifyIcon> components on your form. To set the icon displayed for a control, use the <xref:System.Windows.Forms.NotifyIcon.Icon%2A> property. You can also write code in the <xref:System.Windows.Forms.NotifyIcon.DoubleClick> event handler so that something happens when the user double-clicks the icon. For example, you could make a dialog box appear for the user to configure the background process represented by the icon.
|
||||
|
||||
> [!NOTE]
|
||||
> The <xref:System.Windows.Forms.NotifyIcon> component is used for notification purposes only, to alert users that an action or event has occurred or there has been a change in status of some sort. You should use menus, toolbars, and other user-interface elements for standard interaction with applications.
|
||||
|
||||
### To set the icon
|
||||
|
||||
1. Assign a value to the <xref:System.Windows.Forms.NotifyIcon.Icon%2A> property. The value must be of type `System.Drawing.Icon` and can be loaded from an .ico file. You can specify the icon file in code or by clicking the ellipsis button () next to the <xref:System.Windows.Forms.NotifyIcon.Icon%2A> property in the **Properties** window, and then selecting the file in the **Open** dialog box that appears.
|
||||
|
||||
2. Set the <xref:System.Windows.Forms.NotifyIcon.Visible%2A> property to `true`.
|
||||
|
||||
3. Set the <xref:System.Windows.Forms.NotifyIcon.Text%2A> property to an appropriate ToolTip string.
|
||||
|
||||
In the following code example, the path set for the location of the icon is the **My Documents** folder. This location is used because you can assume that most computers running the Windows operating system will include this folder. Choosing this location also enables users with minimal system access levels to safely run the application. The following example requires a form with a <xref:System.Windows.Forms.NotifyIcon> control already added. It also requires an icon file named `Icon.ico`.
|
||||
|
||||
```vb
|
||||
' You should replace the bold icon in the sample below
|
||||
' with an icon of your own choosing.
|
||||
NotifyIcon1.Icon = New _
|
||||
System.Drawing.Icon(System.Environment.GetFolderPath _
|
||||
(System.Environment.SpecialFolder.Personal) _
|
||||
& "\Icon.ico")
|
||||
NotifyIcon1.Visible = True
|
||||
NotifyIcon1.Text = "Antivirus program"
|
||||
```
|
||||
|
||||
```csharp
|
||||
// You should replace the bold icon in the sample below
|
||||
// with an icon of your own choosing.
|
||||
// Note the escape character used (@) when specifying the path.
|
||||
notifyIcon1.Icon =
|
||||
new System.Drawing.Icon (System.Environment.GetFolderPath
|
||||
(System.Environment.SpecialFolder.Personal)
|
||||
+ @"\Icon.ico");
|
||||
notifyIcon1.Visible = true;
|
||||
notifyIcon1.Text = "Antivirus program";
|
||||
```
|
||||
|
||||
```cpp
|
||||
// You should replace the bold icon in the sample below
|
||||
// with an icon of your own choosing.
|
||||
notifyIcon1->Icon = gcnew
|
||||
System::Drawing::Icon(String::Concat
|
||||
(System::Environment::GetFolderPath
|
||||
(System::Environment::SpecialFolder::Personal),
|
||||
"\\Icon.ico"));
|
||||
notifyIcon1->Visible = true;
|
||||
notifyIcon1->Text = "Antivirus program";
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.NotifyIcon>
|
||||
- <xref:System.Windows.Forms.NotifyIcon.Icon%2A>
|
||||
- [How to: Associate a Shortcut Menu with a Windows Forms NotifyIcon Component](how-to-associate-a-shortcut-menu-with-a-windows-forms-notifyicon-component.md)
|
||||
- [NotifyIcon Component](notifyicon-component-windows-forms.md)
|
||||
- [NotifyIcon Component Overview](notifyicon-component-overview-windows-forms.md)
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
---
|
||||
title: Attributes in Controls
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "attributes [Windows Forms]"
|
||||
- "attributes [Windows Forms], data binding properties"
|
||||
- "attributes [Windows Forms], control properties"
|
||||
- "attributes [Windows Forms], classes"
|
||||
ms.assetid: 2c5640e9-6c6c-49d7-a5e4-a768f6be7853
|
||||
---
|
||||
# Attributes in Windows Forms Controls
|
||||
The .NET Framework provides a variety of attributes you can apply to the members of your custom controls and components. Some of these attributes affect the run-time behavior of a class, and others affect the design-time behavior.
|
||||
|
||||
## Attributes for Control and Component Properties
|
||||
The following table shows the attributes you can apply to properties or other members of your custom controls and components. For an example that uses many of these attributes, see [How to: Apply Attributes in Windows Forms Controls](how-to-apply-attributes-in-windows-forms-controls.md).
|
||||
|
||||
|Attribute|Description|
|
||||
|---------------|-----------------|
|
||||
|<xref:System.ComponentModel.AmbientValueAttribute>|Specifies the value to pass to a property to cause the property to get its value from another source. This is known as *ambience*.|
|
||||
|<xref:System.ComponentModel.BrowsableAttribute>|Specifies whether a property or event should be displayed in a **Properties** window.|
|
||||
|<xref:System.ComponentModel.CategoryAttribute>|Specifies the name of the category in which to group the property or event when displayed in a <xref:System.Windows.Forms.PropertyGrid> control set to <xref:System.Windows.Forms.PropertySort.Categorized> mode.|
|
||||
|<xref:System.ComponentModel.DefaultValueAttribute>|Specifies the default value for a property.|
|
||||
|<xref:System.ComponentModel.DescriptionAttribute>|Specifies a description for a property or event.|
|
||||
|<xref:System.ComponentModel.DisplayNameAttribute>|Specifies the display name for a property, event, or `public void` method that takes no arguments.|
|
||||
|<xref:System.ComponentModel.EditorAttribute>|Specifies the editor to use to change a property.|
|
||||
|<xref:System.ComponentModel.EditorBrowsableAttribute>|Specifies that a property or method is viewable in an editor.|
|
||||
|<xref:System.ComponentModel.Design.HelpKeywordAttribute>|Specifies the context keyword for a class or member.|
|
||||
|<xref:System.ComponentModel.LocalizableAttribute>|Specifies whether a property should be localized.|
|
||||
|<xref:System.ComponentModel.PasswordPropertyTextAttribute>|Indicates that an object's text representation is obscured by characters such as asterisks.|
|
||||
|<xref:System.ComponentModel.ReadOnlyAttribute>|Specifies whether the property this attribute is bound to is read-only or read/write at design time.|
|
||||
|<xref:System.ComponentModel.RefreshPropertiesAttribute>|Indicates that the property grid should refresh when the associated property value changes.|
|
||||
|<xref:System.ComponentModel.TypeConverterAttribute>|Specifies what type to use as a converter for the object this attribute is bound to.|
|
||||
|
||||
## Attributes for Data Binding Properties
|
||||
The following table shows the attributes you can apply to specify how your custom controls and components interact with data binding.
|
||||
|
||||
|Attribute|Description|
|
||||
|---------------|-----------------|
|
||||
|<xref:System.ComponentModel.BindableAttribute>|Specifies whether a property is typically used for binding.|
|
||||
|<xref:System.ComponentModel.ComplexBindingPropertiesAttribute>|Specifies the data source and data member properties for a component.|
|
||||
|<xref:System.ComponentModel.DefaultBindingPropertyAttribute>|Specifies the default binding property for a component.|
|
||||
|<xref:System.ComponentModel.LookupBindingPropertiesAttribute>|Specifies the data source and data member properties for a component.|
|
||||
|<xref:System.ComponentModel.AttributeProviderAttribute>|Enables attribute redirection.|
|
||||
|
||||
## Attributes for Classes
|
||||
The following table shows the attributes you can apply to specify the behavior of your custom controls and components at design time.
|
||||
|
||||
|Attribute|Description|
|
||||
|---------------|-----------------|
|
||||
|<xref:System.ComponentModel.DefaultEventAttribute>|Specifies the default event for a component.|
|
||||
|<xref:System.ComponentModel.DefaultPropertyAttribute>|Specifies the default property for a component.|
|
||||
|<xref:System.ComponentModel.DesignerAttribute>|Specifies the class used to implement design-time services for a component.|
|
||||
|<xref:System.ComponentModel.DesignerCategoryAttribute>|Specifies that the designer for a class belongs to a certain category.|
|
||||
|<xref:System.ComponentModel.ToolboxItemAttribute>|Represents an attribute of a toolbox item.|
|
||||
|<xref:System.ComponentModel.ToolboxItemFilterAttribute>|Specifies the filter string and filter type to use for a Toolbox item.|
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Attribute>
|
||||
- [How to: Apply Attributes in Windows Forms Controls](how-to-apply-attributes-in-windows-forms-controls.md)
|
||||
- [Extending Design-Time Support](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2013/37899azc(v=vs.120))
|
||||
- [Developing Custom Windows Forms Controls with the .NET Framework](developing-custom-windows-forms-controls.md)
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
---
|
||||
title: Autogenerate Columns in Data-Bound DataGridView Control
|
||||
ms.date: "03/30/2017"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
helpviewer_keywords:
|
||||
- "data grids [Windows Forms], autogenerating columns"
|
||||
- "columns [Windows Forms], autogenerating"
|
||||
- "DataGridView control [Windows Forms], data-bound columns"
|
||||
ms.assetid: 699f6f9e-6aa5-4811-902b-6a2c57dec7d6
|
||||
---
|
||||
# How to: Autogenerate Columns in a Data-Bound Windows Forms DataGridView Control
|
||||
The following code example demonstrates how to display columns from a bound data source in a <xref:System.Windows.Forms.DataGridView> control. When the <xref:System.Windows.Forms.DataGridView.AutoGenerateColumns%2A> property value is `true` (the default), a <xref:System.Windows.Forms.DataGridViewColumn> is created for each column in the data source table.
|
||||
|
||||
If the <xref:System.Windows.Forms.DataGridView> control already has columns when you set the <xref:System.Windows.Forms.DataGridViewComboBoxColumn.DataSource%2A> property, the existing bound columns are compared to the columns in the data source and preserved whenever there is a match. Unbound columns are always preserved. Bound columns for which there is no match in the data source are removed. Columns in the data source for which there is no match in the control generate new <xref:System.Windows.Forms.DataGridViewColumn> objects, which are added to the end of the <xref:System.Windows.Forms.DataGridView.Columns%2A> collection.
|
||||
|
||||
## Example
|
||||
[!code-csharp[System.Windows.Forms.DataGridViewMisc#020](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewMisc/CS/datagridviewmisc.cs#020)]
|
||||
[!code-vb[System.Windows.Forms.DataGridViewMisc#020](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewMisc/VB/datagridviewmisc.vb#020)]
|
||||
|
||||
## Compiling the Code
|
||||
This example requires:
|
||||
|
||||
- A <xref:System.Windows.Forms.DataGridView> control named `customersDataGridView`.
|
||||
|
||||
- A <xref:System.Data.DataSet> object named `customersDataSet` that has a table named `Customers`.
|
||||
|
||||
- References to the <xref:System?displayProperty=nameWithType>, <xref:System.Windows.Forms?displayProperty=nameWithType>, <xref:System.Data?displayProperty=nameWithType>, and <xref:System.Xml?displayProperty=nameWithType> assemblies.
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridView>
|
||||
- <xref:System.Windows.Forms.DataGridView.AutoGenerateColumns%2A?displayProperty=nameWithType>
|
||||
- [Displaying Data in the Windows Forms DataGridView Control](displaying-data-in-the-windows-forms-datagridview-control.md)
|
||||
- [How to: Remove Autogenerated Columns from a Windows Forms DataGridView Control](remove-autogenerated-columns-from-a-wf-datagridview-control.md)
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
---
|
||||
title: Automatically Resize Cells When Content Changes in DataGridView Control
|
||||
ms.date: "03/30/2017"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
- "cpp"
|
||||
helpviewer_keywords:
|
||||
- "data grids [Windows Forms], resizing cells automatically"
|
||||
- "cells [Windows Forms], resizing automatically"
|
||||
- "DataGridView control [Windows Forms], resizing cells"
|
||||
ms.assetid: 1d68934d-a04c-4b12-9e66-c856c6828131
|
||||
---
|
||||
# How to: Automatically Resize Cells When Content Changes in the Windows Forms DataGridView Control
|
||||
You can configure the <xref:System.Windows.Forms.DataGridView> control to resize its rows, columns, and headers automatically whenever content changes, so that cells are always large enough to display their values without clipping.
|
||||
|
||||
You have many options to restrict which cells are used to determine the new sizes. For example, you can configure the control to automatically resize the width of its columns based only on the values in rows that are currently displayed. With this, you can avoid inefficiency when working with large numbers of rows, although in this case, you might want to use sizing methods such as <xref:System.Windows.Forms.DataGridView.AutoResizeColumns%2A> to adjust sizes at times of your choosing.
|
||||
|
||||
For more information about automatic resizing, see [Sizing Options in the Windows Forms DataGridView Control](sizing-options-in-the-windows-forms-datagridview-control.md).
|
||||
|
||||
The following code example demonstrates the options available for automatic resizing.
|
||||
|
||||
## Example
|
||||
[!code-cpp[System.Windows.Forms.DataGridView.AutoSizing#0](~/samples/snippets/cpp/VS_Snippets_Winforms/System.Windows.Forms.DataGridView.AutoSizing/CPP/autosizing.cpp#0)]
|
||||
[!code-csharp[System.Windows.Forms.DataGridView.AutoSizing#0](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.DataGridView.AutoSizing/CS/autosizing.cs#0)]
|
||||
[!code-vb[System.Windows.Forms.DataGridView.AutoSizing#0](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.DataGridView.AutoSizing/VB/autosizing.vb#0)]
|
||||
|
||||
## Compiling the Code
|
||||
This example requires:
|
||||
|
||||
- References to the System, System.Drawing, and System.Windows.Forms assemblies.
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridView>
|
||||
- <xref:System.Windows.Forms.DataGridView.ColumnHeadersHeightSizeMode%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridView.RowHeadersWidthSizeMode%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridView.AutoSizeColumnsMode%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridView.AutoSizeRowsMode%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridViewColumn.AutoSizeMode%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridViewColumn.InheritedAutoSizeMode%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridViewAutoSizeRowsMode>
|
||||
- <xref:System.Windows.Forms.DataGridViewAutoSizeColumnMode>
|
||||
- <xref:System.Windows.Forms.DataGridViewAutoSizeColumnsMode>
|
||||
- <xref:System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode>
|
||||
- <xref:System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode>
|
||||
- [Resizing Columns and Rows in the Windows Forms DataGridView Control](resizing-columns-and-rows-in-the-windows-forms-datagridview-control.md)
|
||||
- [Sizing Options in the Windows Forms DataGridView Control](sizing-options-in-the-windows-forms-datagridview-control.md)
|
||||
- [How to: Programmatically Resize Cells to Fit Content in the Windows Forms DataGridView Control](programmatically-resize-cells-to-fit-content-in-the-datagrid.md)
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
---
|
||||
title: "AutoSize Behavior in the TableLayoutPanel Control"
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "AutoSize property [Windows Forms], tableLayoutPanel control"
|
||||
- "controls [Windows Forms], sizing"
|
||||
- "localizing forms"
|
||||
- "layout [Windows Forms], AutoSize"
|
||||
- "sizing [Windows Forms], automatic"
|
||||
- "TableLayoutPanel control [Windows Forms], AutoSize behavior"
|
||||
- "automatic sizing"
|
||||
- "AutoSizeMode property"
|
||||
ms.assetid: 9233e0c3-2fa6-405e-8701-959479b1250e
|
||||
---
|
||||
# AutoSize Behavior in the TableLayoutPanel Control
|
||||
## Distinct AutoSize Behaviors
|
||||
The <xref:System.Windows.Forms.TableLayoutPanel> control supports automatic sizing behavior in the following ways:
|
||||
|
||||
- Through the <xref:System.Windows.Forms.Control.AutoSize%2A> property;
|
||||
|
||||
- Through the <xref:System.Windows.Forms.TableLayoutStyle.SizeType%2A> property on the <xref:System.Windows.Forms.TableLayoutPanel> control’s column and row styles.
|
||||
|
||||
### The AutoSize Property with Row and Column Styles
|
||||
The following table describes the interaction between the <xref:System.Windows.Forms.Control.AutoSize%2A> property and the <xref:System.Windows.Forms.TableLayoutPanel> control’s column and row styles.
|
||||
|
||||
|AutoSize setting|Style interaction|
|
||||
|----------------------|-----------------------|
|
||||
|`false`|The <xref:System.Windows.Forms.TableLayoutPanel> control proceeds from left to right, and allocates space for the column or row or in the following order.<br /><br /> 1. If the <xref:System.Windows.Forms.TableLayoutStyle.SizeType%2A> property is set to <xref:System.Windows.Forms.SizeType.Absolute>, the number of pixels specified by <xref:System.Windows.Forms.ColumnStyle.Width%2A> or <xref:System.Windows.Forms.RowStyle.Height%2A> is allocated.<br />2. If the <xref:System.Windows.Forms.TableLayoutStyle.SizeType%2A> property is set to <xref:System.Windows.Forms.SizeType.AutoSize>, the number of pixels returned by the child control’s <xref:System.Windows.Forms.Control.GetPreferredSize%2A> method is allocated.<br />3. After space for all <xref:System.Windows.Forms.SizeType.Absolute> and <xref:System.Windows.Forms.SizeType.AutoSize> columns or rows is allocated, any columns or rows with <xref:System.Windows.Forms.TableLayoutStyle.SizeType%2A> set to <xref:System.Windows.Forms.SizeType.Percent> are used to proportionally allocate the remaining free space|
|
||||
|`true`|Similar to the previous interaction, with the exception that <xref:System.Windows.Forms.SizeType.Percent> columns or rows acquire an automatic sizing aspect.<br /><br /> The <xref:System.Windows.Forms.TableLayoutPanel> control expands the column or row to create adequate free space, so that no column or row with <xref:System.Windows.Forms.SizeType.Percent> styling clips its contents. The <xref:System.Windows.Forms.TableLayoutPanel> control allocates the new space proportionally according to the <xref:System.Windows.Forms.ColumnStyle.Width%2A> or <xref:System.Windows.Forms.RowStyle.Height%2A> property.|
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.TableLayoutPanel>
|
||||
- [TableLayoutPanel Control Overview](tablelayoutpanel-control-overview.md)
|
||||
@@ -0,0 +1,67 @@
|
||||
---
|
||||
title: "AutoSize Property Overview"
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "sizing [Windows Forms], automatic"
|
||||
- "layout [Windows Forms], AutoSize"
|
||||
- "automatic sizing"
|
||||
- "AutoSizeMode property"
|
||||
ms.assetid: 62fd82a2-9565-4f65-925b-9d1e66dc4e7d
|
||||
---
|
||||
# AutoSize Property Overview
|
||||
The <xref:System.Windows.Forms.Control.AutoSize%2A> property enables a control to change its size, if necessary, to attain the value specified by the <xref:System.Windows.Forms.Control.PreferredSize%2A> property. You adjust the sizing behavior for specific controls by setting the `AutoSizeMode` property.
|
||||
|
||||
## AutoSize Behavior
|
||||
Only some controls support the <xref:System.Windows.Forms.Control.AutoSize%2A> property. In addition, some controls that support the <xref:System.Windows.Forms.Control.AutoSize%2A> property also support the `AutoSizeMode` property.
|
||||
|
||||
The <xref:System.Windows.Forms.Control.AutoSize%2A> property produces somewhat different behavior, depending on the specific control type and the value of the `AutoSizeMode` property, if the property exists. The following table describes the behaviors that are always true and provides a brief description of each:
|
||||
|
||||
|Always true behavior|Description|
|
||||
|--------------------------|-----------------|
|
||||
|Automatic sizing is a run-time feature.|This means it never grows or shrinks a control and then has no further effect.|
|
||||
|If a control changes size, the value of its <xref:System.Windows.Forms.Control.Location%2A> property always remains constant.|When a control's contents cause it to grow, the control grows toward the right and downward. Controls do not grow to the left.|
|
||||
|The <xref:System.Windows.Forms.Control.Dock%2A> and <xref:System.Windows.Forms.Control.Anchor%2A> properties are honored when <xref:System.Windows.Forms.Control.AutoSize%2A> is `true`.|The value of the control's <xref:System.Windows.Forms.Control.Location%2A> property is adjusted to the correct value.<br /><br /> **Note** The <xref:System.Windows.Forms.Label> control is the exception to this rule. When you set the value of a docked <xref:System.Windows.Forms.Label> control's <xref:System.Windows.Forms.Control.AutoSize%2A> property to `true`, the <xref:System.Windows.Forms.Label> control will not stretch.|
|
||||
|A control's <xref:System.Windows.Forms.Control.MaximumSize%2A> and <xref:System.Windows.Forms.Control.MinimumSize%2A> properties are always honored, regardless of the value of its <xref:System.Windows.Forms.Control.AutoSize%2A> property.|The <xref:System.Windows.Forms.Control.MaximumSize%2A> and <xref:System.Windows.Forms.Control.MinimumSize%2A> properties are not affected by the <xref:System.Windows.Forms.Control.AutoSize%2A> property.|
|
||||
|There is no minimum size set by default.|This means that if a control is set to shrink under <xref:System.Windows.Forms.Control.AutoSize%2A> and it has no contents, the value of its <xref:System.Windows.Forms.Control.Size%2A> property is 0,0. In this case, your control will shrink to a point, and it will not be readily visible.|
|
||||
|If a control does not implement the <xref:System.Windows.Forms.Control.GetPreferredSize%2A> method, the <xref:System.Windows.Forms.Control.GetPreferredSize%2A> method returns last value assigned to the <xref:System.Windows.Forms.Control.Size%2A> property.|This means that setting <xref:System.Windows.Forms.Control.AutoSize%2A> to `true` will have no effect.|
|
||||
|A control in a <xref:System.Windows.Forms.TableLayoutPanel> cell always shrinks to fit in the cell until its <xref:System.Windows.Forms.Control.MinimumSize%2A> is reached.|This size is enforced as a maximum size. This is not the case when the cell is part of an <xref:System.Windows.Forms.SizeType.AutoSize> row or column.|
|
||||
|
||||
## AutoSizeMode Property
|
||||
The `AutoSizeMode` property provides more fine-grained control over the default <xref:System.Windows.Forms.Control.AutoSize%2A> behavior. The `AutoSizeMode` property specifies how a control sizes itself to its content. The content, for example, could be the text for a <xref:System.Windows.Forms.Button> control or the child controls for a container.
|
||||
|
||||
The following table shows the <xref:System.Windows.Forms.AutoSizeMode> settings and a description of the behavior each setting elicits.
|
||||
|
||||
|AutoSizeMode setting|Behavior|
|
||||
|--------------------------|--------------|
|
||||
|GrowAndShrink|The control grows or shrinks to encompass its contents.<br /><br /> The <xref:System.Windows.Forms.Control.MinimumSize%2A> and <xref:System.Windows.Forms.Control.MaximumSize%2A> values are honored, but the current value of the <xref:System.Windows.Forms.Control.Size%2A> property is ignored.<br /><br /> This is the same behavior as controls with the <xref:System.Windows.Forms.Control.AutoSize%2A> property and no `AutoSizeMode` property.|
|
||||
|GrowOnly|The control grows as much as necessary to encompass its contents, but it will not shrink smaller than the value specified by its <xref:System.Windows.Forms.Control.Size%2A> property.<br /><br /> This is the default value for `AutoSizeMode`.|
|
||||
|
||||
## Controls That Support the AutoSize Property
|
||||
The following table lists the controls that support the <xref:System.Windows.Forms.Control.AutoSize%2A> and `AutoSizeMode` properties.
|
||||
|
||||
|AutoSize support|Control type|
|
||||
|----------------------|------------------|
|
||||
|- <xref:System.Windows.Forms.Control.AutoSize%2A> property supported.<br />- No `AutoSizeMode` property.|<xref:System.Windows.Forms.CheckBox><br /><br /> <xref:System.Windows.Forms.DomainUpDown><br /><br /> <xref:System.Windows.Forms.Label><br /><br /> <xref:System.Windows.Forms.LinkLabel><br /><br /> <xref:System.Windows.Forms.MaskedTextBox> (<xref:System.Windows.Forms.TextBox> base)<br /><br /> <xref:System.Windows.Forms.NumericUpDown><br /><br /> <xref:System.Windows.Forms.RadioButton><br /><br /> <xref:System.Windows.Forms.TextBox><br /><br /> <xref:System.Windows.Forms.TrackBar>|
|
||||
|- <xref:System.Windows.Forms.Control.AutoSize%2A> property supported.<br />- `AutoSizeMode` property supported.|<xref:System.Windows.Forms.Button><br /><br /> <xref:System.Windows.Forms.CheckedListBox><br /><br /> <xref:System.Windows.Forms.FlowLayoutPanel><br /><br /> <xref:System.Windows.Forms.Form><br /><br /> <xref:System.Windows.Forms.GroupBox><br /><br /> <xref:System.Windows.Forms.Panel><br /><br /> <xref:System.Windows.Forms.TableLayoutPanel>|
|
||||
|- No <xref:System.Windows.Forms.Control.AutoSize%2A> property.|<xref:System.Windows.Forms.CheckedListBox><br /><br /> <xref:System.Windows.Forms.ComboBox><br /><br /> <xref:System.Windows.Forms.DataGridView><br /><br /> <xref:System.Windows.Forms.DateTimePicker><br /><br /> <xref:System.Windows.Forms.ListBox><br /><br /> <xref:System.Windows.Forms.ListView><br /><br /> <xref:System.Windows.Forms.MaskedTextBox><br /><br /> <xref:System.Windows.Forms.MonthCalendar><br /><br /> <xref:System.Windows.Forms.ProgressBar><br /><br /> <xref:System.Windows.Forms.PropertyGrid><br /><br /> <xref:System.Windows.Forms.RichTextBox><br /><br /> <xref:System.Windows.Forms.SplitContainer><br /><br /> <xref:System.Windows.Forms.TabControl><br /><br /> <xref:System.Windows.Forms.TabPage><br /><br /> <xref:System.Windows.Forms.TreeView><br /><br /> <xref:System.Windows.Forms.WebBrowser><br /><br /> <xref:System.Windows.Forms.ScrollBar>|
|
||||
|
||||
## AutoSize in the Design Environment
|
||||
The following table describes the sizing behavior of a control at design time, based on the value of its <xref:System.Windows.Forms.Control.AutoSize%2A> and `AutoSizeMode` properties.
|
||||
|
||||
Override the <xref:System.Windows.Forms.Design.ControlDesigner.SelectionRules%2A> property to determine whether a given control is in a user-resizable state. In the following table, "cannot" means <xref:System.Windows.Forms.Design.SelectionRules.Moveable> only, "can" means <xref:System.Windows.Forms.Design.SelectionRules.AllSizeable> and <xref:System.Windows.Forms.Design.SelectionRules.Moveable>.
|
||||
|
||||
|AutoSize settings|Design-time sizing gesture|
|
||||
|-----------------------|---------------------------------|
|
||||
|- <xref:System.Windows.Forms.Control.AutoSize%2A> = `true`<br />- No `AutoSizeMode` property.|The user cannot resize the control at design time, except for the following controls:<br /><br /> - <xref:System.Windows.Forms.TextBox><br />- <xref:System.Windows.Forms.MaskedTextBox><br />- <xref:System.Windows.Forms.RichTextBox><br />- <xref:System.Windows.Forms.TrackBar>|
|
||||
|- <xref:System.Windows.Forms.Control.AutoSize%2A> = `true`<br />- `AutoSizeMode` = <xref:System.Windows.Forms.AutoSizeMode.GrowAndShrink>|The user cannot resize the control at design time.|
|
||||
|- <xref:System.Windows.Forms.Control.AutoSize%2A> = `true`<br />- `AutoSizeMode` = <xref:System.Windows.Forms.AutoSizeMode.GrowOnly>|The user can resize the control at design time. When the <xref:System.Windows.Forms.Control.Size%2A> property is set, the user can only increase the size of the control.|
|
||||
|- <xref:System.Windows.Forms.Control.AutoSize%2A> = `false`, or <xref:System.Windows.Forms.Control.AutoSize%2A> property is hidden.|User can resize the control at design time.|
|
||||
|
||||
> [!NOTE]
|
||||
> To maximize productivity, the Windows Forms Designer in Visual Studio shadows the <xref:System.Windows.Forms.Control.AutoSize%2A> property for the <xref:System.Windows.Forms.Form> class. At design time, the form behaves as though the <xref:System.Windows.Forms.Control.AutoSize%2A> property is set to `false`, regardless of its actual setting. At runtime, no special accommodation is made, and the <xref:System.Windows.Forms.Control.AutoSize%2A> property is applied as specified by the property setting.
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.Control.AutoSize%2A>
|
||||
- <xref:System.Windows.Forms.Control.PreferredSize%2A>
|
||||
- <xref:System.Windows.Forms.Control.GetPreferredSize%2A>
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
---
|
||||
title: "BackgroundWorker Component Overview"
|
||||
ms.date: "03/30/2017"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
- "cpp"
|
||||
f1_keywords:
|
||||
- "BackgroundWorker"
|
||||
helpviewer_keywords:
|
||||
- "BackgroundWorker component"
|
||||
- "background tasks"
|
||||
- "Asynchronous Pattern"
|
||||
- "forms [Windows Forms], multithreading"
|
||||
- "components [Windows Forms], asynchronous"
|
||||
- "forms [Windows Forms], background operations"
|
||||
- "threading [Windows Forms], background operations"
|
||||
- "background operations"
|
||||
ms.assetid: 64e9b3ab-7443-4a77-ab17-b8b8c0cb3f62
|
||||
---
|
||||
# BackgroundWorker Component Overview
|
||||
There are many commonly performed operations that can take a long time to execute. For example:
|
||||
|
||||
- Image downloads
|
||||
|
||||
- Web service invocations
|
||||
|
||||
- File downloads and uploads (including for peer-to-peer applications)
|
||||
|
||||
- Complex local computations
|
||||
|
||||
- Database transactions
|
||||
|
||||
- Local disk access, given its slow speed relative to memory access
|
||||
|
||||
Operations like these can cause your user interface to block while they are running. When you want a responsive UI and you are faced with long delays associated with such operations, the <xref:System.ComponentModel.BackgroundWorker> component provides a convenient solution.
|
||||
|
||||
The <xref:System.ComponentModel.BackgroundWorker> component gives you the ability to execute time-consuming operations asynchronously ("in the background"), on a thread different from your application's main UI thread. To use a <xref:System.ComponentModel.BackgroundWorker>, you simply tell it what time-consuming worker method to execute in the background, and then you call the <xref:System.ComponentModel.BackgroundWorker.RunWorkerAsync%2A> method. Your calling thread continues to run normally while the worker method runs asynchronously. When the method is finished, the <xref:System.ComponentModel.BackgroundWorker> alerts the calling thread by firing the <xref:System.ComponentModel.BackgroundWorker.RunWorkerCompleted> event, which optionally contains the results of the operation.
|
||||
|
||||
The <xref:System.ComponentModel.BackgroundWorker> component is available from the **Toolbox**, in the **Components** tab. To add a <xref:System.ComponentModel.BackgroundWorker> to your form, drag the <xref:System.ComponentModel.BackgroundWorker> component onto your form. It appears in the component tray, and its properties appear in the **Properties** window.
|
||||
|
||||
To start your asynchronous operation, use the <xref:System.ComponentModel.BackgroundWorker.RunWorkerAsync%2A> method. <xref:System.ComponentModel.BackgroundWorker.RunWorkerAsync%2A> takes an optional `object` parameter, which can be used to pass arguments to your worker method. The <xref:System.ComponentModel.BackgroundWorker> class exposes the <xref:System.ComponentModel.BackgroundWorker.DoWork> event, to which your worker thread is attached through a <xref:System.ComponentModel.BackgroundWorker.DoWork> event handler.
|
||||
|
||||
The <xref:System.ComponentModel.BackgroundWorker.DoWork> event handler takes a <xref:System.ComponentModel.DoWorkEventArgs> parameter, which has an <xref:System.ComponentModel.DoWorkEventArgs.Argument%2A> property. This property receives the parameter from <xref:System.ComponentModel.BackgroundWorker.RunWorkerAsync%2A> and can be passed to your worker method, which will be called in the <xref:System.ComponentModel.BackgroundWorker.DoWork> event handler. The following example shows how to assign a result from a worker method called `ComputeFibonacci`. It is part of a larger example, which you can find at [How to: Implement a Form That Uses a Background Operation](how-to-implement-a-form-that-uses-a-background-operation.md).
|
||||
|
||||
[!code-cpp[System.ComponentModel.BackgroundWorker#5](~/samples/snippets/cpp/VS_Snippets_Winforms/System.ComponentModel.BackgroundWorker/CPP/fibonacciform.cpp#5)]
|
||||
[!code-csharp[System.ComponentModel.BackgroundWorker#5](~/samples/snippets/csharp/VS_Snippets_Winforms/System.ComponentModel.BackgroundWorker/CS/fibonacciform.cs#5)]
|
||||
[!code-vb[System.ComponentModel.BackgroundWorker#5](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.ComponentModel.BackgroundWorker/VB/fibonacciform.vb#5)]
|
||||
|
||||
For more information on using event handlers, see [Events](https://docs.microsoft.com/dotnet/standard/events/index).
|
||||
|
||||
> [!CAUTION]
|
||||
> When using multithreading of any sort, you potentially expose yourself to very serious and complex bugs. Consult the [Managed Threading Best Practices](https://docs.microsoft.com/dotnet/standard/threading/managed-threading-best-practices) before implementing any solution that uses multithreading.
|
||||
|
||||
For more information on using the <xref:System.ComponentModel.BackgroundWorker> class, see [How to: Run an Operation in the Background](how-to-run-an-operation-in-the-background.md).
|
||||
|
||||
## See also
|
||||
|
||||
- [Managed Threading](https://docs.microsoft.com/dotnet/standard/threading/index)
|
||||
- [Event-based Asynchronous Pattern Overview](https://docs.microsoft.com/dotnet/standard/asynchronous-programming-patterns/event-based-asynchronous-pattern-overview)
|
||||
- [How to: Implement a Form That Uses a Background Operation](how-to-implement-a-form-that-uses-a-background-operation.md)
|
||||
@@ -0,0 +1,49 @@
|
||||
---
|
||||
title: "BackgroundWorker Component"
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "BackgroundWorker component"
|
||||
- "background tasks"
|
||||
- "Asynchronous Pattern"
|
||||
- "forms [Windows Forms], multithreading"
|
||||
- "components [Windows Forms], asynchronous"
|
||||
- "forms [Windows Forms], background operations"
|
||||
- "threading [Windows Forms], background operations"
|
||||
- "background operations"
|
||||
ms.assetid: bef7b0ab-ce57-475a-a2d6-fb8a702a9417
|
||||
---
|
||||
# BackgroundWorker Component
|
||||
The `BackgroundWorker` component enables your form or control to run an operation asynchronously.
|
||||
|
||||
## In This Section
|
||||
[BackgroundWorker Component Overview](backgroundworker-component-overview.md)
|
||||
Describes the `BackgroundWorker` component, which gives you the ability to execute time-consuming operations asynchronously ("in the background"), on a thread different from your application's main UI thread.
|
||||
|
||||
[Walkthrough: Running an Operation in the Background](walkthrough-running-an-operation-in-the-background.md)
|
||||
Demonstrates how to use the `BackgroundWorker` component in the designer to run a time-consuming operation on a separate thread.
|
||||
|
||||
[How to: Run an Operation in the Background](how-to-run-an-operation-in-the-background.md)
|
||||
Demonstrates how to use the `BackgroundWorker` component to run a time-consuming operation on a separate thread.
|
||||
|
||||
[Walkthrough: Implementing a Form That Uses a Background Operation](walkthrough-implementing-a-form-that-uses-a-background-operation.md)
|
||||
Creates an application using the designer that does mathematical computations asynchronously.
|
||||
|
||||
[How to: Implement a Form That Uses a Background Operation](how-to-implement-a-form-that-uses-a-background-operation.md)
|
||||
Creates an application that does mathematical computations asynchronously.
|
||||
|
||||
[How to: Download a File in the Background](how-to-download-a-file-in-the-background.md)
|
||||
Demonstrates how to use the `BackgroundWorker` component to download a file on a separate thread.
|
||||
|
||||
## Reference
|
||||
<xref:System.ComponentModel.BackgroundWorker>
|
||||
Describes this class and has links to all its members.
|
||||
|
||||
<xref:System.ComponentModel.RunWorkerCompletedEventArgs>
|
||||
Describes the type that holds data for the <xref:System.ComponentModel.BackgroundWorker.RunWorkerCompleted> event.
|
||||
|
||||
<xref:System.ComponentModel.ProgressChangedEventArgs>
|
||||
Describes the type that holds data for the <xref:System.ComponentModel.BackgroundWorker.ProgressChanged> event.
|
||||
|
||||
## Related Sections
|
||||
[Event-based Asynchronous Pattern Overview](https://docs.microsoft.com/dotnet/standard/asynchronous-programming-patterns/event-based-asynchronous-pattern-overview)
|
||||
Describes how the asynchronous pattern makes available the advantages of multithreaded applications while hiding many of the complex issues inherent in multithreaded design.
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
---
|
||||
title: Basic Column, Row, and Cell Features in DataGridView Control
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "DataGridView control [Windows Forms], basic features"
|
||||
- "columns [Windows Forms], DataGridView control"
|
||||
- "data grids [Windows Forms], examples"
|
||||
- "DataGridView control [Windows Forms], examples"
|
||||
ms.assetid: 78085f26-d5d2-4b75-813e-e932b72fd06f
|
||||
---
|
||||
# Basic Column, Row, and Cell Features in the Windows Forms DataGridView Control
|
||||
Many basic behaviors of `DataGridView` cells, rows, and columns can be modified by setting single properties. The topics in this section describe several of the most commonly used of these features.
|
||||
|
||||
## In This Section
|
||||
[How to: Hide Columns in the Windows Forms DataGridView Control](how-to-hide-columns-in-the-windows-forms-datagridview-control.md)
|
||||
Describes how to prevent specific columns from appearing in the control.
|
||||
|
||||
[How to: Hide Column Headers in the Windows Forms DataGridView Control](how-to-hide-column-headers-in-the-windows-forms-datagridview-control.md)
|
||||
Describes how to prevent the column headers from appearing in the control.
|
||||
|
||||
[How to: Enable Column Reordering in the Windows Forms DataGridView Control](how-to-enable-column-reordering-in-the-windows-forms-datagridview-control.md)
|
||||
Describes how to enable users to rearrange columns in the control.
|
||||
|
||||
[How to: Freeze Columns in the Windows Forms DataGridView Control](how-to-freeze-columns-in-the-windows-forms-datagridview-control.md)
|
||||
Describes how prevent one or more adjacent columns from scrolling.
|
||||
|
||||
[How to: Make Columns Read-Only in the Windows Forms DataGridView Control](how-to-make-columns-read-only-in-the-windows-forms-datagridview-control.md)
|
||||
Describes how to prevent users from editing specific columns in the control.
|
||||
|
||||
[How to: Prevent Row Addition and Deletion in the Windows Forms DataGridView Control](prevent-row-addition-and-deletion-datagridview.md)
|
||||
Describes how to remove the row for new records at the bottom of the control to prevent users from adding rows. Also describes how to prevent users from deleting rows.
|
||||
|
||||
[How to: Get and Set the Current Cell in the Windows Forms DataGridView Control](get-and-set-the-current-cell-wf-datagridview-control.md)
|
||||
Describes how to access the cell that currently has focus in the control.
|
||||
|
||||
[How to: Display Images in Cells of the Windows Forms DataGridView Control](how-to-display-images-in-cells-of-the-windows-forms-datagridview-control.md)
|
||||
Describes how to create an image column that displays an icon in every cell.
|
||||
|
||||
## Reference
|
||||
<xref:System.Windows.Forms.DataGridView>
|
||||
Provides reference documentation for the control.
|
||||
|
||||
## Related Sections
|
||||
[Basic Formatting and Styling in the Windows Forms DataGridView Control](basic-formatting-and-styling-in-the-windows-forms-datagridview-control.md)
|
||||
Provides topics that describe how to modify the basic appearance of the control and the display formatting of cell data.
|
||||
|
||||
[Programming with Cells, Rows, and Columns in the Windows Forms DataGridView Control](programming-with-cells-rows-and-columns-in-the-datagrid.md)
|
||||
Provides topics that describe how to program with cell, row, and column objects.
|
||||
|
||||
## See also
|
||||
|
||||
- [DataGridView Control](datagridview-control-windows-forms.md)
|
||||
- [Column Types in the Windows Forms DataGridView Control](column-types-in-the-windows-forms-datagridview-control.md)
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
---
|
||||
title: Basic Formatting and Styling in DataGridView Control
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "DataGridView control [Windows Forms], formatting and styling"
|
||||
- "data grids [Windows Forms], formatting"
|
||||
ms.assetid: b9b90836-1f56-4aa9-8db8-edc78fe830e8
|
||||
---
|
||||
# Basic Formatting and Styling in the Windows Forms DataGridView Control
|
||||
The `DataGridView` control makes it easy to define the basic appearance of cells and the display formatting of cell values. You can define appearance and formatting styles for individual cells, for cells in specific columns and rows, or for all cells in the control by setting the properties of the `DataGridViewCellStyle` objects accessed through various `DataGridView` control properties. Additionally, you can modify these styles dynamically based on factors such as the cell value by handling the `CellFormatting` event.
|
||||
|
||||
## In This Section
|
||||
[How to: Change the Border and Gridline Styles in the Windows Forms DataGridView Control](change-the-border-and-gridline-styles-in-the-datagrid.md)
|
||||
Describes how to set `DataGridView` properties that define the appearance of the control border and the boundary lines between cells.
|
||||
|
||||
[Cell Styles in the Windows Forms DataGridView Control](cell-styles-in-the-windows-forms-datagridview-control.md)
|
||||
Describes the `DataGridViewCellStyle` class and how properties of that type interact to define how cells are displayed in the control.
|
||||
|
||||
[How to: Set Default Cell Styles for the Windows Forms DataGridView Control](how-to-set-default-cell-styles-for-the-windows-forms-datagridview-control.md)
|
||||
Describes how to use `DataGridViewCellStyle` properties to define the default appearance of cells in specific rows and columns and in the entire control.
|
||||
|
||||
[How to: Format Data in the Windows Forms DataGridView Control](how-to-format-data-in-the-windows-forms-datagridview-control.md)
|
||||
Describes how to format cell display values using `DataGridViewCellStyle` properties.
|
||||
|
||||
[How to: Set Font and Color Styles in the Windows Forms DataGridView Control](how-to-set-font-and-color-styles-in-the-windows-forms-datagridview-control.md)
|
||||
Describes how to use the `DefaultCellStyle` property to set basic display characteristics for all cells in the control.
|
||||
|
||||
[How to: Set Alternating Row Styles for the Windows Forms DataGridView Control](how-to-set-alternating-row-styles-for-the-windows-forms-datagridview-control.md)
|
||||
Describes how to create a ledger-like effect in the control using alternating rows that are displayed differently.
|
||||
|
||||
[How to: Use the Row Template to Customize Rows in the Windows Forms DataGridView Control](use-the-row-template-to-customize-rows-in-the-datagrid.md)
|
||||
Describes how to use the `RowTemplate` property to set row properties that will be used for all rows in the control.
|
||||
|
||||
## Reference
|
||||
<xref:System.Windows.Forms.DataGridView>
|
||||
Provides reference documentation for the <xref:System.Windows.Forms.DataGridView> control.
|
||||
|
||||
<xref:System.Windows.Forms.DataGridViewCellStyle>
|
||||
Provides reference documentation for the <xref:System.Windows.Forms.DataGridViewCellStyle> class.
|
||||
|
||||
<xref:System.Windows.Forms.DataGridView.CellFormatting>
|
||||
Provides reference documentation for the <xref:System.Windows.Forms.DataGridView.CellFormatting> event.
|
||||
|
||||
<xref:System.Windows.Forms.DataGridView.RowTemplate%2A>
|
||||
Provides reference documentation for the <xref:System.Windows.Forms.DataGridView.RowTemplate%2A> property.
|
||||
|
||||
## Related Sections
|
||||
[Customizing the Windows Forms DataGridView Control](customizing-the-windows-forms-datagridview-control.md)
|
||||
Provides topics that describe custom painting <xref:System.Windows.Forms.DataGridView> cells and rows, and creating derived cell, column, and row types.
|
||||
|
||||
[Basic Column, Row, and Cell Features in the Windows Forms DataGridView Control](basic-column-row-and-cell-features-wf-datagridview-control.md)
|
||||
Provides topics that describe commonly used cell, row, and column properties.
|
||||
|
||||
## See also
|
||||
|
||||
- [DataGridView Control](datagridview-control-windows-forms.md)
|
||||
+147
@@ -0,0 +1,147 @@
|
||||
---
|
||||
title: Best Practices for Scaling DataGridView Control
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "DataGridView control [Windows Forms], row sharing"
|
||||
- "data grids [Windows Forms], best practices"
|
||||
- "DataGridView control [Windows Forms], shared rows"
|
||||
- "DataGridView control [Windows Forms], best practices"
|
||||
- "best practices [Windows Forms], dataGridView control"
|
||||
- "DataGridView control [Windows Forms], scaling"
|
||||
ms.assetid: 8321a8a6-6340-4fd1-b475-fa090b905aaf
|
||||
---
|
||||
# Best Practices for Scaling the Windows Forms DataGridView Control
|
||||
|
||||
The <xref:System.Windows.Forms.DataGridView> control is designed to provide maximum scalability. If you need to display large amounts of data, you should follow the guidelines described in this topic to avoid consuming large amounts of memory or degrading the responsiveness of the user interface (UI). This topic discusses the following issues:
|
||||
|
||||
- Using cell styles efficiently
|
||||
|
||||
- Using shortcut menus efficiently
|
||||
|
||||
- Using automatic resizing efficiently
|
||||
|
||||
- Using the selected cells, rows, and columns collections efficiently
|
||||
|
||||
- Using shared rows
|
||||
|
||||
- Preventing rows from becoming unshared
|
||||
|
||||
If you have special performance needs, you can implement virtual mode and provide your own data management operations. For more information, see [Data Display Modes in the Windows Forms DataGridView Control](data-display-modes-in-the-windows-forms-datagridview-control.md).
|
||||
|
||||
## Using Cell Styles Efficiently
|
||||
|
||||
Each cell, row, and column can have its own style information. Style information is stored in <xref:System.Windows.Forms.DataGridViewCellStyle> objects. Creating cell style objects for many individual <xref:System.Windows.Forms.DataGridView> elements can be inefficient, especially when working with large amounts of data. To avoid a performance impact, use the following guidelines:
|
||||
|
||||
- Avoid setting cell style properties for individual <xref:System.Windows.Forms.DataGridViewCell> or <xref:System.Windows.Forms.DataGridViewRow> objects. This includes the row object specified by the <xref:System.Windows.Forms.DataGridView.RowTemplate%2A> property. Each new row that is cloned from the row template will receive its own copy of the template's cell style object. For maximum scalability, set cell style properties at the <xref:System.Windows.Forms.DataGridView> level. For example, set the <xref:System.Windows.Forms.DataGridView.DefaultCellStyle%2A?displayProperty=nameWithType> property rather than the <xref:System.Windows.Forms.DataGridViewCell.Style%2A?displayProperty=nameWithType> property.
|
||||
|
||||
- If some cells require formatting other than default formatting, use the same <xref:System.Windows.Forms.DataGridViewCellStyle> instance across groups of cells, rows, or columns. Avoid directly setting properties of type <xref:System.Windows.Forms.DataGridViewCellStyle> on individual cells, rows, and columns. For an example of cell style sharing, see [How to: Set Default Cell Styles for the Windows Forms DataGridView Control](how-to-set-default-cell-styles-for-the-windows-forms-datagridview-control.md). You can also avoid a performance penalty when setting cell styles individually by handling the <xref:System.Windows.Forms.DataGridView.CellFormatting> event handler. For an example, see [How to: Customize Data Formatting in the Windows Forms DataGridView Control](how-to-customize-data-formatting-in-the-windows-forms-datagridview-control.md).
|
||||
|
||||
- When determining a cell's style, use the <xref:System.Windows.Forms.DataGridViewCell.InheritedStyle%2A?displayProperty=nameWithType> property rather than the <xref:System.Windows.Forms.DataGridViewCell.Style%2A?displayProperty=nameWithType> property. Accessing the <xref:System.Windows.Forms.DataGridViewCell.Style%2A> property creates a new instance of the <xref:System.Windows.Forms.DataGridViewCellStyle> class if the property has not already been used. Additionally, this object might not contain the complete style information for the cell if some styles are inherited from the row, column, or control. For more information about cell style inheritance, see [Cell Styles in the Windows Forms DataGridView Control](cell-styles-in-the-windows-forms-datagridview-control.md).
|
||||
|
||||
## Using Shortcut Menus Efficiently
|
||||
|
||||
Each cell, row, and column can have its own shortcut menu. Shortcut menus in the <xref:System.Windows.Forms.DataGridView> control are represented by <xref:System.Windows.Forms.ContextMenuStrip> controls. Just as with cell style objects, creating shortcut menus for many individual <xref:System.Windows.Forms.DataGridView> elements will negatively impact performance. To avoid this penalty, use the following guidelines:
|
||||
|
||||
- Avoid creating shortcut menus for individual cells and rows. This includes the row template, which is cloned along with its shortcut menu when new rows are added to the control. For maximum scalability, use only the control's <xref:System.Windows.Forms.Control.ContextMenuStrip%2A> property to specify a single shortcut menu for the entire control.
|
||||
|
||||
- If you require multiple shortcut menus for multiple rows or cells, handle the <xref:System.Windows.Forms.DataGridView.CellContextMenuStripNeeded> or <xref:System.Windows.Forms.DataGridView.RowContextMenuStripNeeded> events. These events let you manage the shortcut menu objects yourself, allowing you to tune performance.
|
||||
|
||||
## Using Automatic Resizing Efficiently
|
||||
|
||||
Rows, columns, and headers can be automatically resized as cell content changes so that the entire contents of cells are displayed without clipping. Changing sizing modes can also resize rows, columns, and headers. To determine the correct size, the <xref:System.Windows.Forms.DataGridView> control must examine the value of each cell that it must accommodate. When working with large data sets, this analysis can negatively impact the performance of the control when automatic resizing occurs. To avoid performance penalties, use the following guidelines:
|
||||
|
||||
- Avoid using automatic sizing on a <xref:System.Windows.Forms.DataGridView> control with a large set of rows. If you do use automatic sizing, only resize based on the displayed rows. Use only the displayed rows in virtual mode as well.
|
||||
|
||||
- For rows and columns, use the `DisplayedCells` or `DisplayedCellsExceptHeaders` field of the <xref:System.Windows.Forms.DataGridViewAutoSizeRowsMode>, <xref:System.Windows.Forms.DataGridViewAutoSizeColumnsMode>, and <xref:System.Windows.Forms.DataGridViewAutoSizeColumnMode> enumerations.
|
||||
|
||||
- For row headers, use the <xref:System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders> or <xref:System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.AutoSizeToFirstHeader> field of the <xref:System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode> enumeration.
|
||||
|
||||
- For maximum scalability, turn off automatic sizing and use programmatic resizing.
|
||||
|
||||
For more information, see [Sizing Options in the Windows Forms DataGridView Control](sizing-options-in-the-windows-forms-datagridview-control.md).
|
||||
|
||||
## Using the Selected Cells, Rows, and Columns Collections Efficiently
|
||||
|
||||
The <xref:System.Windows.Forms.DataGridView.SelectedCells%2A> collection does not perform efficiently with large selections. The <xref:System.Windows.Forms.DataGridView.SelectedRows%2A> and <xref:System.Windows.Forms.DataGridView.SelectedColumns%2A> collections can also be inefficient, although to a lesser degree because there are many fewer rows than cells in a typical <xref:System.Windows.Forms.DataGridView> control, and many fewer columns than rows. To avoid performance penalties when working with these collections, use the following guidelines:
|
||||
|
||||
- To determine whether all the cells in the <xref:System.Windows.Forms.DataGridView> have been selected before you access the contents of the <xref:System.Windows.Forms.DataGridView.SelectedCells%2A> collection, check the return value of the <xref:System.Windows.Forms.DataGridView.AreAllCellsSelected%2A> method. Note, however, that this method can cause rows to become unshared. For more information, see the next section.
|
||||
|
||||
- Avoid using the <xref:System.Collections.ICollection.Count%2A> property of the <xref:System.Windows.Forms.DataGridViewSelectedCellCollection?displayProperty=nameWithType> to determine the number of selected cells. Instead, use the <xref:System.Windows.Forms.DataGridView.GetCellCount%2A?displayProperty=nameWithType> method and pass in the <xref:System.Windows.Forms.DataGridViewElementStates.Selected?displayProperty=nameWithType> value. Similarly, use the <xref:System.Windows.Forms.DataGridViewRowCollection.GetRowCount%2A?displayProperty=nameWithType> and <xref:System.Windows.Forms.DataGridViewColumnCollection.GetColumnCount%2A?displayProperty=nameWithType> methods to determine the number of selected elements, rather than accessing the selected row and column collections.
|
||||
|
||||
- Avoid cell-based selection modes. Instead, set the <xref:System.Windows.Forms.DataGridView.SelectionMode%2A?displayProperty=nameWithType> property to <xref:System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect?displayProperty=nameWithType> or <xref:System.Windows.Forms.DataGridViewSelectionMode.FullColumnSelect?displayProperty=nameWithType>.
|
||||
|
||||
## Using Shared Rows
|
||||
|
||||
Efficient memory use is achieved in the <xref:System.Windows.Forms.DataGridView> control through shared rows. Rows will share as much information about their appearance and behavior as possible by sharing instances of the <xref:System.Windows.Forms.DataGridViewRow> class.
|
||||
|
||||
While sharing row instances saves memory, rows can easily become unshared. For example, whenever a user interacts directly with a cell, its row becomes unshared. Because this cannot be avoided, the guidelines in this topic are useful only when working with very large amounts of data and only when users will interact with a relatively small part of the data each time your program is run.
|
||||
|
||||
A row cannot be shared in an unbound <xref:System.Windows.Forms.DataGridView> control if any of its cells contain values. When the <xref:System.Windows.Forms.DataGridView> control is bound to an external data source or when you implement virtual mode and provide your own data source, the cell values are stored outside the control rather than in cell objects, allowing the rows to be shared.
|
||||
|
||||
A row object can only be shared if the state of all its cells can be determined from the state of the row and the states of the columns containing the cells. If you change the state of a cell so that it can no longer be deduced from the state of its row and column, the row cannot be shared.
|
||||
|
||||
For example, a row cannot be shared in any of the following situations:
|
||||
|
||||
- The row contains a single selected cell that is not in a selected column.
|
||||
|
||||
- The row contains a cell with its <xref:System.Windows.Forms.DataGridViewCell.ToolTipText%2A> or <xref:System.Windows.Forms.DataGridViewCell.ContextMenuStrip%2A> properties set.
|
||||
|
||||
- The row contains a <xref:System.Windows.Forms.DataGridViewComboBoxCell> with its <xref:System.Windows.Forms.DataGridViewComboBoxCell.Items%2A> property set.
|
||||
|
||||
In bound mode or virtual mode, you can provide ToolTips and shortcut menus for individual cells by handling the <xref:System.Windows.Forms.DataGridView.CellToolTipTextNeeded> and <xref:System.Windows.Forms.DataGridView.CellContextMenuStripNeeded> events.
|
||||
|
||||
The <xref:System.Windows.Forms.DataGridView> control will automatically attempt to use shared rows whenever rows are added to the <xref:System.Windows.Forms.DataGridViewRowCollection>. Use the following guidelines to ensure that rows are shared:
|
||||
|
||||
- Avoid calling the `Add(Object[])` overload of the <xref:System.Windows.Forms.DataGridViewRowCollection.Add%2A> method and the `Insert(Object[])` overload of the <xref:System.Windows.Forms.DataGridViewRowCollection.Insert%2A> method of the <xref:System.Windows.Forms.DataGridView.Rows%2A?displayProperty=nameWithType> collection. These overloads automatically create unshared rows.
|
||||
|
||||
- Be sure that the row specified in the <xref:System.Windows.Forms.DataGridView.RowTemplate%2A?displayProperty=nameWithType> property can be shared in the following cases:
|
||||
|
||||
- When calling the `Add()` or `Add(Int32)` overloads of the <xref:System.Windows.Forms.DataGridViewRowCollection.Add%2A> method or the `Insert(Int32,Int32)` overload of the <xref:System.Windows.Forms.DataGridViewRowCollection.Insert%2A> method of the <xref:System.Windows.Forms.DataGridView.Rows%2A?displayProperty=nameWithType> collection.
|
||||
|
||||
- When increasing the value of the <xref:System.Windows.Forms.DataGridView.RowCount%2A?displayProperty=nameWithType> property.
|
||||
|
||||
- When setting the <xref:System.Windows.Forms.DataGridView.DataSource%2A?displayProperty=nameWithType> property.
|
||||
|
||||
- Be sure that the row indicated by the `indexSource` parameter can be shared when calling the <xref:System.Windows.Forms.DataGridViewRowCollection.AddCopy%2A>, <xref:System.Windows.Forms.DataGridViewRowCollection.AddCopies%2A>, <xref:System.Windows.Forms.DataGridViewRowCollection.InsertCopy%2A>, and <xref:System.Windows.Forms.DataGridViewRowCollection.InsertCopies%2A> methods of the <xref:System.Windows.Forms.DataGridView.Rows%2A?displayProperty=nameWithType> collection.
|
||||
|
||||
- Be sure that the specified row or rows can be shared when calling the `Add(DataGridViewRow)` overload of the <xref:System.Windows.Forms.DataGridViewRowCollection.Add%2A> method, the <xref:System.Windows.Forms.DataGridViewRowCollection.AddRange%2A> method, the `Insert(Int32,DataGridViewRow)` overload of the <xref:System.Windows.Forms.DataGridViewRowCollection.Insert%2A> method, and the <xref:System.Windows.Forms.DataGridViewRowCollection.InsertRange%2A> method of the <xref:System.Windows.Forms.DataGridView.Rows%2A?displayProperty=nameWithType> collection.
|
||||
|
||||
To determine whether a row is shared, use the <xref:System.Windows.Forms.DataGridViewRowCollection.SharedRow%2A?displayProperty=nameWithType> method to retrieve the row object, and then check the object's <xref:System.Windows.Forms.DataGridViewBand.Index%2A> property. Shared rows always have an <xref:System.Windows.Forms.DataGridViewBand.Index%2A> property value of –1.
|
||||
|
||||
## Preventing Rows from Becoming Unshared
|
||||
|
||||
Shared rows can become unshared as a result of code or user action. To avoid a performance impact, you should avoid causing rows to become unshared. During application development, you can handle the <xref:System.Windows.Forms.DataGridView.RowUnshared> event to determine when rows become unshared. This is useful when debugging row-sharing problems.
|
||||
|
||||
To prevent rows from becoming unshared, use the following guidelines:
|
||||
|
||||
- Avoid indexing the <xref:System.Windows.Forms.DataGridView.Rows%2A> collection or iterating through it with a `foreach` loop. You will not typically need to access rows directly. <xref:System.Windows.Forms.DataGridView> methods that operate on rows take row index arguments rather than row instances. Additionally, handlers for row-related events receive event argument objects with row properties that you can use to manipulate rows without causing them to become unshared.
|
||||
|
||||
- If you need to access a row object, use the <xref:System.Windows.Forms.DataGridViewRowCollection.SharedRow%2A?displayProperty=nameWithType> method and pass in the row's actual index. Note, however, that modifying a shared row object retrieved through this method will modify all the rows that share this object. The row for new records is not shared with other rows, however, so it will not be affected when you modify any other row. Note also that different rows represented by a shared row may have different shortcut menus. To retrieve the correct shortcut menu from a shared row instance, use the <xref:System.Windows.Forms.DataGridViewRow.GetContextMenuStrip%2A> method and pass in the row's actual index. If you access the shared row's <xref:System.Windows.Forms.DataGridViewRow.ContextMenuStrip%2A> property instead, it will use the shared row index of -1 and will not retrieve the correct shortcut menu.
|
||||
|
||||
- Avoid indexing the <xref:System.Windows.Forms.DataGridViewRow.Cells%2A?displayProperty=nameWithType> collection. Accessing a cell directly will cause its parent row to become unshared, instantiating a new <xref:System.Windows.Forms.DataGridViewRow>. Handlers for cell-related events receive event argument objects with cell properties that you can use to manipulate cells without causing rows to become unshared. You can also use the <xref:System.Windows.Forms.DataGridView.CurrentCellAddress%2A> property to retrieve the row and column indexes of the current cell without accessing the cell directly.
|
||||
|
||||
- Avoid cell-based selection modes. These modes cause rows to become unshared. Instead, set the <xref:System.Windows.Forms.DataGridView.SelectionMode%2A?displayProperty=nameWithType> property to <xref:System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect?displayProperty=nameWithType> or <xref:System.Windows.Forms.DataGridViewSelectionMode.FullColumnSelect?displayProperty=nameWithType>.
|
||||
|
||||
- Do not handle the <xref:System.Windows.Forms.DataGridViewRowCollection.CollectionChanged?displayProperty=nameWithType> or <xref:System.Windows.Forms.DataGridView.RowStateChanged?displayProperty=nameWithType> events. These events cause rows to become unshared. Also, do not call the <xref:System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged%2A?displayProperty=nameWithType> or <xref:System.Windows.Forms.DataGridView.OnRowStateChanged%2A?displayProperty=nameWithType> methods, which raise these events.
|
||||
|
||||
- Do not access the <xref:System.Windows.Forms.DataGridView.SelectedCells%2A?displayProperty=nameWithType> collection when the <xref:System.Windows.Forms.DataGridView.SelectionMode%2A?displayProperty=nameWithType> property value is <xref:System.Windows.Forms.DataGridViewSelectionMode.FullColumnSelect>, <xref:System.Windows.Forms.DataGridViewSelectionMode.ColumnHeaderSelect>, <xref:System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect>, or <xref:System.Windows.Forms.DataGridViewSelectionMode.RowHeaderSelect>. This causes all selected rows to become unshared.
|
||||
|
||||
- Do not call the <xref:System.Windows.Forms.DataGridView.AreAllCellsSelected%2A?displayProperty=nameWithType> method. This method can cause rows to become unshared.
|
||||
|
||||
- Do not call the <xref:System.Windows.Forms.DataGridView.SelectAll%2A?displayProperty=nameWithType> method when the <xref:System.Windows.Forms.DataGridView.SelectionMode%2A?displayProperty=nameWithType> property value is <xref:System.Windows.Forms.DataGridViewSelectionMode.CellSelect>. This causes all rows to become unshared.
|
||||
|
||||
- Do not set the <xref:System.Windows.Forms.DataGridViewCell.ReadOnly%2A> or <xref:System.Windows.Forms.DataGridViewCell.Selected%2A> property of a cell to `false` when the corresponding property in its column is set to `true`. This causes all rows to become unshared.
|
||||
|
||||
- Do not access the <xref:System.Windows.Forms.DataGridViewRowCollection.List%2A?displayProperty=nameWithType> property. This causes all rows to become unshared.
|
||||
|
||||
- Do not call the `Sort(IComparer)` overload of the <xref:System.Windows.Forms.DataGridView.Sort%2A> method. Sorting with a custom comparer causes all rows to become unshared.
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridView>
|
||||
- [Performance Tuning in the Windows Forms DataGridView Control](performance-tuning-in-the-windows-forms-datagridview-control.md)
|
||||
- [Virtual Mode in the Windows Forms DataGridView Control](virtual-mode-in-the-windows-forms-datagridview-control.md)
|
||||
- [Data Display Modes in the Windows Forms DataGridView Control](data-display-modes-in-the-windows-forms-datagridview-control.md)
|
||||
- [Cell Styles in the Windows Forms DataGridView Control](cell-styles-in-the-windows-forms-datagridview-control.md)
|
||||
- [How to: Set Default Cell Styles for the Windows Forms DataGridView Control](how-to-set-default-cell-styles-for-the-windows-forms-datagridview-control.md)
|
||||
- [Sizing Options in the Windows Forms DataGridView Control](sizing-options-in-the-windows-forms-datagridview-control.md)
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
---
|
||||
title: "Best Practices for the TableLayoutPanel Control"
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "layout [Windows Forms]"
|
||||
- "TableLayoutPanel control [Windows Forms], best practices"
|
||||
- "forms [Windows Forms], best practices"
|
||||
- "AutoSize property [Windows Forms], tableLayoutPanel control"
|
||||
- "controls [Windows Forms], sizing"
|
||||
- "TableLayoutPanel control [Windows Forms], AutoSize behavior"
|
||||
- "layout [Windows Forms], AutoSize"
|
||||
- "layout [Windows Forms], best practices"
|
||||
- "best practices [Windows Forms], tableLayoutPanel control"
|
||||
- "sizing [Windows Forms], automatic"
|
||||
- "automatic sizing"
|
||||
ms.assetid: b6706efb-d7a4-45ec-8cf4-08fa993e3afb
|
||||
---
|
||||
# Best Practices for the TableLayoutPanel Control
|
||||
The <xref:System.Windows.Forms.TableLayoutPanel> control provides powerful layout features that you should consider carefully before using on your Windows Forms.
|
||||
|
||||
## Recommendations
|
||||
The following recommendations will help you use the <xref:System.Windows.Forms.TableLayoutPanel> control to its best advantage.
|
||||
|
||||
### Targeted Use
|
||||
Use the <xref:System.Windows.Forms.TableLayoutPanel> control sparingly. You should not use it in all situations that require a resizable layout. The following list describes layouts that benefit most from the use of the <xref:System.Windows.Forms.TableLayoutPanel> control:
|
||||
|
||||
- Layouts in which there are multiple parts of the form that resize proportionally to each other.
|
||||
|
||||
- Layouts that will be modified or generated dynamically at run time, such as data entry forms that have user-customizable fields added or subtracted based on preferences.
|
||||
|
||||
- Layouts that should remain at an overall fixed size. For example, you may have a dialog box that should remain smaller than 800 x 600, but you need to support localized strings.
|
||||
|
||||
The following list describes layouts that do not benefit greatly from using the <xref:System.Windows.Forms.TableLayoutPanel> control:
|
||||
|
||||
- Simple data entry forms with a single column of labels and a single column of text-entry areas.
|
||||
|
||||
- Forms with a single large display area that should fill all the available space when a resize occurs. An example of this is a form that displays a single <xref:System.Windows.Forms.PropertyGrid> control. In this case, use anchoring, because nothing else should expand when the form is resized.
|
||||
|
||||
Choose carefully which controls need to be in a <xref:System.Windows.Forms.TableLayoutPanel> control. If you have room for your text to grow by 30% using anchoring, consider using the <xref:System.Windows.Forms.Control.Anchor%2A> property only. If you can estimate the space required by your layout, use of <xref:System.Windows.Forms.Control.Dock%2A> and <xref:System.Windows.Forms.Control.Anchor%2A> is easier than estimating the details of remaining space and <xref:System.Windows.Forms.Control.AutoSize%2A> behavior.
|
||||
|
||||
In general, when designing your layout with the <xref:System.Windows.Forms.TableLayoutPanel> control, keep the design as simple as possible.
|
||||
|
||||
### Use the Document Outline Window
|
||||
The Document Outline window gives you a tree view of your layout, which you can use to manipulate the z-order and parent-child relationships of your controls. From the **View menu**, select **Other Windows**, then select **Document Outline**.
|
||||
|
||||
### Avoid Nesting
|
||||
Avoid nesting other <xref:System.Windows.Forms.TableLayoutPanel> controls within a <xref:System.Windows.Forms.TableLayoutPanel> control. Debugging nested layouts can be difficult.
|
||||
|
||||
### Avoid Visual Inheritance
|
||||
The <xref:System.Windows.Forms.TableLayoutPanel> control does not support visual inheritance in the Windows Forms Designer in Visual Studio. A <xref:System.Windows.Forms.TableLayoutPanel> control in a derived class appears as "locked" at design time.
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.TableLayoutPanel>
|
||||
- <xref:System.Windows.Forms.FlowLayoutPanel>
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
---
|
||||
title: Bind Data to DataGridView Control Using the Designer
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "Windows Forms controls, binding to a data source"
|
||||
- "data sources [Windows Forms], binding to Windows Forms controls"
|
||||
- "DataGridView control [Windows Forms], data binding"
|
||||
ms.assetid: f4f46009-cec2-441b-8668-6b5af057558b
|
||||
---
|
||||
# How to: Bind Data to the Windows Forms DataGridView Control Using the Designer
|
||||
You can use the designer to connect a <xref:System.Windows.Forms.DataGridView> control to data sources of several different varieties, including databases, business objects, or Web services. When you bind the control to a data source using the designer, the control is automatically bound to a <xref:System.Windows.Forms.BindingSource> component that represents the data source. Additionally, columns are automatically generated in the control to match the schema information provided by the data source.
|
||||
|
||||
After columns have been generated, you can modify them to meet your needs. For example, you can remove or hide columns you are not interested in displaying, you can rearrange the columns, or you can modify the column types. For more information about modifying columns, see the topics listed in the See Also section.
|
||||
|
||||
You can also bind multiple <xref:System.Windows.Forms.DataGridView> controls to related tables to create master/detail relationships. In this configuration, one control displays a parent table and another control displays only those rows from a child table that are related to the current row in the parent table. For more information, see [How to: Display Related Data in a Windows Forms Application](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2013/57tx3hhe(v=vs.120)).
|
||||
|
||||
The following procedure requires a **Windows Application** project with a form that contains a <xref:System.Windows.Forms.DataGridView> control or two controls for a master/detail relationship. For information about starting such a project, see [How to: Create a Windows Forms application project](/visualstudio/ide/step-1-create-a-windows-forms-application-project) and [How to: Add Controls to Windows Forms](how-to-add-controls-to-windows-forms.md).
|
||||
|
||||
## To bind the control to a data source
|
||||
|
||||
1. Click the designer actions glyph () on the upper-right corner of the <xref:System.Windows.Forms.DataGridView> control.
|
||||
|
||||
2. Click the drop-down arrow for the **Choose Data Source** option.
|
||||
|
||||
3. If your project does not already have a data source, click **Add Project Data Source** and follow the steps indicated by the wizard.
|
||||
|
||||
For more information, see [Data Source Configuration Wizard](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2013/w4dd7z6t(v=vs.120)). Your new data source will appear in the **Choose Data Source** drop-down window. If your new data source contains only one member, such as a single database table, the control will automatically bind to that member. Otherwise, continue to the next step.
|
||||
|
||||
4. Expand the **Other Data Sources** and **Project Data Sources** nodes if they are not already expanded, and then select the data source to bind the control to.
|
||||
|
||||
5. If your data source contains more than one member, such as if you have created a <xref:System.Data.DataSet?displayProperty=nameWithType> that contains multiple tables, expand the data source, and then select the specific member to bind to.
|
||||
|
||||
6. To create a master/detail relationship, in the **Choose Data Source** drop-down window for a second <xref:System.Windows.Forms.DataGridView> control, expand the <xref:System.Windows.Forms.BindingSource> created for the parent table, and then select the related child table from the list shown.
|
||||
|
||||
> [!NOTE]
|
||||
> If your project already has a data source, you can also use the **Data Sources** window to create a data form. For more information, see [Data Sources Window](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2013/6ckyxa83(v=vs.120)).
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridView>
|
||||
- <xref:System.Windows.Forms.BindingSource>
|
||||
- <xref:System.Windows.Forms.DataGridView.DataMember%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridView.DataSource%2A?displayProperty=nameWithType>
|
||||
- [How to: Connect to Data in a Database](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2013/fxk9yw1t(v=vs.120))
|
||||
- [How to: Add and Remove Columns in the Windows Forms DataGridView Control Using the Designer](add-and-remove-columns-in-the-datagrid-using-the-designer.md)
|
||||
- [How to: Change the Order of Columns in the Windows Forms DataGridView Control Using the Designer](change-the-order-of-columns-in-the-datagrid-using-the-designer.md)
|
||||
- [How to: Change the Type of a Windows Forms DataGridView Column Using the Designer](change-the-type-of-a-wf-datagridview-column-using-the-designer.md)
|
||||
- [How to: Freeze Columns in the Windows Forms DataGridView Control Using the Designer](freeze-columns-in-the-datagrid-using-the-designer.md)
|
||||
- [How to: Hide Columns in the Windows Forms DataGridView Control Using the Designer](hide-columns-in-the-datagrid-using-the-designer.md)
|
||||
- [How to: Make Columns Read-Only in the Windows Forms DataGridView Control Using the Designer](make-columns-read-only-in-the-datagrid-using-the-designer.md)
|
||||
- [How to: Create a Windows Forms application project](/visualstudio/ide/step-1-create-a-windows-forms-application-project)
|
||||
- [How to: Add Controls to Windows Forms](how-to-add-controls-to-windows-forms.md)
|
||||
- [Data Sources Window](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2013/6ckyxa83(v=vs.120))
|
||||
- [How to: Display Related Data in a Windows Forms Application](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2013/57tx3hhe(v=vs.120))
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
---
|
||||
title: Bind Controls with the BindingSource Component Using the Designer
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "controls [Windows Forms], binding"
|
||||
- "BindingSource component [Windows Forms], binding controls"
|
||||
- "data binding [Windows Forms], BindingSource component"
|
||||
ms.assetid: 391ae170-de5c-40f8-8233-91cb2ee4683a
|
||||
---
|
||||
# How to: Bind Windows Forms Controls with the BindingSource Component Using the Designer
|
||||
After you have added controls to your form and determined the user interface for your application, you can bind the controls to a data source, so that, at run time, users can alter and save data related to the application.
|
||||
|
||||
Binding a control or series of controls in Windows Forms is most easily accomplished using the <xref:System.Windows.Forms.BindingSource> control as a bridge between the controls on the form and the data source.
|
||||
|
||||
One or more controls on a form can be bound to data; in the following procedure, a <xref:System.Windows.Forms.TextBox> control is bound to a data source.
|
||||
|
||||
To complete the procedure, it is assumed that you will bind to a data source derived from a database. For more information on creating data sources from other stores of data, see [Add new data sources](/visualstudio/data-tools/add-new-data-sources).
|
||||
|
||||
## To bind a control at design time
|
||||
|
||||
1. Drag a <xref:System.Windows.Forms.TextBox> control on to the form.
|
||||
|
||||
2. In the **Properties** window:
|
||||
|
||||
1. Expand the **(DataBindings)** node.
|
||||
|
||||
2. Click the arrow next to the <xref:System.Windows.Forms.TextBox.Text%2A> property.
|
||||
|
||||
The **DataSource** UI type editor opens.
|
||||
|
||||
If a data source has previously been configured for the project or form, it will appear.
|
||||
|
||||
3. Click **Add Project Data Source** to connect to data and create a data source.
|
||||
|
||||
4. On the **Data Source Configuration Wizard** welcome page, click **Next**.
|
||||
|
||||
5. On the **Choose a Data Source Type** page, select **Database**.
|
||||
|
||||
6. On the **Choose Your Data Connection** page, select a data connection from the list of available connections. If your desired data connection is not available select **New Connection** to create a new data connection.
|
||||
|
||||
7. Select **Yes, save the connection** to save the connection string in the application configuration file.
|
||||
|
||||
8. Select the database objects to bring into your application. In this case, select a field in a table that you would like the <xref:System.Windows.Forms.TextBox> to display.
|
||||
|
||||
9. Replace the default dataset name if you want.
|
||||
|
||||
10. Click **Finish**.
|
||||
|
||||
11. In the **Properties** window, click the arrow next to the <xref:System.Windows.Forms.TextBox.Text%2A> property again. In the **DataSource** UI type editor, select the name of the field to bind the <xref:System.Windows.Forms.TextBox> to.
|
||||
|
||||
The **DataSource** UI type editor closes and the data set, <xref:System.Windows.Forms.BindingSource> and table adapter specific to that data connection are added to your form.
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.BindingSource>
|
||||
- <xref:System.Windows.Forms.BindingNavigator>
|
||||
- [Add new data sources](/visualstudio/data-tools/add-new-data-sources)
|
||||
- [Data Sources Window](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2013/6ckyxa83(v=vs.120))
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
---
|
||||
title: Bind DataGrid Control to a Data Source Using the Designer
|
||||
ms.date: "03/30/2017"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
- "cpp"
|
||||
helpviewer_keywords:
|
||||
- "datasets [Windows Forms], binding to DataGrid control"
|
||||
- "data binding [Windows Forms], DataGrid control"
|
||||
- "DataGrid control [Windows Forms], data binding"
|
||||
- "Windows Forms controls, data binding"
|
||||
- "bound controls [Windows Forms]"
|
||||
ms.assetid: 4e96e3d0-b1cc-4de1-8774-bc9970ec4554
|
||||
---
|
||||
# How to: Bind the Windows Forms DataGrid Control to a Data Source Using the Designer
|
||||
|
||||
> [!NOTE]
|
||||
> The <xref:System.Windows.Forms.DataGridView> control replaces and adds functionality to the <xref:System.Windows.Forms.DataGrid> control; however, the <xref:System.Windows.Forms.DataGrid> control is retained for both backward compatibility and future use, if you choose. For more information, see [Differences Between the Windows Forms DataGridView and DataGrid Controls](differences-between-the-windows-forms-datagridview-and-datagrid-controls.md).
|
||||
|
||||
The Windows Forms <xref:System.Windows.Forms.DataGrid> control is specifically designed to display information from a data source. You bind the control at design time by setting the <xref:System.Windows.Forms.DataGrid.DataSource%2A> and <xref:System.Windows.Forms.DataGrid.DataMember%2A> properties, or at run time by calling the <xref:System.Windows.Forms.DataGrid.SetDataBinding%2A> method. Although you can display data from a variety of data sources, the most typical sources are datasets and data views.
|
||||
|
||||
If the data source is available at design time—for example, if the form contains an instance of a dataset or a data view—you can bind the grid to the data source at design time. You can then preview what the data will look like in the grid.
|
||||
|
||||
You can also bind the grid programmatically, at run time. This is useful when you want to set a data source based on information you get at run time. For example, the application might let the user specify the name of a table to view. It is also necessary in situations where the data source does not exist at design time. This includes data sources such as arrays, collections, untyped datasets, and data readers.
|
||||
|
||||
The following procedure requires a **Windows Application** project with a form containing a <xref:System.Windows.Forms.DataGrid> control. For information about setting up such a project, see [How to: Create a Windows Forms application project](/visualstudio/ide/step-1-create-a-windows-forms-application-project) and [How to: Add Controls to Windows Forms](how-to-add-controls-to-windows-forms.md). In Visual Studio 2005, the <xref:System.Windows.Forms.DataGrid> control is not in the **Toolbox** by default. For information about adding it, see [How to: Add Items to the Toolbox](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2010/ms165355(v=vs.100)). Additionally in Visual Studio 2005, you can use the **Data Sources** window for design-time data binding. For more information see [Bind controls to data in Visual Studio](/visualstudio/data-tools/bind-controls-to-data-in-visual-studio).
|
||||
|
||||
## To data-bind the DataGrid control to a single table in the designer
|
||||
|
||||
1. Set the control's <xref:System.Windows.Forms.DataGrid.DataSource%2A> property to the object containing the data items you want to bind to.
|
||||
|
||||
2. If the data source is a dataset, set the <xref:System.Windows.Forms.DataGrid.DataMember%2A> property to the name of the table to bind to.
|
||||
|
||||
3. If the data source is a dataset or a data view based on a dataset table, add code to the form to fill the dataset.
|
||||
|
||||
The exact code you use depends on where the dataset is getting data. If the dataset is being populated directly from a database, you typically call the `Fill` method of a data adapter, as in the following code example, which populates a dataset called `DsCategories1`:
|
||||
|
||||
```vb
|
||||
sqlDataAdapter1.Fill(DsCategories1)
|
||||
```
|
||||
|
||||
```csharp
|
||||
sqlDataAdapter1.Fill(DsCategories1);
|
||||
```
|
||||
|
||||
```cpp
|
||||
sqlDataAdapter1->Fill(dsCategories1);
|
||||
```
|
||||
|
||||
4. (Optional) Add the appropriate table styles and column styles to the grid.
|
||||
|
||||
If there are no table styles, you will see the table, but with minimal formatting and with all columns visible.
|
||||
|
||||
## To data-bind the DataGrid control to multiple tables in a dataset in the designer
|
||||
|
||||
1. Set the control's <xref:System.Windows.Forms.DataGrid.DataSource%2A> property to the object containing the data items you want to bind to.
|
||||
|
||||
2. If the dataset contains related tables (that is, if it contains a relation object), set the <xref:System.Windows.Forms.DataGrid.DataMember%2A> property to the name of the parent table.
|
||||
|
||||
3. Write code to fill the dataset.
|
||||
|
||||
## See also
|
||||
|
||||
- [DataGrid Control Overview](datagrid-control-overview-windows-forms.md)
|
||||
- [How to: Add Tables and Columns to the Windows Forms DataGrid Control](how-to-add-tables-and-columns-to-the-windows-forms-datagrid-control.md)
|
||||
- [DataGrid Control](datagrid-control-windows-forms.md)
|
||||
- [Windows Forms Data Binding](../windows-forms-data-binding.md)
|
||||
- [Accessing data in Visual Studio](/visualstudio/data-tools/accessing-data-in-visual-studio)
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
---
|
||||
title: "BindingNavigator Control Overview"
|
||||
ms.date: "03/30/2017"
|
||||
f1_keywords:
|
||||
- "DataNavigator"
|
||||
helpviewer_keywords:
|
||||
- "BindingNavigator control [Windows Forms], about BindingNavigator control"
|
||||
- "records [Windows Forms], navigating on a form"
|
||||
- "data [Windows Forms], navigating"
|
||||
- "data navigation"
|
||||
ms.assetid: 4423eede-f8d1-4d02-822f-5bf8432680d0
|
||||
---
|
||||
# BindingNavigator Control Overview (Windows Forms)
|
||||
You can use the <xref:System.Windows.Forms.BindingNavigator> control to create a standardized means for users to search and change data on a Windows Form. You frequently use <xref:System.Windows.Forms.BindingNavigator> with the <xref:System.Windows.Forms.BindingSource> component to enable users to move through data records on a form and interact with the records.
|
||||
|
||||
## How the BindingNavigator Works
|
||||
|
||||
The <xref:System.Windows.Forms.BindingNavigator> control is composed of a <xref:System.Windows.Forms.ToolStrip> with a series of <xref:System.Windows.Forms.ToolStripItem> objects for most of the common data-related actions: adding data, deleting data, and navigating through data. By default, the <xref:System.Windows.Forms.BindingNavigator> control contains these standard buttons. The following screenshot shows the <xref:System.Windows.Forms.BindingNavigator> control on a form:
|
||||
|
||||

|
||||
|
||||
The following table lists the controls and describes their functions.
|
||||
|
||||
|Control|Function|
|
||||
|-------------|--------------|
|
||||
|<xref:System.Windows.Forms.BindingNavigator.AddNewItem%2A> button|Inserts a new row into the underlying data source.|
|
||||
|<xref:System.Windows.Forms.BindingNavigator.DeleteItem%2A> button|Deletes the current row from the underlying data source.|
|
||||
|<xref:System.Windows.Forms.BindingNavigator.MoveFirstItem%2A> button|Moves to the first item in the underlying data source.|
|
||||
|<xref:System.Windows.Forms.BindingNavigator.MoveLastItem%2A> button|Moves to the last item in the underlying data source.|
|
||||
|<xref:System.Windows.Forms.BindingNavigator.MoveNextItem%2A> button|Moves to the next item in the underlying data source.|
|
||||
|<xref:System.Windows.Forms.BindingNavigator.MovePreviousItem%2A> button|Moves to the previous item in the underlying data source.|
|
||||
|<xref:System.Windows.Forms.BindingNavigator.PositionItem%2A> text box|Returns the current position within the underlying data source.|
|
||||
|<xref:System.Windows.Forms.BindingNavigator.CountItem%2A> text box|Returns the total number of items in the underlying data source.|
|
||||
|
||||
For each control in this collection, there is a corresponding member of the <xref:System.Windows.Forms.BindingSource> component that programmatically provides the same functionality. For example, the <xref:System.Windows.Forms.BindingNavigator.MoveFirstItem%2A> button corresponds to the <xref:System.Windows.Forms.BindingSource.MoveFirst%2A> method of the <xref:System.Windows.Forms.BindingSource> component, the <xref:System.Windows.Forms.BindingNavigator.DeleteItem%2A> button corresponds to the <xref:System.Windows.Forms.BindingSource.RemoveCurrent%2A> method, and so on.
|
||||
|
||||
If the default buttons are not suited to your application, or if you require additional buttons to support other types of functionality, you can supply your own <xref:System.Windows.Forms.ToolStrip> buttons. Also see [How to: Add Load, Save, and Cancel Buttons to the Windows Forms BindingNavigator Control](load-save-and-cancel-bindingnavigator.md).
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.BindingNavigator>
|
||||
- <xref:System.Windows.Forms.BindingSource>
|
||||
- [BindingNavigator Control](bindingnavigator-control-windows-forms.md)
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
---
|
||||
title: "BindingNavigator Control"
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "BindingNavigator control [Windows Forms]"
|
||||
- "data [Windows Forms], navigating"
|
||||
- "data navigation"
|
||||
ms.assetid: 18c1e2a5-9834-40d3-9b2e-2b545e4e769e
|
||||
---
|
||||
# BindingNavigator Control (Windows Forms)
|
||||
The `BindingNavigator` control is the navigation and manipulation user interface (UI) for controls that are bound to data. The `BindingNavigator` control enables users to navigate through and manipulate data on a Windows Form.
|
||||
|
||||
The topics in this section provide an overview of the `BindingNavigator` control and offer step-by-step instructions how to use the control navigate data and move through a <xref:System.Data.DataSet>.
|
||||
|
||||
## In This Section
|
||||
[BindingNavigator Control Overview](bindingnavigator-control-overview-windows-forms.md)
|
||||
Introduces the general concepts of the `BindingNavigator` control, which enables users to move through the items of a data source.
|
||||
|
||||
[How to: Navigate Data with the Windows Forms BindingNavigator Control](how-to-navigate-data-with-the-windows-forms-bindingnavigator-control.md)
|
||||
Provides steps to bind a `BindingNavigator` control to a data source.
|
||||
|
||||
[How to: Move Through a DataSet with the Windows Forms BindingNavigator Control](move-through-a-dataset-with-wf-bindingnavigator-control.md)
|
||||
Demonstrates using a `BindingNavigator` control to move through records in a <xref:System.Data.DataSet>.
|
||||
|
||||
Also see [How to: Add Load, Save, and Cancel Buttons to the Windows Forms BindingNavigator Control](load-save-and-cancel-bindingnavigator.md).
|
||||
|
||||
## Reference
|
||||
<xref:System.Windows.Forms.BindingNavigator>
|
||||
Provides reference documentation for the <xref:System.Windows.Forms.BindingNavigator> control.
|
||||
|
||||
<xref:System.Windows.Forms.BindingSource>
|
||||
Provides reference documentation for the <xref:System.Windows.Forms.BindingSource> control.
|
||||
|
||||
## Related Sections
|
||||
[Bind controls to data in Visual Studio](/visualstudio/data-tools/bind-controls-to-data-in-visual-studio)
|
||||
+174
@@ -0,0 +1,174 @@
|
||||
---
|
||||
title: "BindingSource Component Architecture"
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "BindingSource component [Windows Forms], architecture"
|
||||
- "Windows Forms, data binding"
|
||||
- "BindingSource component [Windows Forms], about BindingSource component"
|
||||
- "data binding [Windows Forms], BindingSource component"
|
||||
ms.assetid: 7bc69c90-8a11-48b1-9336-3adab5b41591
|
||||
---
|
||||
# BindingSource Component Architecture
|
||||
With the <xref:System.Windows.Forms.BindingSource> component, you can universally bind all Windows Forms controls to data sources.
|
||||
|
||||
The <xref:System.Windows.Forms.BindingSource> component simplifies the process of binding controls to a data source and provides the following advantages over traditional data binding:
|
||||
|
||||
- Enables design-time binding to business objects.
|
||||
|
||||
- Encapsulates <xref:System.Windows.Forms.CurrencyManager> functionality and exposes <xref:System.Windows.Forms.CurrencyManager> events at design time.
|
||||
|
||||
- Simplifies creating a list that supports the <xref:System.ComponentModel.IBindingList> interface by providing list change notification for data sources that do not natively support list change notification.
|
||||
|
||||
- Provides an extensibility point for the <xref:System.ComponentModel.IBindingList.AddNew%2A?displayProperty=nameWithType> method.
|
||||
|
||||
- Provides a level of indirection between the data source and the control. This indirection is important when the data source may change at run time.
|
||||
|
||||
- Interoperates with other data-related Windows Forms controls, specifically the <xref:System.Windows.Forms.BindingNavigator> and the <xref:System.Windows.Forms.DataGridView> controls.
|
||||
|
||||
For these reasons, the <xref:System.Windows.Forms.BindingSource> component is the preferred way to bind your Windows Forms controls to data sources.
|
||||
|
||||
## BindingSource Features
|
||||
The <xref:System.Windows.Forms.BindingSource> component provides several features for binding controls to data. With these features, you can implement most data-binding scenarios with almost no coding on your part.
|
||||
|
||||
The <xref:System.Windows.Forms.BindingSource> component accomplishes this by providing a consistent interface for accessing many different kinds of data sources. This means that you use the same procedure for binding to any type. For example, you can attach the <xref:System.Windows.Forms.BindingSource.DataSource%2A> property to a <xref:System.Data.DataSet> or to a business object and in both cases you use the same set of properties, methods, and events to manipulate the data source.
|
||||
|
||||
The consistent interface provided by the <xref:System.Windows.Forms.BindingSource> component greatly simplifies the process of binding data to controls. For data-source types that provide change notification, the <xref:System.Windows.Forms.BindingSource> component automatically communicates changes between the control and the data source. For data-source types that do not provide change notification, events are provided that let you raise change notifications. The following list shows the features supported by the <xref:System.Windows.Forms.BindingSource> component:
|
||||
|
||||
- Indirection.
|
||||
|
||||
- Currency management.
|
||||
|
||||
- Data source as a list.
|
||||
|
||||
- <xref:System.Windows.Forms.BindingSource> as an <xref:System.ComponentModel.IBindingList>.
|
||||
|
||||
- Custom item creation.
|
||||
|
||||
- Transactional item creation.
|
||||
|
||||
- <xref:System.Collections.IEnumerable> support.
|
||||
|
||||
- Design-time support.
|
||||
|
||||
- Static <xref:System.Windows.Forms.ListBindingHelper> methods.
|
||||
|
||||
- Sorting and filtering with the <xref:System.ComponentModel.IBindingListView> interface.
|
||||
|
||||
- Integration with <xref:System.Windows.Forms.BindingNavigator>.
|
||||
|
||||
### Indirection
|
||||
The <xref:System.Windows.Forms.BindingSource> component provides a level of indirection between a control and a data source. Instead of binding a control directly to a data source, you bind the control to a <xref:System.Windows.Forms.BindingSource>, and you attach the data source to the <xref:System.Windows.Forms.BindingSource> component's <xref:System.Windows.Forms.BindingSource.DataSource%2A> property.
|
||||
|
||||
With this level of indirection, you can change the data source without resetting the control binding. This gives you the following capabilities:
|
||||
|
||||
- You can attach the <xref:System.Windows.Forms.BindingSource> to different data sources while retaining the current control bindings.
|
||||
|
||||
- You can change items in the data source and notify bound controls. For more information, see [How to: Reflect Data Source Updates in a Windows Forms Control with the BindingSource](reflect-data-source-updates-in-a-wf-control-with-the-bindingsource.md).
|
||||
|
||||
- You can bind to a <xref:System.Type> instead of an object in memory. For more information, see [How to: Bind a Windows Forms Control to a Type](how-to-bind-a-windows-forms-control-to-a-type.md). You can then bind to an object at run time.
|
||||
|
||||
### Currency Management
|
||||
The <xref:System.Windows.Forms.BindingSource> component implements the <xref:System.Windows.Forms.ICurrencyManagerProvider> interface to handle currency management for you. With the <xref:System.Windows.Forms.ICurrencyManagerProvider> interface, you can also access to the currency manager for a <xref:System.Windows.Forms.BindingSource>, in addition to the currency manager for another <xref:System.Windows.Forms.BindingSource> bound to the same <xref:System.Windows.Forms.BindingSource.DataMember%2A>.
|
||||
|
||||
The <xref:System.Windows.Forms.BindingSource> component encapsulates <xref:System.Windows.Forms.CurrencyManager> functionality and exposes the most common <xref:System.Windows.Forms.CurrencyManager> properties and events. The following table describes some of the members related to currency management.
|
||||
|
||||
<xref:System.Windows.Forms.ICurrencyManagerProvider.CurrencyManager%2A> property
|
||||
Gets the currency manager associated with the <xref:System.Windows.Forms.BindingSource>.
|
||||
|
||||
<xref:System.Windows.Forms.ICurrencyManagerProvider.GetRelatedCurrencyManager%2A> method
|
||||
If there is another <xref:System.Windows.Forms.BindingSource> bound to the specified data member, gets its currency manager.
|
||||
|
||||
<xref:System.Windows.Forms.BindingSource.Current%2A> property
|
||||
Gets the current item of the data source.
|
||||
|
||||
<xref:System.Windows.Forms.BindingSource.Position%2A> property
|
||||
Gets or sets the current position in the underlying list.
|
||||
|
||||
<xref:System.Windows.Forms.BindingSource.EndEdit%2A> method
|
||||
Applies pending changes to the underlying data source.
|
||||
|
||||
<xref:System.Windows.Forms.BindingSource.CancelEdit%2A> method
|
||||
Cancels the current edit operation.
|
||||
|
||||
### Data Source as a List
|
||||
The <xref:System.Windows.Forms.BindingSource> component implements the <xref:System.ComponentModel.IBindingListView> and <xref:System.ComponentModel.ITypedList> interfaces. With this implementation, you can use the <xref:System.Windows.Forms.BindingSource> component itself as a data source, without any external storage.
|
||||
|
||||
When the <xref:System.Windows.Forms.BindingSource> component is attached to a data source, it exposes the data source as a list.
|
||||
|
||||
The <xref:System.Windows.Forms.BindingSource.DataSource%2A> property can be set to several data sources. These include types, objects, and lists of types. The resulting data source will be exposed as a list. The following table shows some of the common data sources and the resulting list evaluation.
|
||||
|
||||
|DataSource property|List results|
|
||||
|-------------------------|------------------|
|
||||
|A null reference (`Nothing` in Visual Basic)|An empty <xref:System.ComponentModel.IBindingList> of objects. Adding an item sets the list to the type of the added item.|
|
||||
|A null reference (`Nothing` in Visual Basic) with <xref:System.Windows.Forms.BindingSource.DataMember%2A> set|Not supported; raises <xref:System.ArgumentException>.|
|
||||
|Non-list type or object of type "T"|An empty <xref:System.ComponentModel.IBindingList> of type "T".|
|
||||
|Array instance|An <xref:System.ComponentModel.IBindingList> containing the array elements.|
|
||||
|<xref:System.Collections.IEnumerable> instance|An <xref:System.ComponentModel.IBindingList> containing the <xref:System.Collections.IEnumerable> items|
|
||||
|List instance containing type "T"|An <xref:System.ComponentModel.IBindingList> instance containing type "T".|
|
||||
|
||||
Additionally, <xref:System.Windows.Forms.BindingSource.DataSource%2A> can be set to other list types, such as <xref:System.ComponentModel.IListSource> and <xref:System.ComponentModel.ITypedList>, and the <xref:System.Windows.Forms.BindingSource> will handle them appropriately. In this case, the type that is contained in the list should have a parameterless constructor.
|
||||
|
||||
### BindingSource as an IBindingList
|
||||
The <xref:System.Windows.Forms.BindingSource> component provides members for accessing and manipulating the underlying data as an <xref:System.ComponentModel.IBindingList>. The following table describes some of these members.
|
||||
|
||||
|Member|Description|
|
||||
|------------|-----------------|
|
||||
|<xref:System.Windows.Forms.BindingSource.List%2A> property|Gets the list that results from the evaluation of the <xref:System.Windows.Forms.BindingSource.DataSource%2A> or <xref:System.Windows.Forms.BindingSource.DataMember%2A> properties.|
|
||||
|<xref:System.Windows.Forms.BindingSource.AddNew%2A> method|Adds a new item to the underlying list. Applies to data sources that implement the <xref:System.ComponentModel.IBindingList> interface and allow adding items (that is, the <xref:System.Windows.Forms.BindingSource.AllowNew%2A> property is set to `true`).|
|
||||
|
||||
### Custom Item Creation
|
||||
You can handle the <xref:System.Windows.Forms.BindingSource.AddingNew> event to provide your own item-creation logic. The <xref:System.Windows.Forms.BindingSource.AddingNew> event occurs before a new object is added to the <xref:System.Windows.Forms.BindingSource>. This event is raised after the <xref:System.Windows.Forms.BindingSource.AddNew%2A> method is called, but before the new item is added to the underlying list. By handling this event, you can provide custom item creation behavior without deriving from the <xref:System.Windows.Forms.BindingSource> class. For more information, see [How to: Customize Item Addition with the Windows Forms BindingSource](how-to-customize-item-addition-with-the-windows-forms-bindingsource.md).
|
||||
|
||||
### Transactional Item Creation
|
||||
The <xref:System.Windows.Forms.BindingSource> component implements the <xref:System.ComponentModel.ICancelAddNew> interface, which enables transactional item creation. After a new item is provisionally created by using a call to <xref:System.Windows.Forms.BindingSource.AddNew%2A>, the addition may be committed or rolled back in the following ways:
|
||||
|
||||
- The <xref:System.ComponentModel.ICancelAddNew.EndNew%2A> method will explicitly commit the pending addition.
|
||||
|
||||
- Performing another collection operation, such as an insertion, removal, or move, will implicitly commit the pending addition.
|
||||
|
||||
- The <xref:System.ComponentModel.ICancelAddNew.CancelNew%2A> method will roll back the pending addition if the method has not already been committed.
|
||||
|
||||
### IEnumerable Support
|
||||
The <xref:System.Windows.Forms.BindingSource> component enables binding controls to <xref:System.Collections.IEnumerable> data sources. With this component, you can bind to a data source such as a <xref:System.Data.SqlClient.SqlDataReader?displayProperty=nameWithType>.
|
||||
|
||||
When an <xref:System.Collections.IEnumerable> data source is assigned to the <xref:System.Windows.Forms.BindingSource> component, the <xref:System.Windows.Forms.BindingSource> creates an <xref:System.ComponentModel.IBindingList> and adds the contents of the <xref:System.Collections.IEnumerable> data source to the list.
|
||||
|
||||
### Design-Time Support
|
||||
Some object types cannot be created at design time, such as objects created from a factory class, or objects returned by a Web service. You may sometimes have to bind your controls to these types at design time, even though there is no object in memory to which your controls can bind. You may, for example, need to label the column headers of a <xref:System.Windows.Forms.DataGridView> control with the names of your custom type's public properties.
|
||||
|
||||
To support this scenario, the <xref:System.Windows.Forms.BindingSource> component supports binding to a <xref:System.Type>. When you assign a <xref:System.Type> to the <xref:System.Windows.Forms.BindingSource.DataSource%2A> property, the <xref:System.Windows.Forms.BindingSource> component creates an empty <xref:System.ComponentModel.BindingList%601> of <xref:System.Type> items. Any controls you subsequently bind to the <xref:System.Windows.Forms.BindingSource> component will be alerted to the presence of the properties or schema of your type at design time, or at run time. For more information, see [How to: Bind a Windows Forms Control to a Type](how-to-bind-a-windows-forms-control-to-a-type.md).
|
||||
|
||||
### Static ListBindingHelper Methods
|
||||
The <xref:System.Windows.Forms.BindingContext?displayProperty=nameWithType>, <xref:System.Windows.Forms.CurrencyManager?displayProperty=nameWithType>, and <xref:System.Windows.Forms.BindingSource> types all share common logic to generate a list from a `DataSource`/`DataMember` pair. Additionally, this common logic is publicly exposed for use by control authors and other third parties in the following `static` methods:
|
||||
|
||||
- <xref:System.Windows.Forms.ListBindingHelper.GetListItemProperties%2A>
|
||||
|
||||
- <xref:System.Windows.Forms.ListBindingHelper.GetList%2A>.
|
||||
|
||||
- <xref:System.Windows.Forms.ListBindingHelper.GetListName%2A>
|
||||
|
||||
- <xref:System.Windows.Forms.ListBindingHelper.GetListItemType%2A>
|
||||
|
||||
### Sorting and Filtering with the IBindingListView Interface
|
||||
The <xref:System.Windows.Forms.BindingSource> component implements the <xref:System.ComponentModel.IBindingListView> interface, which extends the <xref:System.ComponentModel.IBindingList> interface. The <xref:System.ComponentModel.IBindingList> offers single column sorting and the <xref:System.ComponentModel.IBindingListView> offers advanced sorting and filtering. With <xref:System.ComponentModel.IBindingListView>, you can sort and filter items in the data source, if the data source also implements one of these interfaces. The <xref:System.Windows.Forms.BindingSource> component does not provide a reference implementation of these members. Instead, calls are forwarded to the underlying list.
|
||||
|
||||
The following table describes the properties you use for sorting and filtering.
|
||||
|
||||
|Member|Description|
|
||||
|------------|-----------------|
|
||||
|<xref:System.Windows.Forms.BindingSource.Filter%2A> property|If the data source is an <xref:System.ComponentModel.IBindingListView>, gets or sets the expression used to filter which rows are viewed.|
|
||||
|<xref:System.Windows.Forms.BindingSource.Sort%2A> property|If the data source is an <xref:System.ComponentModel.IBindingList>, gets or sets a column name used for sorting and sort order information.<br /><br /> -or-<br /><br /> If the data source is an <xref:System.ComponentModel.IBindingListView> and supports advanced sorting, gets multiple column names used for sorting and sort order|
|
||||
|
||||
### Integration with BindingNavigator
|
||||
You can use the <xref:System.Windows.Forms.BindingSource> component to bind any Windows Forms control to a data source, but the <xref:System.Windows.Forms.BindingNavigator> control is designed specifically to work with the <xref:System.Windows.Forms.BindingSource> component. The <xref:System.Windows.Forms.BindingNavigator> control provides a user interface for controlling the <xref:System.Windows.Forms.BindingSource> component's current item. By default, the <xref:System.Windows.Forms.BindingNavigator> control provides buttons that correspond to the navigation methods on the <xref:System.Windows.Forms.BindingSource> component. For more information, see [How to: Navigate Data with the Windows Forms BindingNavigator Control](how-to-navigate-data-with-the-windows-forms-bindingnavigator-control.md).
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.BindingSource>
|
||||
- <xref:System.Windows.Forms.BindingNavigator>
|
||||
- [BindingSource Component Overview](bindingsource-component-overview.md)
|
||||
- [BindingNavigator Control](bindingnavigator-control-windows-forms.md)
|
||||
- [Windows Forms Data Binding](../windows-forms-data-binding.md)
|
||||
- [Controls to Use on Windows Forms](controls-to-use-on-windows-forms.md)
|
||||
- [How to: Bind a Windows Forms Control to a Type](how-to-bind-a-windows-forms-control-to-a-type.md)
|
||||
- [How to: Reflect Data Source Updates in a Windows Forms Control with the BindingSource](reflect-data-source-updates-in-a-wf-control-with-the-bindingsource.md)
|
||||
@@ -0,0 +1,62 @@
|
||||
---
|
||||
title: "BindingSource Component Overview"
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "Windows Forms, data binding"
|
||||
- "controls [Windows Forms], binding to data"
|
||||
- "BindingSource component [Windows Forms], about BindingSource component"
|
||||
- "data binding [Windows Forms], BindingSource component"
|
||||
ms.assetid: be838caf-fcb0-4b68-827f-58b2c04b747f
|
||||
---
|
||||
# BindingSource Component Overview
|
||||
The <xref:System.Windows.Forms.BindingSource> component is designed to simplify the process of binding controls to an underlying data source. The <xref:System.Windows.Forms.BindingSource> component acts as both a conduit and a data source for other controls to bind to. It provides an abstraction of your form's data connection while passing through commands to the underlying list of data. Additionally, you can add data directly to it, so that the component itself functions as a data source.
|
||||
|
||||
## BindingSource Component as an Intermediary
|
||||
The <xref:System.Windows.Forms.BindingSource> component acts as the data source for some or all of the controls on the form. In Visual Studio, the <xref:System.Windows.Forms.BindingSource> can be bound to a control by means of the `DataBindings` property, which is accessible from the **Properties** window. Also see [How to: Bind Windows Forms Controls with the BindingSource Component Using the Designer](bind-wf-controls-with-the-bindingsource.md).
|
||||
|
||||
You can bind the <xref:System.Windows.Forms.BindingSource> component to both simple data sources, like a single property of an object or a basic collection like <xref:System.Collections.ArrayList>, and complex data sources, like a database table. The <xref:System.Windows.Forms.BindingSource> component acts as an intermediary that provides binding and currency management services. At design time or run time, you can bind a <xref:System.Windows.Forms.BindingSource> component to a complex data source by setting its <xref:System.Windows.Forms.BindingSource.DataSource%2A> and <xref:System.Windows.Forms.BindingSource.DataMember%2A> properties to the database and table, respectively. The following illustration demonstrates where the <xref:System.Windows.Forms.BindingSource> component fits into the existing data-binding architecture.
|
||||
|
||||

|
||||
|
||||
> [!NOTE]
|
||||
> At design time, some actions, like dragging a database table from a data window onto a blank form, will create the <xref:System.Windows.Forms.BindingSource> component, bind it to the underlying data source, and add data-aware controls all in one operation. Also see [Bind Windows Forms controls to data in Visual Studio](/visualstudio/data-tools/bind-windows-forms-controls-to-data-in-visual-studio).
|
||||
|
||||
## BindingSource Component as a Data Source
|
||||
If you start adding items to the <xref:System.Windows.Forms.BindingSource> component without first specifying a list to be bound to, the component will act like a list-style data source and accept these added items.
|
||||
|
||||
Additionally, you can write code to provide custom "AddNew" functionality by means of the <xref:System.Windows.Forms.BindingSource.AddingNew> event, which is raised when the <xref:System.Windows.Forms.BindingSource.AddNew%2A> method is called prior to the item being added to the list. For more information, see [BindingSource Component Architecture](bindingsource-component-architecture.md).
|
||||
|
||||
## Navigation
|
||||
For users that need to navigate the data on a form, the <xref:System.Windows.Forms.BindingNavigator> component enables you to navigate and manipulate data, in coordination with a <xref:System.Windows.Forms.BindingSource> component. For more information, see [BindingNavigator Control](bindingnavigator-control-windows-forms.md).
|
||||
|
||||
## Data Manipulation
|
||||
The: <xref:System.Windows.Forms.BindingSource> acts as a <xref:System.Windows.Forms.CurrencyManager> for all of its bindings and can, therefore, provide access to currency and position information regarding the data source. The following table shows the members that the <xref:System.Windows.Forms.BindingSource> component provides for accessing and manipulating the underlying data.
|
||||
|
||||
|Member|Description|
|
||||
|------------|-----------------|
|
||||
|<xref:System.Windows.Forms.BindingSource.Current%2A> property|Gets the current item of the data source.|
|
||||
|<xref:System.Windows.Forms.BindingSource.Position%2A> property|Gets or sets the current position in the underlying list.|
|
||||
|<xref:System.Windows.Forms.BindingSource.List%2A> property|Gets the list that is the evaluation of the <xref:System.Windows.Forms.BindingSource.DataSource%2A> and <xref:System.Windows.Forms.BindingSource.DataMember%2A> evaluation. If <xref:System.Windows.Forms.BindingSource.DataMember%2A> is not set, returns the list specified by <xref:System.Windows.Forms.BindingSource.DataSource%2A>.|
|
||||
|<xref:System.Windows.Forms.BindingSource.Insert%2A> method|Inserts an item in the list at the specified index.|
|
||||
|<xref:System.Windows.Forms.BindingSource.RemoveCurrent%2A> method|Removes the current item from the list.|
|
||||
|<xref:System.Windows.Forms.BindingSource.EndEdit%2A> method|Applies pending changes to the underlying data source.|
|
||||
|<xref:System.Windows.Forms.BindingSource.CancelEdit%2A> method|Cancels the current edit operation.|
|
||||
|<xref:System.Windows.Forms.BindingSource.AddNew%2A> method|Adds a new item to the underlying list. If the data source implements <xref:System.ComponentModel.IBindingList> and returns an item from the <xref:System.Windows.Forms.BindingSource.AddingNew> event, adds this item. Otherwise, the request is passed to the list's <xref:System.ComponentModel.IBindingList.AddNew%2A> method. If the underlying list is not an <xref:System.ComponentModel.IBindingList>, the item is automatically created through its public parameterless constructor.|
|
||||
|
||||
## Sorting and Filtering
|
||||
Usually, you should work with an ordered or filtered view of the data source. The following table shows the members that the <xref:System.Windows.Forms.BindingSource> component data source provides.
|
||||
|
||||
|Member|Description|
|
||||
|------------|-----------------|
|
||||
|<xref:System.Windows.Forms.BindingSource.Sort%2A> property|If the data source is an <xref:System.ComponentModel.IBindingList>, gets or sets a column name used for sorting and sort order information. If the data source is an <xref:System.ComponentModel.IBindingListView> and supports advanced sorting, gets multiple column names used for sorting and sort order information|
|
||||
|<xref:System.Windows.Forms.BindingSource.Filter%2A> property|If the data source is an <xref:System.ComponentModel.IBindingListView>, gets or sets the expression used to filter which rows are viewed.|
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.BindingSource>
|
||||
- <xref:System.Windows.Forms.BindingNavigator>
|
||||
- [BindingSource Component Architecture](bindingsource-component-architecture.md)
|
||||
- [BindingSource Component](bindingsource-component.md)
|
||||
- [BindingNavigator Control](bindingnavigator-control-windows-forms.md)
|
||||
- [Windows Forms Data Binding](../windows-forms-data-binding.md)
|
||||
- [Controls to Use on Windows Forms](controls-to-use-on-windows-forms.md)
|
||||
@@ -0,0 +1,65 @@
|
||||
---
|
||||
title: "BindingSource Component"
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "data binding [Windows Forms], Windows Forms"
|
||||
- "Windows Forms, data binding control"
|
||||
- "BindingSource component [Windows Forms]"
|
||||
ms.assetid: 3e2faf4c-f5b8-4fa6-9fbc-f59c37ec2fb9
|
||||
---
|
||||
# BindingSource Component
|
||||
Encapsulates a data source for binding to controls.
|
||||
|
||||
The <xref:System.Windows.Forms.BindingSource> component serves two purposes. First, it provides a layer of indirection when binding the controls on a form to data. This is accomplished by binding the <xref:System.Windows.Forms.BindingSource> component to your data source, and then binding the controls on your form to the <xref:System.Windows.Forms.BindingSource> component. All further interaction with the data, including navigating, sorting, filtering, and updating, is accomplished with calls to the <xref:System.Windows.Forms.BindingSource> component.
|
||||
|
||||
Second, the <xref:System.Windows.Forms.BindingSource> component can act as a strongly typed data source. Adding a type to the <xref:System.Windows.Forms.BindingSource> component with the <xref:System.Windows.Forms.BindingSource.Add%2A> method creates a list of that type.
|
||||
|
||||
## In This Section
|
||||
[BindingSource Component Overview](bindingsource-component-overview.md)
|
||||
Introduces the general concepts of the <xref:System.Windows.Forms.BindingSource> component, which allows you to bind a data source to a control.
|
||||
|
||||
[How to: Bind Windows Forms Controls to DBNull Database Values](how-to-bind-windows-forms-controls-to-dbnull-database-values.md)
|
||||
Shows how to handle a <xref:System.DBNull> value from the data source using the <xref:System.Windows.Forms.BindingSource> component.
|
||||
|
||||
[How to: Sort and Filter ADO.NET Data with the Windows Forms BindingSource Component](sort-and-filter-ado-net-data-with-wf-bindingsource-component.md)
|
||||
Demonstrates using the <xref:System.Windows.Forms.BindingSource> component to apply sorts and filters to displayed data.
|
||||
|
||||
[How to: Bind to a Web Service Using the Windows Forms BindingSource](how-to-bind-to-a-web-service-using-the-windows-forms-bindingsource.md)
|
||||
Shows how to use the <xref:System.Windows.Forms.BindingSource> component to bind to a Web service.
|
||||
|
||||
[How to: Handle Errors and Exceptions that Occur with Databinding](how-to-handle-errors-and-exceptions-that-occur-with-databinding.md)
|
||||
Demonstrates using the <xref:System.Windows.Forms.BindingSource> component to gracefully handle errors that occur in a data binding operation.
|
||||
|
||||
[How to: Bind a Windows Forms Control to a Type](how-to-bind-a-windows-forms-control-to-a-type.md)
|
||||
Demonstrates using a <xref:System.Windows.Forms.BindingSource> component to bind to a type.
|
||||
|
||||
[How to: Bind a Windows Forms Control to a Factory Object](how-to-bind-a-windows-forms-control-to-a-factory-object.md)
|
||||
Demonstrates using a <xref:System.Windows.Forms.BindingSource> component to bind to a factory object or method.
|
||||
|
||||
[How to: Customize Item Addition with the Windows Forms BindingSource](how-to-customize-item-addition-with-the-windows-forms-bindingsource.md)
|
||||
Demonstrates using a <xref:System.Windows.Forms.BindingSource> component to create new items and add them to a data source.
|
||||
|
||||
[How to: Raise Change Notifications Using the BindingSource ResetItem Method](how-to-raise-change-notifications-using-the-bindingsource-resetitem-method.md)
|
||||
Demonstrates using a <xref:System.Windows.Forms.BindingSource> component to raise change-notification events for data sources that do not support change notification.
|
||||
|
||||
[How to: Raise Change Notifications Using a BindingSource and the INotifyPropertyChanged Interface](raise-change-notifications--bindingsource.md)
|
||||
Demonstrates how to use a type that inherits from the <xref:System.ComponentModel.INotifyPropertyChanged> with a <xref:System.Windows.Forms.BindingSource> control.
|
||||
|
||||
[How to: Reflect Data Source Updates in a Windows Forms Control with the BindingSource](reflect-data-source-updates-in-a-wf-control-with-the-bindingsource.md)
|
||||
Demonstrates how to respond to changes in the data source using the <xref:System.Windows.Forms.BindingSource> component.
|
||||
|
||||
[How to: Share Bound Data Across Forms Using the BindingSource Component](how-to-share-bound-data-across-forms-using-the-bindingsource-component.md)
|
||||
Shows how to use the <xref:System.Windows.Forms.BindingSource> to bind multiple forms to the same data source.
|
||||
|
||||
## Reference
|
||||
<xref:System.Windows.Forms.BindingSource>
|
||||
Provides reference documentation for the <xref:System.Windows.Forms.BindingSource> component.
|
||||
|
||||
<xref:System.Windows.Forms.BindingNavigator>
|
||||
Provides reference documentation for the <xref:System.Windows.Forms.BindingNavigator> control.
|
||||
|
||||
## Related Sections
|
||||
[Windows Forms Data Binding](../windows-forms-data-binding.md)
|
||||
Contains links to topics describing the Windows Forms data binding architecture.
|
||||
|
||||
Also see [Bind controls to data in Visual Studio](/visualstudio/data-tools/bind-controls-to-data-in-visual-studio).
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
---
|
||||
title: "Button Control Overview"
|
||||
ms.date: "03/30/2017"
|
||||
f1_keywords:
|
||||
- "Button"
|
||||
helpviewer_keywords:
|
||||
- "Button control [Windows Forms], about Button control"
|
||||
- "buttons [Windows Forms], about buttons"
|
||||
ms.assetid: 255b291b-51a9-4a92-a1a4-2400cd82443f
|
||||
---
|
||||
# Button Control Overview (Windows Forms)
|
||||
The Windows Forms <xref:System.Windows.Forms.Button> control allows the user to click it to perform an action. When the button is clicked, it looks as if it is being pushed in and released. Whenever the user clicks a button, the <xref:System.Windows.Forms.Control.Click> event handler is invoked. You place code in the <xref:System.Windows.Forms.Control.Click> event handler to perform any action you choose.
|
||||
|
||||
The text displayed on the button is contained in the <xref:System.Windows.Forms.Control.Text%2A> property. If your text exceeds the width of the button, it will wrap to the next line. However, it will be clipped if the control cannot accommodate its overall height. For more information, see [How to: Set the Text Displayed by a Windows Forms Control](how-to-set-the-text-displayed-by-a-windows-forms-control.md). The <xref:System.Windows.Forms.Control.Text%2A> property can contain an access key, which allows a user to "click" the control by pressing the ALT key with the access key. For details, see [How to: Create Access Keys for Windows Forms Controls](how-to-create-access-keys-for-windows-forms-controls.md). The appearance of the text is controlled by the <xref:System.Windows.Forms.Control.Font%2A> property and the <xref:System.Windows.Forms.ButtonBase.TextAlign%2A> property.
|
||||
|
||||
The <xref:System.Windows.Forms.Button> control can also display images using the <xref:System.Windows.Forms.ButtonBase.Image%2A> and <xref:System.Windows.Forms.ButtonBase.ImageList%2A> properties. For more information, see [How to: Set the Image Displayed by a Windows Forms Control](how-to-set-the-image-displayed-by-a-windows-forms-control.md).
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.Button>
|
||||
- [How to: Respond to Windows Forms Button Clicks](how-to-respond-to-windows-forms-button-clicks.md)
|
||||
- [Ways to Select a Windows Forms Button Control](ways-to-select-a-windows-forms-button-control.md)
|
||||
- [How to: Designate a Windows Forms Button as the Accept Button Using the Designer](designate-a-wf-button-as-the-accept-button-using-the-designer.md)
|
||||
- [How to: Designate a Windows Forms Button as the Cancel Button Using the Designer](designate-a-wf-button-as-the-cancel-button-using-the-designer.md)
|
||||
- [Button Control](button-control-windows-forms.md)
|
||||
@@ -0,0 +1,38 @@
|
||||
---
|
||||
title: "Button Control"
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "buttons"
|
||||
- "Button control [Windows Forms]"
|
||||
ms.assetid: d38bc40c-8040-4f19-9e88-2c665b0ab80b
|
||||
---
|
||||
# Button Control (Windows Forms)
|
||||
The Windows Forms `Button` control allows the user to click it to perform an action. The `Button` control can display both text and images. When the button is clicked, it looks as if it is being pushed in and released.
|
||||
|
||||
## In This Section
|
||||
[Button Control Overview](button-control-overview-windows-forms.md)
|
||||
Explains what this control is and its key features and properties.
|
||||
|
||||
[How to: Respond to Windows Forms Button Clicks](how-to-respond-to-windows-forms-button-clicks.md)
|
||||
Explains the most basic use of a button on a Windows Form.
|
||||
|
||||
[How to: Designate a Windows Forms Button as the Accept Button](how-to-designate-a-windows-forms-button-as-the-accept-button.md)
|
||||
Explains how to designate a `Button` control to be the accept button, also known as the default button.
|
||||
|
||||
[How to: Designate a Windows Forms Button as the Cancel Button](how-to-designate-a-windows-forms-button-as-the-cancel-button.md)
|
||||
Explains how to designate a `Button` control to be the cancel button, which is clicked whenever the user presses the ESC key.
|
||||
|
||||
[Ways to Select a Windows Forms Button Control](ways-to-select-a-windows-forms-button-control.md)
|
||||
Lists methods of selecting a button.
|
||||
|
||||
Also see [How to: Designate a Windows Forms Button as the Accept Button Using the Designer](designate-a-wf-button-as-the-accept-button-using-the-designer.md) and [How to: Designate a Windows Forms Button as the Cancel Button Using the Designer](designate-a-wf-button-as-the-cancel-button-using-the-designer.md).
|
||||
|
||||
## Reference
|
||||
<xref:System.Windows.Forms.Button> class
|
||||
Describes this class and has links to all its members.
|
||||
|
||||
## Related Sections
|
||||
[Controls to Use on Windows Forms](controls-to-use-on-windows-forms.md)
|
||||
Provides a complete list of Windows Forms controls, with links to information on their use.
|
||||
|
||||
Also see [User Input to Dialog Boxes](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2010/1s9ws53w(v=vs.100)) and [How to: Close Dialog Boxes and Retain User Input](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2010/65ad5907(v=vs.100)).
|
||||
+162
@@ -0,0 +1,162 @@
|
||||
---
|
||||
title: Cell Styles in DataGridView Control
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "DataGridView control [Windows Forms], cell styles"
|
||||
- "cells [Windows Forms], styles"
|
||||
- "data grids [Windows Forms], cell styles"
|
||||
ms.assetid: dbb75ed6-8804-4232-8382-f9920c2e380c
|
||||
---
|
||||
# Cell Styles in the Windows Forms DataGridView Control
|
||||
Each cell within the <xref:System.Windows.Forms.DataGridView> control can have its own style, such as text format, background color, foreground color, and font. Typically, however, multiple cells will share particular style characteristics.
|
||||
|
||||
Groups of cells that share styles may include all cells within particular rows or columns, all cells that contain particular values, or all cells in the control. Because these groups overlap, each cell may get its styling information from more than one place. For example, you may want every cell in a <xref:System.Windows.Forms.DataGridView> control to use the same font, but only cells in currency columns to use currency format, and only currency cells with negative numbers to use a red foreground color.
|
||||
|
||||
## The DataGridViewCellStyle Class
|
||||
The <xref:System.Windows.Forms.DataGridViewCellStyle> class contains the following properties related to visual style:
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewCellStyle.BackColor%2A> and <xref:System.Windows.Forms.DataGridViewCellStyle.ForeColor%2A>
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewCellStyle.SelectionBackColor%2A> and <xref:System.Windows.Forms.DataGridViewCellStyle.SelectionForeColor%2A>
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewCellStyle.Font%2A>
|
||||
|
||||
This class also contains the following properties related to formatting:
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewCellStyle.Format%2A> and <xref:System.Windows.Forms.DataGridViewCellStyle.FormatProvider%2A>
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewCellStyle.NullValue%2A> and <xref:System.Windows.Forms.DataGridViewCellStyle.DataSourceNullValue%2A>
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewCellStyle.WrapMode%2A>
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewCellStyle.Alignment%2A>
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewCellStyle.Padding%2A>
|
||||
|
||||
For more information on these properties and other cell-style properties, see the <xref:System.Windows.Forms.DataGridViewCellStyle> reference documentation and the topics listed in the See Also section below.
|
||||
|
||||
## Using DataGridViewCellStyle Objects
|
||||
You can retrieve <xref:System.Windows.Forms.DataGridViewCellStyle> objects from various properties of the <xref:System.Windows.Forms.DataGridView>, <xref:System.Windows.Forms.DataGridViewColumn>, <xref:System.Windows.Forms.DataGridViewRow>, and <xref:System.Windows.Forms.DataGridViewCell> classes and their derived classes. If one of these properties has not yet been set, retrieving its value will create a new <xref:System.Windows.Forms.DataGridViewCellStyle> object. You can also instantiate your own <xref:System.Windows.Forms.DataGridViewCellStyle> objects and assign them to these properties.
|
||||
|
||||
You can avoid unnecessary duplication of style information by sharing <xref:System.Windows.Forms.DataGridViewCellStyle> objects among multiple <xref:System.Windows.Forms.DataGridView> elements. Because the styles set at the control, column, and row levels filter down through each level to the cell level, you can also avoid style duplication by setting only those style properties at each level that differ from the levels above. This is described in more detail in the Style Inheritance section that follows.
|
||||
|
||||
The following table lists the primary properties that get or set <xref:System.Windows.Forms.DataGridViewCellStyle> objects.
|
||||
|
||||
|Property|Classes|Description|
|
||||
|--------------|-------------|-----------------|
|
||||
|`DefaultCellStyle`|<xref:System.Windows.Forms.DataGridView>, <xref:System.Windows.Forms.DataGridViewColumn>, <xref:System.Windows.Forms.DataGridViewRow>, and derived classes|Gets or sets default styles used by all cells in the entire control (including header cells), in a column, or in a row.|
|
||||
|<xref:System.Windows.Forms.DataGridView.RowsDefaultCellStyle%2A>|<xref:System.Windows.Forms.DataGridView>|Gets or sets default cell styles used by all rows in the control. This does not include header cells.|
|
||||
|<xref:System.Windows.Forms.DataGridView.AlternatingRowsDefaultCellStyle%2A>|<xref:System.Windows.Forms.DataGridView>|Gets or sets default cell styles used by alternating rows in the control. Used to create a ledger-like effect.|
|
||||
|<xref:System.Windows.Forms.DataGridView.RowHeadersDefaultCellStyle%2A>|<xref:System.Windows.Forms.DataGridView>|Gets or sets default cell styles used by the control's row headers. Overridden by the current theme if visual styles are enabled.|
|
||||
|<xref:System.Windows.Forms.DataGridView.ColumnHeadersDefaultCellStyle%2A>|<xref:System.Windows.Forms.DataGridView>|Gets or sets default cell styles used by the control's column headers. Overridden by the current theme if visual styles are enabled.|
|
||||
|<xref:System.Windows.Forms.DataGridViewCell.Style%2A>|<xref:System.Windows.Forms.DataGridViewCell> and derived classes|Gets or sets styles specified at the cell level. These styles override those inherited from higher levels.|
|
||||
|`InheritedStyle`|<xref:System.Windows.Forms.DataGridViewCell>, <xref:System.Windows.Forms.DataGridViewRow>, <xref:System.Windows.Forms.DataGridViewColumn>, and derived classes|Gets all the styles currently applied to the cell, row, or column, including styles inherited from higher levels.|
|
||||
|
||||
As mentioned above, getting the value of a style property automatically instantiates a new <xref:System.Windows.Forms.DataGridViewCellStyle> object if the property has not been previously set. To avoid creating these objects unnecessarily, the row and column classes have a <xref:System.Windows.Forms.DataGridViewBand.HasDefaultCellStyle%2A> property that you can check to determine whether the <xref:System.Windows.Forms.DataGridViewBand.DefaultCellStyle%2A> property has been set. Similarly, the cell classes have a <xref:System.Windows.Forms.DataGridViewCell.HasStyle%2A> property that indicates whether the <xref:System.Windows.Forms.DataGridViewCell.Style%2A> property has been set.
|
||||
|
||||
Each of the style properties has a corresponding *PropertyName*`Changed` event on the <xref:System.Windows.Forms.DataGridView> control. For row, column, and cell properties, the name of the event begins with "`Row`", "`Column`", or "`Cell`" (for example, <xref:System.Windows.Forms.DataGridView.RowDefaultCellStyleChanged>). Each of these events occurs when the corresponding style property is set to a different <xref:System.Windows.Forms.DataGridViewCellStyle> object. These events do not occur when you retrieve a <xref:System.Windows.Forms.DataGridViewCellStyle> object from a style property and modify its property values. To respond to changes to the cell style objects themselves, handle the <xref:System.Windows.Forms.DataGridView.CellStyleContentChanged> event.
|
||||
|
||||
## Style Inheritance
|
||||
Each <xref:System.Windows.Forms.DataGridViewCell> gets its appearance from its <xref:System.Windows.Forms.DataGridViewCell.InheritedStyle%2A> property. The <xref:System.Windows.Forms.DataGridViewCellStyle> object returned by this property inherits its values from a hierarchy of properties of type <xref:System.Windows.Forms.DataGridViewCellStyle>. These properties are listed below in the order in which the <xref:System.Windows.Forms.DataGridViewCell.InheritedStyle%2A> for non-header cells obtains its values.
|
||||
|
||||
1. <xref:System.Windows.Forms.DataGridViewCell.Style%2A?displayProperty=nameWithType>
|
||||
|
||||
2. <xref:System.Windows.Forms.DataGridViewRow.DefaultCellStyle%2A?displayProperty=nameWithType>
|
||||
|
||||
3. <xref:System.Windows.Forms.DataGridView.AlternatingRowsDefaultCellStyle%2A?displayProperty=nameWithType> (only for cells in rows with odd index numbers)
|
||||
|
||||
4. <xref:System.Windows.Forms.DataGridView.RowsDefaultCellStyle%2A?displayProperty=nameWithType>
|
||||
|
||||
5. <xref:System.Windows.Forms.DataGridViewColumn.DefaultCellStyle%2A?displayProperty=nameWithType>
|
||||
|
||||
6. <xref:System.Windows.Forms.DataGridView.DefaultCellStyle%2A?displayProperty=nameWithType>
|
||||
|
||||
For row and column header cells, the <xref:System.Windows.Forms.DataGridViewCell.InheritedStyle%2A> property is populated by values from the following list of source properties in the given order.
|
||||
|
||||
1. <xref:System.Windows.Forms.DataGridViewCell.Style%2A?displayProperty=nameWithType>
|
||||
|
||||
2. <xref:System.Windows.Forms.DataGridView.ColumnHeadersDefaultCellStyle%2A?displayProperty=nameWithType> or <xref:System.Windows.Forms.DataGridView.RowHeadersDefaultCellStyle%2A?displayProperty=nameWithType>
|
||||
|
||||
3. <xref:System.Windows.Forms.DataGridView.DefaultCellStyle%2A?displayProperty=nameWithType>
|
||||
|
||||
The following diagram illustrates this process.
|
||||
|
||||

|
||||
|
||||
You can also access the styles inherited by specific rows and columns. The column <xref:System.Windows.Forms.DataGridViewColumn.InheritedStyle%2A> property inherits its values from the following properties.
|
||||
|
||||
1. <xref:System.Windows.Forms.DataGridViewColumn.DefaultCellStyle%2A?displayProperty=nameWithType>
|
||||
|
||||
2. <xref:System.Windows.Forms.DataGridView.DefaultCellStyle%2A?displayProperty=nameWithType>
|
||||
|
||||
The row <xref:System.Windows.Forms.DataGridViewRow.InheritedStyle%2A> property inherits its values from the following properties.
|
||||
|
||||
1. <xref:System.Windows.Forms.DataGridViewRow.DefaultCellStyle%2A?displayProperty=nameWithType>
|
||||
|
||||
2. <xref:System.Windows.Forms.DataGridView.AlternatingRowsDefaultCellStyle%2A?displayProperty=nameWithType> (only for cells in rows with odd index numbers)
|
||||
|
||||
3. <xref:System.Windows.Forms.DataGridView.RowsDefaultCellStyle%2A?displayProperty=nameWithType>
|
||||
|
||||
4. <xref:System.Windows.Forms.DataGridView.DefaultCellStyle%2A?displayProperty=nameWithType>
|
||||
|
||||
For each property in a <xref:System.Windows.Forms.DataGridViewCellStyle> object returned by an `InheritedStyle` property, the property value is obtained from the first cell style in the appropriate list that has the corresponding property set to a value other than the <xref:System.Windows.Forms.DataGridViewCellStyle> class defaults.
|
||||
|
||||
The following table illustrates how the <xref:System.Windows.Forms.DataGridViewCellStyle.ForeColor%2A> property value for an example cell is inherited from its containing column.
|
||||
|
||||
|Property of type `DataGridViewCellStyle`|Example `ForeColor` value for retrieved object|
|
||||
|----------------------------------------------|----------------------------------------------------|
|
||||
|<xref:System.Windows.Forms.DataGridViewCell.Style%2A?displayProperty=nameWithType>|<xref:System.Drawing.Color.Empty?displayProperty=nameWithType>|
|
||||
|<xref:System.Windows.Forms.DataGridViewRow.DefaultCellStyle%2A?displayProperty=nameWithType>|<xref:System.Drawing.Color.Red%2A?displayProperty=nameWithType>|
|
||||
|<xref:System.Windows.Forms.DataGridView.AlternatingRowsDefaultCellStyle%2A?displayProperty=nameWithType>|<xref:System.Drawing.Color.Empty?displayProperty=nameWithType>|
|
||||
|<xref:System.Windows.Forms.DataGridView.RowsDefaultCellStyle%2A?displayProperty=nameWithType>|<xref:System.Drawing.Color.Empty?displayProperty=nameWithType>|
|
||||
|<xref:System.Windows.Forms.DataGridViewColumn.DefaultCellStyle%2A?displayProperty=nameWithType>|<xref:System.Drawing.Color.DarkBlue%2A?displayProperty=nameWithType>|
|
||||
|<xref:System.Windows.Forms.DataGridView.DefaultCellStyle%2A?displayProperty=nameWithType>|<xref:System.Drawing.Color.Black%2A?displayProperty=nameWithType>|
|
||||
|
||||
In this case, the <xref:System.Drawing.Color.Red%2A?displayProperty=nameWithType> value from the cell's row is the first real value on the list. This becomes the <xref:System.Windows.Forms.DataGridViewCellStyle.ForeColor%2A> property value of the cell's <xref:System.Windows.Forms.DataGridViewCell.InheritedStyle%2A>.
|
||||
|
||||
The following diagram illustrates how different <xref:System.Windows.Forms.DataGridViewCellStyle> properties can inherit their values from different places.
|
||||
|
||||

|
||||
|
||||
By taking advantage of style inheritance, you can provide appropriate styles for the entire control without having to specify the same information in multiple places.
|
||||
|
||||
Although header cells participate in style inheritance as described, the objects returned by the <xref:System.Windows.Forms.DataGridView.ColumnHeadersDefaultCellStyle%2A> and <xref:System.Windows.Forms.DataGridView.RowHeadersDefaultCellStyle%2A> properties of the <xref:System.Windows.Forms.DataGridView> control have initial property values that override the property values of the object returned by the <xref:System.Windows.Forms.DataGridView.DefaultCellStyle%2A> property. If you want the properties set for the object returned by the <xref:System.Windows.Forms.DataGridView.DefaultCellStyle%2A> property to apply to row and column headers, you must set the corresponding properties of the objects returned by the <xref:System.Windows.Forms.DataGridView.ColumnHeadersDefaultCellStyle%2A> and <xref:System.Windows.Forms.DataGridView.RowHeadersDefaultCellStyle%2A> properties to the defaults indicated for the <xref:System.Windows.Forms.DataGridViewCellStyle> class.
|
||||
|
||||
> [!NOTE]
|
||||
> If visual styles are enabled, the row and column headers (except for the <xref:System.Windows.Forms.DataGridView.TopLeftHeaderCell%2A>) are automatically styled by the current theme, overriding any styles specified by these properties.
|
||||
|
||||
The <xref:System.Windows.Forms.DataGridViewButtonColumn>, <xref:System.Windows.Forms.DataGridViewImageColumn>, and <xref:System.Windows.Forms.DataGridViewCheckBoxColumn> types also initialize some values of the object returned by the column <xref:System.Windows.Forms.DataGridViewColumn.DefaultCellStyle%2A> property. For more information, see the reference documentation for these types.
|
||||
|
||||
## Setting Styles Dynamically
|
||||
To customize the styles of cells with particular values, implement a handler for the <xref:System.Windows.Forms.DataGridView.CellFormatting?displayProperty=nameWithType> event. Handlers for this event receive an argument of the <xref:System.Windows.Forms.DataGridViewCellFormattingEventArgs> type. This object contains properties that let you determine the value of the cell being formatted along with its location in the <xref:System.Windows.Forms.DataGridView> control. This object also contains a <xref:System.Windows.Forms.DataGridViewCellFormattingEventArgs.CellStyle%2A> property that is initialized to the value of the <xref:System.Windows.Forms.DataGridViewCell.InheritedStyle%2A> property of the cell being formatted. You can modify the cell style properties to specify style information appropriate to the cell value and location.
|
||||
|
||||
> [!NOTE]
|
||||
> The <xref:System.Windows.Forms.DataGridView.RowPrePaint> and <xref:System.Windows.Forms.DataGridView.RowPostPaint> events also receive a <xref:System.Windows.Forms.DataGridViewCellStyle> object in the event data, but in their case, it is a copy of the row <xref:System.Windows.Forms.DataGridViewRow.InheritedStyle%2A> property for read-only purposes, and changes to it do not affect the control.
|
||||
|
||||
You can also dynamically modify the styles of individual cells in response to events such as the <xref:System.Windows.Forms.DataGridView.CellMouseEnter?displayProperty=nameWithType> and <xref:System.Windows.Forms.DataGridView.CellMouseLeave> events. For example, in a handler for the <xref:System.Windows.Forms.DataGridView.CellMouseEnter> event, you could store the current value of the cell background color (retrieved through the cell's <xref:System.Windows.Forms.DataGridViewCell.Style%2A> property), then set it to a new color that will highlight the cell when the mouse hovers over it. In a handler for the <xref:System.Windows.Forms.DataGridView.CellMouseLeave> event, you can then restore the background color to the original value.
|
||||
|
||||
> [!NOTE]
|
||||
> Caching the values stored in the cell's <xref:System.Windows.Forms.DataGridViewCell.Style%2A> property is important regardless of whether a particular style value is set. If you temporarily replace a style setting, restoring it to its original "not set" state ensures that the cell will go back to inheriting the style setting from a higher level. If you need to determine the actual style in effect for a cell regardless of whether the style is inherited, use the cell's <xref:System.Windows.Forms.DataGridViewCell.InheritedStyle%2A> property.
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridView>
|
||||
- <xref:System.Windows.Forms.DataGridViewCellStyle>
|
||||
- <xref:System.Windows.Forms.DataGridView.AlternatingRowsDefaultCellStyle%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridView.ColumnHeadersDefaultCellStyle%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridView.DefaultCellStyle%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridView.RowHeadersDefaultCellStyle%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridView.RowsDefaultCellStyle%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridViewBand.InheritedStyle%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridViewRow.InheritedStyle%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridViewColumn.InheritedStyle%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridViewBand.DefaultCellStyle%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridViewCell.InheritedStyle%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridViewCell.Style%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridView.CellFormatting?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridView.CellStyleContentChanged?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridView.RowPrePaint?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridView.RowPostPaint?displayProperty=nameWithType>
|
||||
- [Basic Formatting and Styling in the Windows Forms DataGridView Control](basic-formatting-and-styling-in-the-windows-forms-datagridview-control.md)
|
||||
- [How to: Set Default Cell Styles for the Windows Forms DataGridView Control](how-to-set-default-cell-styles-for-the-windows-forms-datagridview-control.md)
|
||||
- [Data Formatting in the Windows Forms DataGridView Control](data-formatting-in-the-windows-forms-datagridview-control.md)
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
---
|
||||
title: Change Displayed Data at Run Time in DataGrid Control
|
||||
ms.date: "03/30/2017"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
- "cpp"
|
||||
helpviewer_keywords:
|
||||
- "DataGrid control [Windows Forms], dynamically changing at run time"
|
||||
- "DataGrid control [Windows Forms], data binding"
|
||||
- "cells [Windows Forms], changing DataGrid cell values"
|
||||
ms.assetid: 0c7a6d00-30de-416e-8223-0a81ddb4c1f8
|
||||
---
|
||||
# How to: Change Displayed Data at Run Time in the Windows Forms DataGrid Control
|
||||
> [!NOTE]
|
||||
> The <xref:System.Windows.Forms.DataGridView> control replaces and adds functionality to the <xref:System.Windows.Forms.DataGrid> control; however, the <xref:System.Windows.Forms.DataGrid> control is retained for both backward compatibility and future use, if you choose. For more information, see [Differences Between the Windows Forms DataGridView and DataGrid Controls](differences-between-the-windows-forms-datagridview-and-datagrid-controls.md).
|
||||
|
||||
After you have created a Windows Forms <xref:System.Windows.Forms.DataGrid> using the design-time features, you may also wish to dynamically change elements of the <xref:System.Data.DataSet> object of the grid at run time. This can include changes to either individual values of the table or changing which data source is bound to the <xref:System.Windows.Forms.DataGrid> control. Changes to individual values are done through the <xref:System.Data.DataSet> object, not the <xref:System.Windows.Forms.DataGrid> control.
|
||||
|
||||
### To change data programmatically
|
||||
|
||||
1. Specify the desired table from the <xref:System.Data.DataSet> object and the desired row and field from the table and set the cell equal to the new value.
|
||||
|
||||
> [!NOTE]
|
||||
> To specify the first table of the <xref:System.Data.DataSet> or the first row of the table, use 0.
|
||||
|
||||
The following example shows how to change the second entry of the first row of the first table of a dataset by clicking `Button1`. The <xref:System.Data.DataSet> (`ds`) and Tables (`0` and `1`) were previously created.
|
||||
|
||||
```vb
|
||||
Protected Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
|
||||
ds.tables(0).rows(0)(1) = "NewEntry"
|
||||
End Sub
|
||||
```
|
||||
|
||||
```csharp
|
||||
private void button1_Click(object sender, System.EventArgs e)
|
||||
{
|
||||
ds.Tables[0].Rows[0][1]="NewEntry";
|
||||
}
|
||||
```
|
||||
|
||||
```cpp
|
||||
private:
|
||||
void button1_Click(System::Object^ sender, System::EventArgs^ e)
|
||||
{
|
||||
dataSet1->Tables[0]->Rows[0][1] = "NewEntry";
|
||||
}
|
||||
```
|
||||
|
||||
(Visual C#, Visual C++) Place the following code in the form's constructor to register the event handler.
|
||||
|
||||
```csharp
|
||||
this.button1.Click += new System.EventHandler(this.button1_Click);
|
||||
```
|
||||
|
||||
```cpp
|
||||
this->button1->Click +=
|
||||
gcnew System::EventHandler(this, &Form1::button1_Click);
|
||||
```
|
||||
|
||||
At run time you can use the <xref:System.Windows.Forms.DataGrid.SetDataBinding%2A> method to bind the <xref:System.Windows.Forms.DataGrid> control to a different data source. For example, you may have several ADO.NET data controls, each connected to a different database.
|
||||
|
||||
### To change the DataSource programmatically
|
||||
|
||||
1. Set the <xref:System.Windows.Forms.DataGrid.SetDataBinding%2A> method to the name of the data source and table you want to bind to.
|
||||
|
||||
The following example shows how to change the date source using the <xref:System.Windows.Forms.DataGrid.SetDataBinding%2A> method to an ADO.NET data control (adoPubsAuthors) that is connected to the Authors table in the Pubs database.
|
||||
|
||||
```vb
|
||||
Private Sub ResetSource()
|
||||
DataGrid1.SetDataBinding(adoPubsAuthors, "Authors")
|
||||
End Sub
|
||||
```
|
||||
|
||||
```csharp
|
||||
private void ResetSource()
|
||||
{
|
||||
DataGrid1.SetDataBinding(adoPubsAuthors, "Authors");
|
||||
}
|
||||
```
|
||||
|
||||
```cpp
|
||||
private:
|
||||
void ResetSource()
|
||||
{
|
||||
dataGrid1->SetDataBinding(adoPubsAuthors, "Authors");
|
||||
}
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [ADO.NET DataSets](https://docs.microsoft.com/dotnet/framework/data/adonet/ado-net-datasets)
|
||||
- [How to: Delete or Hide Columns in the Windows Forms DataGrid Control](how-to-delete-or-hide-columns-in-the-windows-forms-datagrid-control.md)
|
||||
- [How to: Add Tables and Columns to the Windows Forms DataGrid Control](how-to-add-tables-and-columns-to-the-windows-forms-datagrid-control.md)
|
||||
- [How to: Bind the Windows Forms DataGrid Control to a Data Source](how-to-bind-the-windows-forms-datagrid-control-to-a-data-source.md)
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
---
|
||||
title: Change the Border and Gridline Styles in DataGridView Control
|
||||
ms.date: "03/30/2017"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
helpviewer_keywords:
|
||||
- "gridlines [Windows Forms], changing styles"
|
||||
- "data grids [Windows Forms], changing gridline styles"
|
||||
- "DataGridView control [Windows Forms], border styles"
|
||||
- "data grids [Windows Forms], changing border styles"
|
||||
- "DataGridView control [Windows Forms], gridline styles"
|
||||
ms.assetid: 2f413c7a-4025-4171-8e3a-66ef908ea583
|
||||
---
|
||||
# How to: Change the Border and Gridline Styles in the Windows Forms DataGridView Control
|
||||
With the <xref:System.Windows.Forms.DataGridView> control, you can customize the appearance of the control's border and gridlines to improve the user experience. You can modify the gridline color and the control border style in addition to the border styles for the cells within the control. You can also apply different cell border styles for ordinary cells, row header cells, and column header cells.
|
||||
|
||||
> [!NOTE]
|
||||
> The gridline color is used only with the <xref:System.Windows.Forms.DataGridViewCellBorderStyle.Single>, <xref:System.Windows.Forms.DataGridViewCellBorderStyle.SingleHorizontal>, and <xref:System.Windows.Forms.DataGridViewCellBorderStyle.SingleVertical> values of the <xref:System.Windows.Forms.DataGridViewCellBorderStyle> enumeration and the <xref:System.Windows.Forms.DataGridViewHeaderBorderStyle.Single> value of the <xref:System.Windows.Forms.DataGridViewHeaderBorderStyle> enumeration. The other values of these enumerations use colors specified by the operating system. Additionally, when visual styles are enabled on Windows XP and the Windows Server 2003 family through the <xref:System.Windows.Forms.Application.EnableVisualStyles%2A?displayProperty=nameWithType> method, the <xref:System.Windows.Forms.DataGridView.GridColor%2A> property value is not used.
|
||||
|
||||
### To change the gridline color programmatically
|
||||
|
||||
- Set the <xref:System.Windows.Forms.DataGridView.GridColor%2A> property.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.DataGridViewMisc#031](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewMisc/CS/datagridviewmisc.cs#031)]
|
||||
[!code-vb[System.Windows.Forms.DataGridViewMisc#031](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewMisc/VB/datagridviewmisc.vb#031)]
|
||||
|
||||
### To change the border style of the entire DataGridView control programmatically
|
||||
|
||||
- Set the <xref:System.Windows.Forms.DataGridView.BorderStyle%2A> property to one of the <xref:System.Windows.Forms.BorderStyle> enumeration values.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.DataGridViewMisc#032](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewMisc/CS/datagridviewmisc.cs#032)]
|
||||
[!code-vb[System.Windows.Forms.DataGridViewMisc#032](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewMisc/VB/datagridviewmisc.vb#032)]
|
||||
|
||||
### To change the border styles for DataGridView cells programmatically
|
||||
|
||||
- Set the <xref:System.Windows.Forms.DataGridView.CellBorderStyle%2A>, <xref:System.Windows.Forms.DataGridView.RowHeadersBorderStyle%2A>, and <xref:System.Windows.Forms.DataGridView.ColumnHeadersBorderStyle%2A> properties.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.DataGridViewMisc#033](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewMisc/CS/datagridviewmisc.cs#033)]
|
||||
[!code-vb[System.Windows.Forms.DataGridViewMisc#033](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewMisc/VB/datagridviewmisc.vb#033)]
|
||||
|
||||
## Example
|
||||
[!code-csharp[System.Windows.Forms.DataGridViewMisc#030](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewMisc/CS/datagridviewmisc.cs#030)]
|
||||
[!code-vb[System.Windows.Forms.DataGridViewMisc#030](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewMisc/VB/datagridviewmisc.vb#030)]
|
||||
|
||||
## Compiling the Code
|
||||
This example requires:
|
||||
|
||||
- A <xref:System.Windows.Forms.DataGridView> control named `dataGridView1`.
|
||||
|
||||
- References to the <xref:System?displayProperty=nameWithType>, <xref:System.Windows.Forms?displayProperty=nameWithType>, and <xref:System.Drawing?displayProperty=nameWithType> assemblies.
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.BorderStyle>
|
||||
- <xref:System.Windows.Forms.DataGridView.BorderStyle%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridView.CellBorderStyle%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridView.ColumnHeadersBorderStyle%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridView.GridColor%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridView.RowHeadersBorderStyle%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridViewCellBorderStyle>
|
||||
- <xref:System.Windows.Forms.DataGridViewHeaderBorderStyle>
|
||||
- [Basic Formatting and Styling in the Windows Forms DataGridView Control](basic-formatting-and-styling-in-the-windows-forms-datagridview-control.md)
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
---
|
||||
title: Change the Order of Columns in DataGridView Control Using the Designer
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "columns [Windows Forms], order of"
|
||||
- "DataGridView control [Windows Forms], column order"
|
||||
- "Windows Forms, columns"
|
||||
- "data [Windows Forms], displaying"
|
||||
ms.assetid: 7fe52a98-75d6-448c-97a5-65ca2c568c1a
|
||||
---
|
||||
# How to: Change the Order of Columns in the Windows Forms DataGridView Control Using the Designer
|
||||
|
||||
When you bind a Windows Forms <xref:System.Windows.Forms.DataGridView> control to a data source, the display order of the automatically generated columns is dictated by the data source. If this order is not what you prefer, you can change the order of the columns using the designer. You may also want to add unbound columns to the control and change their display order. For information about how to change the column order programmatically, see [How to: Change the Order of Columns in the Windows Forms DataGridView Control](how-to-change-the-order-of-columns-in-the-windows-forms-datagridview-control.md).
|
||||
|
||||
The following procedure requires a **Windows Application** project with a form containing a <xref:System.Windows.Forms.DataGridView> control. For information about setting up such a project, see [How to: Create a Windows Forms application project](/visualstudio/ide/step-1-create-a-windows-forms-application-project) and [How to: Add Controls to Windows Forms](how-to-add-controls-to-windows-forms.md).
|
||||
|
||||
## To change the column order using the designer
|
||||
|
||||
1. Click the designer actions glyph () on the upper-right corner of the <xref:System.Windows.Forms.DataGridView> control, and then select **Edit Columns**.
|
||||
|
||||
2. Select a column from the **Selected Columns** list.
|
||||
|
||||
3. Click the up or down arrow to the right of the **Selected Columns** list until the selected column is in the position you want.
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridView>
|
||||
- [How to: Add and Remove Columns in the Windows Forms DataGridView Control Using the Designer](add-and-remove-columns-in-the-datagrid-using-the-designer.md)
|
||||
- [How to: Create a Windows Forms application project](/visualstudio/ide/step-1-create-a-windows-forms-application-project)
|
||||
- [How to: Add Controls to Windows Forms](how-to-add-controls-to-windows-forms.md)
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
---
|
||||
title: Change the Type of a DataGridView Column Using the Designer
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "Windows Forms, columns"
|
||||
- "columns [Windows Forms], types"
|
||||
- "DataGridView control [Windows Forms], changing column type"
|
||||
- "data [Windows Forms], displaying"
|
||||
ms.assetid: 7f994d45-600d-4190-a187-35803214b40c
|
||||
---
|
||||
# How to: Change the Type of a Windows Forms DataGridView Column Using the Designer
|
||||
Sometimes you will want to change the type of a column that has already been added to a Windows Forms <xref:System.Windows.Forms.DataGridView> control. For example, you may want to modify the types of some of the columns that are generated automatically when you bind the control to a data source. This is useful when the table you display has columns containing foreign keys to rows in a related table. In this case, you may want to replace the text box columns that display these foreign keys with combo box columns that display more meaningful values from the related table.
|
||||
|
||||
The following procedure requires a **Windows Application** project with a form containing a <xref:System.Windows.Forms.DataGridView> control. For information about setting up such a project, see [How to: Create a Windows Forms application project](/visualstudio/ide/step-1-create-a-windows-forms-application-project) and [How to: Add Controls to Windows Forms](how-to-add-controls-to-windows-forms.md).
|
||||
|
||||
### To change the type of a column using the designer
|
||||
|
||||
1. Click the designer actions glyph () on the upper-right corner of the <xref:System.Windows.Forms.DataGridView> control, and then select **Edit Columns**.
|
||||
|
||||
2. Select a column from the **Selected Columns** list.
|
||||
|
||||
3. In the **Column Properties** grid, set the `ColumnType` property to the new column type.
|
||||
|
||||
> [!NOTE]
|
||||
> The `ColumnType` property is a design-time-only property that indicates the class representing the column type. It is not an actual property defined in a column class.
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridView>
|
||||
- <xref:System.Windows.Forms.DataGridViewColumn>
|
||||
- [How to: Create a Windows Forms application project](/visualstudio/ide/step-1-create-a-windows-forms-application-project)
|
||||
- [How to: Add Controls to Windows Forms](how-to-add-controls-to-windows-forms.md)
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
---
|
||||
title: "CheckBox Control Overview"
|
||||
ms.date: "03/30/2017"
|
||||
f1_keywords:
|
||||
- "CheckBox"
|
||||
helpviewer_keywords:
|
||||
- "CheckBox control [Windows Forms], about CheckBox control"
|
||||
- "data binding [Windows Forms], checkbox controls"
|
||||
- "check boxes [Windows Forms], about check boxes"
|
||||
ms.assetid: 085a4e0b-9046-473f-b141-d0edddfb2ebb
|
||||
---
|
||||
# CheckBox Control Overview (Windows Forms)
|
||||
The Windows Forms <xref:System.Windows.Forms.CheckBox> control indicates whether a particular condition is on or off. It is commonly used to present a Yes/No or True/False selection to the user. You can use check box controls in groups to display multiple choices from which the user can select one or more.
|
||||
|
||||
The check box control is similar to the radio button control in that each is used to indicate a selection that is made by the user. They differ in that only one radio button in a group can be selected at a time. With the check box control, however, any number of check boxes may be selected.
|
||||
|
||||
A check box may be connected to elements in a database using simple data binding. Multiple check boxes may be grouped using the <xref:System.Windows.Forms.GroupBox> control. This is useful for visual appearance and also for user interface design, since grouped controls can be moved around together on the form designer. For more information, see [Windows Forms Data Binding](../windows-forms-data-binding.md) and [GroupBox Control](groupbox-control-windows-forms.md).
|
||||
|
||||
The <xref:System.Windows.Forms.CheckBox> control has two important properties, <xref:System.Windows.Forms.CheckBox.Checked%2A> and <xref:System.Windows.Forms.CheckBox.CheckState%2A>. The <xref:System.Windows.Forms.CheckBox.Checked%2A> property returns either `true` or `false`. The <xref:System.Windows.Forms.CheckBox.CheckState%2A> property returns either <xref:System.Windows.Forms.CheckState.Checked> or <xref:System.Windows.Forms.CheckState.Unchecked>; or, if the <xref:System.Windows.Forms.CheckBox.ThreeState%2A> property is set to `true`, <xref:System.Windows.Forms.CheckBox.CheckState%2A> may also return <xref:System.Windows.Forms.CheckState.Indeterminate>. In the indeterminate state, the box is displayed with a dimmed appearance to indicate the option is unavailable.
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.CheckBox>
|
||||
- [How to: Set Options with Windows Forms CheckBox Controls](how-to-set-options-with-windows-forms-checkbox-controls.md)
|
||||
- [How to: Respond to Windows Forms CheckBox Clicks](how-to-respond-to-windows-forms-checkbox-clicks.md)
|
||||
- [CheckBox Control](checkbox-control-windows-forms.md)
|
||||
@@ -0,0 +1,32 @@
|
||||
---
|
||||
title: "CheckBox Control"
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "checked list items [Windows Forms], Windows Forms controls"
|
||||
- "CheckBox control [Windows Forms]"
|
||||
- "list controls [Windows Forms], Windows Forms"
|
||||
- "list items [Windows Forms], Windows Forms controls that display"
|
||||
- "checked list items"
|
||||
- "check boxes"
|
||||
ms.assetid: 0719d798-c307-40d3-9040-72a62679c227
|
||||
---
|
||||
# CheckBox Control (Windows Forms)
|
||||
The Windows Forms `CheckBox` control indicates whether a particular condition is on or off. It is commonly used to present a Yes/No or True/False selection to the user. You can use check box controls in groups to display multiple choices from which the user can select one or more. It is similar to the <xref:System.Windows.Forms.RadioButton> control, but any number of grouped `CheckBox` controls may be selected.
|
||||
|
||||
## In This Section
|
||||
[CheckBox Control Overview](checkbox-control-overview-windows-forms.md)
|
||||
Explains what this control is and its key features and properties.
|
||||
|
||||
[How to: Respond to Windows Forms CheckBox Clicks](how-to-respond-to-windows-forms-checkbox-clicks.md)
|
||||
Explains how to use a check box to determine your application's actions.
|
||||
|
||||
[How to: Set Options with Windows Forms CheckBox Controls](how-to-set-options-with-windows-forms-checkbox-controls.md)
|
||||
Describes how to use a check box to set options such as properties of an object.
|
||||
|
||||
## Reference
|
||||
<xref:System.Windows.Forms.CheckBox> class
|
||||
Describes this class and has links to all its members.
|
||||
|
||||
## Related Sections
|
||||
[Controls to Use on Windows Forms](controls-to-use-on-windows-forms.md)
|
||||
Provides a complete list of Windows Forms controls, with links to information on their use.
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
---
|
||||
title: "CheckedListBox Control Overview"
|
||||
ms.date: "03/30/2017"
|
||||
f1_keywords:
|
||||
- "CheckedListBox"
|
||||
helpviewer_keywords:
|
||||
- "CheckedListBox control [Windows Forms], about CheckedListBox control"
|
||||
- "check marks"
|
||||
- "list boxes [Windows Forms], about list boxes"
|
||||
ms.assetid: e6cff3ce-e2ba-458e-851f-299e8e9c8abf
|
||||
---
|
||||
# CheckedListBox Control Overview (Windows Forms)
|
||||
The Windows Forms <xref:System.Windows.Forms.CheckedListBox> control extends the <xref:System.Windows.Forms.ListBox> control. It does almost everything that a list box does and also can display a check mark next to items in the list. Other differences between the two controls are that checked list boxes only support <xref:System.Windows.Forms.DrawMode.Normal?displayProperty=nameWithType>; and that checked list boxes can only have one item or none selected. Note that a selected item appears highlighted on the form and is not the same as a checked item.
|
||||
|
||||
Checked list boxes can have items added at design time using the **String Collection Editor** or their items can added dynamically from a collection at run time, using the <xref:System.Windows.Forms.CheckedListBox.Items%2A> property. For more information, see [How to: Add and Remove Items from a Windows Forms ComboBox, ListBox, or CheckedListBox Control](add-and-remove-items-from-a-wf-combobox.md).
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.CheckedListBox>
|
||||
- <xref:System.Windows.Forms.CheckedListBox.Items%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.ListControl.DataSource%2A?displayProperty=nameWithType>
|
||||
- [ListBox Control Overview](listbox-control-overview-windows-forms.md)
|
||||
- [Windows Forms Controls Used to List Options](windows-forms-controls-used-to-list-options.md)
|
||||
- [How to: Determine Checked Items in the Windows Forms CheckedListBox Control](how-to-determine-checked-items-in-the-windows-forms-checkedlistbox-control.md)
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
---
|
||||
title: "CheckedListBox Control"
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "CheckedListBox control [Windows Forms], reference"
|
||||
- "checked list items [Windows Forms], Windows Forms controls"
|
||||
- "list controls [Windows Forms], Windows Forms"
|
||||
- "CheckedListBox control [Windows Forms]"
|
||||
- "list box controls [Windows Forms]"
|
||||
- "check marks"
|
||||
- "list boxes [Windows Forms], displaying check marks"
|
||||
- "list boxes [Windows Forms], Windows Forms controls"
|
||||
- "list items [Windows Forms], Windows Forms controls that display"
|
||||
- "list boxes"
|
||||
ms.assetid: 2a525f72-0245-41d7-94e9-b9c971dc4484
|
||||
---
|
||||
# CheckedListBox Control (Windows Forms)
|
||||
The Windows Forms `CheckedListBox` control displays a list of items, like the <xref:System.Windows.Forms.ListBox> control, and also can display a check mark next to items in the list.
|
||||
|
||||
## In This Section
|
||||
[CheckedListBox Control Overview](checkedlistbox-control-overview-windows-forms.md)
|
||||
Explains what this control is and its key features and properties.
|
||||
|
||||
[How to: Determine Checked Items in the Windows Forms CheckedListBox Control](how-to-determine-checked-items-in-the-windows-forms-checkedlistbox-control.md)
|
||||
Describes how to step through a list to determine which items are checked.
|
||||
|
||||
## Reference
|
||||
<xref:System.Windows.Forms.CheckedListBox> class
|
||||
Describes this class and has links to all its members.
|
||||
|
||||
## Related Sections
|
||||
[Windows Forms Controls Used to List Options](windows-forms-controls-used-to-list-options.md)
|
||||
Provides a list of things you can do with list boxes, combo boxes, and checked list boxes.
|
||||
|
||||
[Controls to Use on Windows Forms](controls-to-use-on-windows-forms.md)
|
||||
Provides a complete list of Windows Forms controls, with links to information on their use.
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
---
|
||||
title: "ColorDialog Component Overview"
|
||||
ms.date: "03/30/2017"
|
||||
f1_keywords:
|
||||
- "ColorDialog"
|
||||
helpviewer_keywords:
|
||||
- "color dialog box [Windows Forms], about color dialog box"
|
||||
- "ColorDialog component [Windows Forms], about ColorDialog"
|
||||
ms.assetid: 6dbdd8f0-f697-4728-bb09-7ea156f6d800
|
||||
---
|
||||
# ColorDialog Component Overview (Windows Forms)
|
||||
The Windows Forms <xref:System.Windows.Forms.ColorDialog> component is a pre-configured dialog box that allows the user to select a color from a palette and to add custom colors to that palette. It is the same dialog box that you see in other Windows-based applications to select colors. Use it within your Windows-based application as a simple solution in lieu of configuring your own dialog box.
|
||||
|
||||
The color selected in the dialog box is returned in the <xref:System.Windows.Forms.ColorDialog.Color%2A> property. If the <xref:System.Windows.Forms.ColorDialog.AllowFullOpen%2A> property is set to `false`, the "Define Custom Colors" button is disabled and the user is restricted to the predefined colors in the palette. If the <xref:System.Windows.Forms.ColorDialog.SolidColorOnly%2A> property is set to `true`, the user cannot select dithered colors. To display the dialog box, you must call its <xref:System.Windows.Forms.CommonDialog.ShowDialog%2A> method.
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.ColorDialog>
|
||||
- [ColorDialog Component](colordialog-component-windows-forms.md)
|
||||
- [Dialog-Box Controls and Components](dialog-box-controls-and-components-windows-forms.md)
|
||||
- [How to: Change the Appearance of the Windows Forms ColorDialog Component](how-to-change-the-appearance-of-the-windows-forms-colordialog-component.md)
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
---
|
||||
title: "ColorDialog Component"
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "ColorDialog component"
|
||||
- "colors [Windows Forms], dialog box"
|
||||
- "color dialog box"
|
||||
ms.assetid: 261793e0-8252-47c5-b76c-621857b8da44
|
||||
---
|
||||
# ColorDialog Component (Windows Forms)
|
||||
The Windows Forms <xref:System.Windows.Forms.ColorDialog> component is a pre-configured dialog box that allows the user to select a color from a palette and to add custom colors to that palette. It is the same dialog box that you see in other Windows-based applications to select colors. Use it within your Windows-based application as a simple solution in lieu of configuring your own dialog box.
|
||||
|
||||
## In This Section
|
||||
[ColorDialog Component Overview](colordialog-component-overview-windows-forms.md)
|
||||
Introduces the general concepts of the <xref:System.Windows.Forms.ColorDialog> component, which allows you to display a pre-configured dialog box that users can use to select colors from a palette.
|
||||
|
||||
[How to: Change the Appearance of the Windows Forms ColorDialog Component](how-to-change-the-appearance-of-the-windows-forms-colordialog-component.md)
|
||||
Describes how to change the colors available to users and other properties.
|
||||
|
||||
[How to: Show a Color Palette with the ColorDialog Component](how-to-show-a-color-palette-with-the-colordialog-component.md)
|
||||
Explains how to select a color at run time by means of an instance of the <xref:System.Windows.Forms.ColorDialog> component.
|
||||
|
||||
## Related Sections
|
||||
[Controls You Can Use On Windows Forms](controls-to-use-on-windows-forms.md)
|
||||
Provides a complete list of Windows Forms controls, with links to information on their use.
|
||||
|
||||
<xref:System.Windows.Forms.ColorDialog>
|
||||
Provides reference information on the <xref:System.Windows.Forms.ColorDialog> class and its members.
|
||||
|
||||
[Dialog-Box Controls and Components](dialog-box-controls-and-components-windows-forms.md)
|
||||
Lists a set of controls that allow users to perform standard interactions with the application or system.
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
---
|
||||
title: Column Fill Mode in DataGridView Control
|
||||
description: Learn how the Windows Forms DataGridView control in column fill mode resizes its columns automatically so that they fill the width of the available display area.
|
||||
ms.date: "03/30/2017"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
helpviewer_keywords:
|
||||
- "data grids [Windows Forms], automatically resizing columns"
|
||||
- "DataGridView control [Windows Forms], column fill mode"
|
||||
- "data grids [Windows Forms], column fill mode"
|
||||
ms.assetid: b4ef7411-ebf4-4e26-bb33-aecec90de80c
|
||||
---
|
||||
# Column Fill Mode in the Windows Forms DataGridView Control
|
||||
In column fill mode, the <xref:System.Windows.Forms.DataGridView> control resizes its columns automatically so that they fill the width of the available display area. The control does not display the horizontal scroll bar except when it is necessary to keep the width of every column equal to or greater than its <xref:System.Windows.Forms.DataGridViewColumn.MinimumWidth%2A> property value.
|
||||
|
||||
The sizing behavior of each column depends on its <xref:System.Windows.Forms.DataGridViewColumn.InheritedAutoSizeMode%2A> property. The value of this property is inherited from the column's <xref:System.Windows.Forms.DataGridViewColumn.AutoSizeMode%2A> property or the control's <xref:System.Windows.Forms.DataGridView.AutoSizeColumnsMode%2A> property if the column value is <xref:System.Windows.Forms.DataGridViewAutoSizeColumnMode.NotSet> (the default value).
|
||||
|
||||
Each column can have a different size mode, but any columns with a size mode of <xref:System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill> will share the display-area width that is not used by the other columns. This width is divided among the fill-mode columns in proportions relative to their <xref:System.Windows.Forms.DataGridViewColumn.FillWeight%2A> property values. For example, if two columns have <xref:System.Windows.Forms.DataGridViewColumn.FillWeight%2A> values of 100 and 200, the first column will be half as wide as the second column.
|
||||
|
||||
## User Resizing in Fill Mode
|
||||
Unlike sizing modes that resize based on cell contents, fill mode does not prevent users from resizing columns that have <xref:System.Windows.Forms.DataGridViewColumn.Resizable%2A> property values of `true`. When a user resizes a fill-mode column, any fill-mode columns after the resized column (to the right if <xref:System.Windows.Forms.Control.RightToLeft%2A> is `false`; otherwise, to the left) are also resized to compensate for the change in the available width. If there are no fill-mode columns after the resized column, then all other fill-mode columns in the control are resized to compensate. If there are no other fill-mode columns in the control, the resize is ignored. If a column that is not in fill mode is resized, all fill-mode columns in the control change sizes to compensate.
|
||||
|
||||
After resizing a fill-mode column, the <xref:System.Windows.Forms.DataGridViewColumn.FillWeight%2A> values for all columns that changed are adjusted proportionally. For example, if four fill-mode columns have <xref:System.Windows.Forms.DataGridViewColumn.FillWeight%2A> values of 100, resizing the second column to half its original width will result in <xref:System.Windows.Forms.DataGridViewColumn.FillWeight%2A> values of 100, 50, 125, and 125. Resizing a column that is not in fill mode will not change any <xref:System.Windows.Forms.DataGridViewColumn.FillWeight%2A> values because the fill-mode columns will simply resize to compensate while retaining the same proportions.
|
||||
|
||||
## Content-Based FillWeight Adjustment
|
||||
You can initialize <xref:System.Windows.Forms.DataGridViewColumn.FillWeight%2A> values for fill-mode columns by using the <xref:System.Windows.Forms.DataGridView> automatic resizing methods, such as the <xref:System.Windows.Forms.DataGridView.AutoResizeColumns%2A> method. This method first calculates the widths required by columns to display their contents. Next, the control adjusts the <xref:System.Windows.Forms.DataGridViewColumn.FillWeight%2A> values for all fill-mode columns so that their proportions match the proportions of the calculated widths. Finally, the control resizes the fill-mode columns using the new <xref:System.Windows.Forms.DataGridViewColumn.FillWeight%2A> proportions so that all columns in the control fill the available horizontal space.
|
||||
|
||||
## Example
|
||||
|
||||
### Description
|
||||
By using appropriate values for the <xref:System.Windows.Forms.DataGridViewColumn.AutoSizeMode%2A>, <xref:System.Windows.Forms.DataGridViewColumn.MinimumWidth%2A>, <xref:System.Windows.Forms.DataGridViewColumn.FillWeight%2A>, and <xref:System.Windows.Forms.DataGridViewColumn.Resizable%2A> properties, you can customize the column-sizing behaviors for many different scenarios.
|
||||
|
||||
The following demonstration code enables you to experiment with different values for the <xref:System.Windows.Forms.DataGridViewColumn.AutoSizeMode%2A>, <xref:System.Windows.Forms.DataGridViewColumn.FillWeight%2A>, and <xref:System.Windows.Forms.DataGridViewColumn.MinimumWidth%2A> properties of different columns. In this example, a <xref:System.Windows.Forms.DataGridView> control is bound to its own <xref:System.Windows.Forms.DataGridView.Columns%2A> collection, and one column is bound to each of the <xref:System.Windows.Forms.DataGridViewColumn.HeaderText%2A>, <xref:System.Windows.Forms.DataGridViewColumn.AutoSizeMode%2A>, <xref:System.Windows.Forms.DataGridViewColumn.FillWeight%2A>, <xref:System.Windows.Forms.DataGridViewColumn.MinimumWidth%2A>, and <xref:System.Windows.Forms.DataGridViewColumn.Width%2A> properties. Each of the columns is also represented by a row in the control, and changing values in a row will update the properties of the corresponding column so that you can see how the values interact.
|
||||
|
||||
### Code
|
||||
[!code-csharp[System.Windows.Forms.DataGridViewFillColumnsDemo#00](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewFillColumnsDemo/CS/fillcolumns.cs#00)]
|
||||
[!code-vb[System.Windows.Forms.DataGridViewFillColumnsDemo#00](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewFillColumnsDemo/vb/fillcolumns.vb#00)]
|
||||
|
||||
### Comments
|
||||
To use this demonstration application:
|
||||
|
||||
- Change the size of the form. Observe how columns change their widths while retaining the proportions indicated by the <xref:System.Windows.Forms.DataGridViewColumn.FillWeight%2A> property values.
|
||||
|
||||
- Change the column sizes by dragging the column dividers with the mouse. Observe how the <xref:System.Windows.Forms.DataGridViewColumn.FillWeight%2A> values change.
|
||||
|
||||
- Change the <xref:System.Windows.Forms.DataGridViewColumn.MinimumWidth%2A> value for one column, then drag to resize the form. Observe how, when you make the form small enough, the <xref:System.Windows.Forms.DataGridViewColumn.Width%2A> values do not go below the <xref:System.Windows.Forms.DataGridViewColumn.MinimumWidth%2A> values.
|
||||
|
||||
- Change the <xref:System.Windows.Forms.DataGridViewColumn.MinimumWidth%2A> values for all columns to large numbers so that the combined values exceed the width of the control. Observe how the horizontal scroll bar appears.
|
||||
|
||||
- Change the <xref:System.Windows.Forms.DataGridViewColumn.AutoSizeMode%2A> values for some columns. Observe the effect when you resize columns or the form.
|
||||
|
||||
## Compiling the Code
|
||||
This example requires:
|
||||
|
||||
- References to the System, System.Drawing, and System.Windows.Forms assemblies.
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridView>
|
||||
- <xref:System.Windows.Forms.DataGridView.AutoResizeColumns%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridView.AutoSizeColumnsMode%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridViewAutoSizeColumnsMode>
|
||||
- <xref:System.Windows.Forms.DataGridViewColumn>
|
||||
- <xref:System.Windows.Forms.DataGridViewColumn.InheritedAutoSizeMode%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridViewColumn.AutoSizeMode%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridViewAutoSizeColumnMode>
|
||||
- <xref:System.Windows.Forms.DataGridViewColumn.FillWeight%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridViewColumn.MinimumWidth%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridViewColumn.Width%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridViewColumn.Resizable%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.Control.RightToLeft%2A?displayProperty=nameWithType>
|
||||
- [Resizing Columns and Rows in the Windows Forms DataGridView Control](resizing-columns-and-rows-in-the-windows-forms-datagridview-control.md)
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
---
|
||||
title: Column Sort Modes in DataGridView Control
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "data grids [Windows Forms], sort modes"
|
||||
- "DataGridView control [Windows Forms], sort mode"
|
||||
ms.assetid: 43715887-2df9-4da7-bcf1-b9c7c842b2bf
|
||||
---
|
||||
# Column Sort Modes in the Windows Forms DataGridView Control
|
||||
<xref:System.Windows.Forms.DataGridView> columns have three sort modes. The sort mode for each column is specified through the <xref:System.Windows.Forms.DataGridViewColumn.SortMode%2A> property of the column, which can be set to one of the following <xref:System.Windows.Forms.DataGridViewColumnSortMode> enumeration values.
|
||||
|
||||
|`DataGridViewColumnSortMode` value|Description|
|
||||
|----------------------------------------|-----------------|
|
||||
|<xref:System.Windows.Forms.DataGridViewColumnSortMode.Automatic>|Default for text box columns. Unless column headers are used for selection, clicking the column header automatically sorts the <xref:System.Windows.Forms.DataGridView> by this column and displays a glyph indicating the sort order.|
|
||||
|<xref:System.Windows.Forms.DataGridViewColumnSortMode.NotSortable>|Default for non–text box columns. You can sort this column programmatically; however, it is not intended for sorting, so no space is reserved for the sorting glyph.|
|
||||
|<xref:System.Windows.Forms.DataGridViewColumnSortMode.Programmatic>|You can sort this column programmatically, and space is reserved for the sorting glyph.|
|
||||
|
||||
You might want to change the sort mode for a column that defaults to <xref:System.Windows.Forms.DataGridViewColumnSortMode.NotSortable> if it contains values that can be meaningfully ordered. For example, if you have a database column containing numbers that represent item states, you can display these numbers as corresponding icons by binding an image column to the database column. You can then change the numerical cell values into image display values in a handler for the <xref:System.Windows.Forms.DataGridView.CellFormatting?displayProperty=nameWithType> event. In this case, setting the <xref:System.Windows.Forms.DataGridViewColumn.SortMode%2A> property to <xref:System.Windows.Forms.DataGridViewColumnSortMode.Automatic> will enable your users to sort the column. Automatic sorting will enable your users to group items that have the same state even if the states corresponding to the numbers do not have a natural sequence. Check box columns are another example where automatic sorting is useful for grouping items in the same state.
|
||||
|
||||
You can sort a <xref:System.Windows.Forms.DataGridView> programmatically by the values in any column or in multiple columns, regardless of the <xref:System.Windows.Forms.DataGridViewColumn.SortMode%2A> settings. Programmatic sorting is useful when you want to provide your own user interface (UI) for sorting or when you want to implement custom sorting. Providing your own sorting UI is useful, for example, when you set the <xref:System.Windows.Forms.DataGridView> selection mode to enable column header selection. In this case, although the column headers cannot be used for sorting, you still want the headers to display the appropriate sorting glyph, so you would set the <xref:System.Windows.Forms.DataGridViewColumn.SortMode%2A> property to <xref:System.Windows.Forms.DataGridViewColumnSortMode.Programmatic>.
|
||||
|
||||
Columns set to programmatic sort mode do not automatically display a sorting glyph. For these columns, you must display the glyph yourself by setting the <xref:System.Windows.Forms.DataGridViewColumnHeaderCell.SortGlyphDirection%2A?displayProperty=nameWithType> property. This is necessary if you want flexibility in custom sorting. For example, if you sort the <xref:System.Windows.Forms.DataGridView> by multiple columns, you might want to display multiple sorting glyphs or no sorting glyph.
|
||||
|
||||
Although you can programmatically sort a <xref:System.Windows.Forms.DataGridView> by any column, some columns, such as button columns, might not contain values that can be meaningfully ordered. For these columns, a <xref:System.Windows.Forms.DataGridViewColumn.SortMode%2A> property setting of <xref:System.Windows.Forms.DataGridViewColumnSortMode.NotSortable> indicates that it will never be used for sorting, so there is no need to reserve space in the header for the sorting glyph.
|
||||
|
||||
When a <xref:System.Windows.Forms.DataGridView> is sorted, you can determine both the sort column and the sort order by checking the values of the <xref:System.Windows.Forms.DataGridView.SortedColumn%2A> and <xref:System.Windows.Forms.DataGridView.SortOrder%2A> properties. These values are not meaningful after a custom sorting operation. For more information about custom sorting, see the Custom Sorting section later in this topic.
|
||||
|
||||
When a <xref:System.Windows.Forms.DataGridView> control containing both bound and unbound columns is sorted, the values in the unbound columns cannot be maintained automatically. To maintain these values, you must implement virtual mode by setting the <xref:System.Windows.Forms.DataGridView.VirtualMode%2A> property to `true` and handling the <xref:System.Windows.Forms.DataGridView.CellValueNeeded> and <xref:System.Windows.Forms.DataGridView.CellValuePushed> events. For more information, see [How to: Implement Virtual Mode in the Windows Forms DataGridView Control](how-to-implement-virtual-mode-in-the-windows-forms-datagridview-control.md). Sorting by unbound columns in bound mode is not supported.
|
||||
|
||||
## Programmatic Sorting
|
||||
You can sort a <xref:System.Windows.Forms.DataGridView> programmatically by calling its <xref:System.Windows.Forms.DataGridView.Sort%2A> method.
|
||||
|
||||
The `Sort(DataGridViewColumn,ListSortDirection)` overload of the <xref:System.Windows.Forms.DataGridView.Sort%2A> method takes a <xref:System.Windows.Forms.DataGridViewColumn> and a <xref:System.ComponentModel.ListSortDirection> enumeration value as parameters. This overload is useful when sorting by columns with values that can be meaningfully ordered, but which you do not want to configure for automatic sorting. When you call this overload and pass in a column with a <xref:System.Windows.Forms.DataGridViewColumn.SortMode%2A> property value of <xref:System.Windows.Forms.DataGridViewColumnSortMode.Automatic?displayProperty=nameWithType>, the <xref:System.Windows.Forms.DataGridView.SortedColumn%2A> and <xref:System.Windows.Forms.DataGridView.SortOrder%2A> properties are set automatically and the appropriate sorting glyph appears in the column header.
|
||||
|
||||
> [!NOTE]
|
||||
> When the <xref:System.Windows.Forms.DataGridView> control is bound to an external data source by setting the <xref:System.Windows.Forms.DataGridView.DataSource%2A> property, the `Sort(DataGridViewColumn,ListSortDirection)` method overload does not work for unbound columns. Additionally, when the <xref:System.Windows.Forms.DataGridView.VirtualMode%2A> property is `true`, you can call this overload only for bound columns. To determine whether a column is data-bound, check the <xref:System.Windows.Forms.DataGridViewColumn.IsDataBound%2A> property value. Sorting unbound columns in bound mode is not supported.
|
||||
|
||||
## Custom Sorting
|
||||
You can customize <xref:System.Windows.Forms.DataGridView> by using the `Sort(IComparer)` overload of the <xref:System.Windows.Forms.DataGridView.Sort%2A> method or by handling the <xref:System.Windows.Forms.DataGridView.SortCompare> event.
|
||||
|
||||
The `Sort(IComparer)` method overload takes an instance of a class that implements the <xref:System.Collections.IComparer> interface as a parameter. This overload is useful when you want to provide custom sorting; for example, when the values in a column do not have a natural sort order or when the natural sort order is inappropriate. In this case, you cannot use automatic sorting, but you might still want your users to sort by clicking the column headers. You can call this overload in a handler for the <xref:System.Windows.Forms.DataGridView.ColumnHeaderMouseClick> event if you do not use column headers for selection.
|
||||
|
||||
> [!NOTE]
|
||||
> The `Sort(IComparer)` method overload works only when the <xref:System.Windows.Forms.DataGridView> control is not bound to an external data source and the <xref:System.Windows.Forms.DataGridView.VirtualMode%2A> property value is `false`. To customize sorting for columns bound to an external data source, you must use the sorting operations provided by the data source. In virtual mode, you must provide your own sorting operations for unbound columns.
|
||||
|
||||
To use the `Sort(IComparer)` method overload, you must create your own class that implements the <xref:System.Collections.IComparer> interface. This interface requires your class to implement the <xref:System.Collections.IComparer.Compare%2A?displayProperty=nameWithType> method, to which the <xref:System.Windows.Forms.DataGridView> passes <xref:System.Windows.Forms.DataGridViewRow> objects as input when the `Sort(IComparer)` method overload is called. With this, you can calculate the correct row ordering based on the values in any column.
|
||||
|
||||
The `Sort(IComparer)` method overload does not set the <xref:System.Windows.Forms.DataGridView.SortedColumn%2A> and <xref:System.Windows.Forms.DataGridView.SortOrder%2A> properties, so you must always set the <xref:System.Windows.Forms.DataGridViewColumnHeaderCell.SortGlyphDirection%2A?displayProperty=nameWithType> property to display the sorting glyph.
|
||||
|
||||
As an alternative to the `Sort(IComparer)` method overload, you can provide custom sorting by implementing a handler for the <xref:System.Windows.Forms.DataGridView.SortCompare> event. This event occurs when users click the headers of columns configured for automatic sorting or when you call the `Sort(DataGridViewColumn,ListSortDirection)` overload of the <xref:System.Windows.Forms.DataGridView.Sort%2A> method. The event occurs for each pair of rows in the control, enabling you to calculate their correct order.
|
||||
|
||||
> [!NOTE]
|
||||
> The <xref:System.Windows.Forms.DataGridView.SortCompare> event does not occur when the <xref:System.Windows.Forms.DataGridView.DataSource%2A> property is set or when the <xref:System.Windows.Forms.DataGridView.VirtualMode%2A> property value is `true`.
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridView>
|
||||
- <xref:System.Windows.Forms.DataGridView.Sort%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridView.SortedColumn%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridView.SortOrder%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridViewColumn.SortMode%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridViewColumnHeaderCell.SortGlyphDirection%2A?displayProperty=nameWithType>
|
||||
- [Sorting Data in the Windows Forms DataGridView Control](sorting-data-in-the-windows-forms-datagridview-control.md)
|
||||
- [How to: Set the Sort Modes for Columns in the Windows Forms DataGridView Control](set-the-sort-modes-for-columns-wf-datagridview-control.md)
|
||||
- [How to: Customize Sorting in the Windows Forms DataGridView Control](how-to-customize-sorting-in-the-windows-forms-datagridview-control.md)
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
---
|
||||
title: Column Types in DataGridView Control
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "columns [Windows Forms], types"
|
||||
- "DataGridView control [Windows Forms], column types"
|
||||
- "data grids [Windows Forms], columns"
|
||||
ms.assetid: f0a0a9f1-8757-4bfd-891f-d7d12870dbed
|
||||
---
|
||||
# Column Types in the Windows Forms DataGridView Control
|
||||
The <xref:System.Windows.Forms.DataGridView> control uses several column types to display its information and enable users to modify or add information.
|
||||
|
||||
When you bind a <xref:System.Windows.Forms.DataGridView> control and set the <xref:System.Windows.Forms.DataGridView.AutoGenerateColumns%2A> property to `true`, columns are automatically generated using default column types appropriate for the data types contained in the bound data source.
|
||||
|
||||
You can also create instances of any of the column classes yourself and add them to the collection returned by the <xref:System.Windows.Forms.DataGridView.Columns%2A> property. You can create these instances for use as unbound columns, or you can manually bind them. Manually bound columns are useful, for example, when you want to replace an automatically generated column of one type with a column of another type.
|
||||
|
||||
The following table describes the various column classes available for use in the <xref:System.Windows.Forms.DataGridView> control.
|
||||
|
||||
|Class|Description|
|
||||
|-----------|-----------------|
|
||||
|<xref:System.Windows.Forms.DataGridViewTextBoxColumn>|Used with text-based values. Generated automatically when binding to numbers and strings.|
|
||||
|<xref:System.Windows.Forms.DataGridViewCheckBoxColumn>|Used with <xref:System.Boolean> and <xref:System.Windows.Forms.CheckState> values. Generated automatically when binding to values of these types.|
|
||||
|<xref:System.Windows.Forms.DataGridViewImageColumn>|Used to display images. Generated automatically when binding to byte arrays, <xref:System.Drawing.Image> objects, or <xref:System.Drawing.Icon> objects.|
|
||||
|<xref:System.Windows.Forms.DataGridViewButtonColumn>|Used to display buttons in cells. Not automatically generated when binding. Typically used as unbound columns.|
|
||||
|<xref:System.Windows.Forms.DataGridViewComboBoxColumn>|Used to display drop-down lists in cells. Not automatically generated when binding. Typically data-bound manually.|
|
||||
|<xref:System.Windows.Forms.DataGridViewLinkColumn>|Used to display links in cells. Not automatically generated when binding. Typically data-bound manually.|
|
||||
|Your custom column type|You can create your own column class by inheriting the <xref:System.Windows.Forms.DataGridViewColumn> class or any of its derived classes to provide custom appearance, behavior, or hosted controls. For more information, see [How to: Customize Cells and Columns in the Windows Forms DataGridView Control by Extending Their Behavior and Appearance](customize-cells-and-columns-in-the-datagrid-by-extending-behavior.md)|
|
||||
|
||||
These column types are described in more detail in the following sections.
|
||||
|
||||
## DataGridViewTextBoxColumn
|
||||
The <xref:System.Windows.Forms.DataGridViewTextBoxColumn> is a general-purpose column type for use with text-based values such as numbers and strings. In editing mode, a <xref:System.Windows.Forms.TextBox> control is displayed in the active cell, enabling users to modify the cell value.
|
||||
|
||||
Cell values are automatically converted to strings for display. Values entered or modified by the user are automatically parsed to create a cell value of the appropriate data type. You can customize these conversions by handling the <xref:System.Windows.Forms.DataGridView.CellFormatting> and <xref:System.Windows.Forms.DataGridView.CellParsing> events of the <xref:System.Windows.Forms.DataGridView> control.
|
||||
|
||||
The cell value data type of a column is specified in the <xref:System.Windows.Forms.DataGridViewColumn.ValueType%2A> property of the column.
|
||||
|
||||
## DataGridViewCheckBoxColumn
|
||||
The <xref:System.Windows.Forms.DataGridViewCheckBoxColumn> is used with <xref:System.Boolean> and <xref:System.Windows.Forms.CheckState> values. <xref:System.Boolean> values display as two-state or three-state check boxes, depending on the value of the <xref:System.Windows.Forms.DataGridViewCheckBoxColumn.ThreeState%2A> property. When the column is bound to <xref:System.Windows.Forms.CheckState> values, the <xref:System.Windows.Forms.DataGridViewCheckBoxColumn.ThreeState%2A> property value is `true` by default.
|
||||
|
||||
Typically, check box cell values are intended either for storage, like any other data, or for performing bulk operations. If you want to respond immediately when users click a check box cell, you can handle the <xref:System.Windows.Forms.DataGridView.CellClick> event, but this event occurs before the cell value is updated. If you need the new value at the time of the click, one option is to calculate what the expected value will be based on the current value. Another approach is to commit the change immediately, and handle the <xref:System.Windows.Forms.DataGridView.CellValueChanged> event to respond to it. To commit the change when the cell is clicked, you must handle the <xref:System.Windows.Forms.DataGridView.CurrentCellDirtyStateChanged> event. In the handler, if the current cell is a check box cell, call the <xref:System.Windows.Forms.DataGridView.CommitEdit%2A> method and pass in the <xref:System.Windows.Forms.DataGridViewDataErrorContexts.Commit> value.
|
||||
|
||||
## DataGridViewImageColumn
|
||||
The <xref:System.Windows.Forms.DataGridViewImageColumn> is used to display images. Image columns can be populated automatically from a data source, populated manually for unbound columns, or populated dynamically in a handler for the <xref:System.Windows.Forms.DataGridView.CellFormatting> event.
|
||||
|
||||
The automatic population of an image column from a data source works with byte arrays in a variety of image formats, including all formats supported by the <xref:System.Drawing.Image> class and the OLE Picture format used by Microsoft® Access and the Northwind sample database.
|
||||
|
||||
Populating an image column manually is useful when you want to provide the functionality of a <xref:System.Windows.Forms.DataGridViewButtonColumn>, but with a customized appearance. You can handle the <xref:System.Windows.Forms.DataGridView.CellClick?displayProperty=nameWithType> event to respond to clicks within an image cell.
|
||||
|
||||
Populating the cells of an image column in a handler for the <xref:System.Windows.Forms.DataGridView.CellFormatting> event is useful when you want to provide images for calculated values or values in non-image formats. For example, you may have a "Risk" column with string values such as `"high"`, `"middle"`, and `"low"` that you want to display as icons. Alternately, you may have an "Image" column that contains the locations of images that must be loaded rather than the binary content of the images.
|
||||
|
||||
## DataGridViewButtonColumn
|
||||
With the <xref:System.Windows.Forms.DataGridViewButtonColumn>, you can display a column of cells that contain buttons. This is useful when you want to provide an easy way for your users to perform actions on particular records, such as placing an order or displaying child records in a separate window.
|
||||
|
||||
Button columns are not generated automatically when data-binding a <xref:System.Windows.Forms.DataGridView> control. To use button columns, you must create them manually and add them to the collection returned by the <xref:System.Windows.Forms.DataGridView.Columns%2A?displayProperty=nameWithType> property.
|
||||
|
||||
You can respond to user clicks in button cells by handling the <xref:System.Windows.Forms.DataGridView.CellClick?displayProperty=nameWithType> event.
|
||||
|
||||
## DataGridViewComboBoxColumn
|
||||
With the <xref:System.Windows.Forms.DataGridViewComboBoxColumn>, you can display a column of cells that contain drop-down list boxes. This is useful for data entry in fields that can only contain particular values, such as the Category column of the Products table in the Northwind sample database.
|
||||
|
||||
You can populate the drop-down list used for all cells the same way you would populate a <xref:System.Windows.Forms.ComboBox> drop-down list, either manually through the collection returned by the <xref:System.Windows.Forms.DataGridViewComboBoxColumn.Items%2A> property, or by binding it to a data source through the <xref:System.Windows.Forms.DataGridViewComboBoxColumn.DataSource%2A>, <xref:System.Windows.Forms.DataGridViewComboBoxColumn.DisplayMember%2A>, and <xref:System.Windows.Forms.DataGridViewComboBoxColumn.ValueMember%2A> properties. For more information, see [ComboBox Control](combobox-control-windows-forms.md).
|
||||
|
||||
You can bind the actual cell values to the data source used by the <xref:System.Windows.Forms.DataGridView> control by setting the <xref:System.Windows.Forms.DataGridViewColumn.DataPropertyName%2A> property of the <xref:System.Windows.Forms.DataGridViewComboBoxColumn?displayProperty=nameWithType>.
|
||||
|
||||
Combo box columns are not generated automatically when data-binding a <xref:System.Windows.Forms.DataGridView> control. To use combo box columns, you must create them manually and add them to the collection returned by the <xref:System.Windows.Forms.DataGridView.Columns%2A> property.
|
||||
|
||||
## DataGridViewLinkColumn
|
||||
With the <xref:System.Windows.Forms.DataGridViewLinkColumn>, you can display a column of cells that contain hyperlinks. This is useful for URL values in the data source or as an alternative to the button column for special behaviors such as opening a window with child records.
|
||||
|
||||
Link columns are not generated automatically when data-binding a <xref:System.Windows.Forms.DataGridView> control. To use link columns, you must create them manually and add them to the collection returned by the <xref:System.Windows.Forms.DataGridView.Columns%2A> property.
|
||||
|
||||
You can respond to user clicks on links by handling the <xref:System.Windows.Forms.DataGridView.CellContentClick> event. This event is distinct from the <xref:System.Windows.Forms.DataGridView.CellClick> and <xref:System.Windows.Forms.DataGridView.CellMouseClick> events, which occur when a user clicks anywhere in a cell.
|
||||
|
||||
The <xref:System.Windows.Forms.DataGridViewLinkColumn> class provides several properties for modifying the appearance of links before, during, and after they are clicked.
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridView>
|
||||
- <xref:System.Windows.Forms.DataGridViewColumn>
|
||||
- <xref:System.Windows.Forms.DataGridViewButtonColumn>
|
||||
- <xref:System.Windows.Forms.DataGridViewCheckBoxColumn>
|
||||
- <xref:System.Windows.Forms.DataGridViewComboBoxColumn>
|
||||
- <xref:System.Windows.Forms.DataGridViewImageColumn>
|
||||
- <xref:System.Windows.Forms.DataGridViewTextBoxColumn>
|
||||
- <xref:System.Windows.Forms.DataGridViewLinkColumn>
|
||||
- [DataGridView Control](datagridview-control-windows-forms.md)
|
||||
- [How to: Display Images in Cells of the Windows Forms DataGridView Control](how-to-display-images-in-cells-of-the-windows-forms-datagridview-control.md)
|
||||
- [How to: Work with Image Columns in the Windows Forms DataGridView Control](how-to-work-with-image-columns-in-the-windows-forms-datagridview-control.md)
|
||||
- [Customizing the Windows Forms DataGridView Control](customizing-the-windows-forms-datagridview-control.md)
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
---
|
||||
title: "ComboBox Control Overview"
|
||||
ms.date: "03/30/2017"
|
||||
f1_keywords:
|
||||
- "ComboBox"
|
||||
helpviewer_keywords:
|
||||
- "drop-down lists [Windows Forms], Windows Forms"
|
||||
- "ComboBox control [Windows Forms], about ComboBox control"
|
||||
- "drop-down lists [Windows Forms], ComboBox control"
|
||||
- "combo boxes [Windows Forms], about combo boxes"
|
||||
ms.assetid: a58b393f-a614-45d1-8961-857a024b5acd
|
||||
---
|
||||
# ComboBox Control Overview (Windows Forms)
|
||||
The Windows Forms <xref:System.Windows.Forms.ComboBox> control is used to display data in a drop-down combo box. By default, the <xref:System.Windows.Forms.ComboBox> control appears in two parts: the top part is a text box that allows the user to type a list item. The second part is a list box that displays a list of items from which the user can select one. For more information on other styles of combo box, see [When to Use a Windows Forms ComboBox Instead of a ListBox](when-to-use-a-windows-forms-combobox-instead-of-a-listbox.md).
|
||||
|
||||
The <xref:System.Windows.Forms.ComboBox.SelectedIndex%2A> property returns an integer value that corresponds to the selected list item. You can programmatically change the selected item by changing the <xref:System.Windows.Forms.ComboBox.SelectedIndex%2A> value in code; the corresponding item in the list will appear in the text box portion of the combo box. If no item is selected, the <xref:System.Windows.Forms.ComboBox.SelectedIndex%2A> value is -1. If the first item in the list is selected, then the <xref:System.Windows.Forms.ComboBox.SelectedIndex%2A> value is 0. The <xref:System.Windows.Forms.ComboBox.SelectedItem%2A> property is similar to <xref:System.Windows.Forms.ComboBox.SelectedIndex%2A> , but returns the item itself, usually a string value. The <xref:System.Windows.Forms.ComboBox.ObjectCollection.Count%2A> property reflects the number of items in the list, and the value of the <xref:System.Windows.Forms.ComboBox.ObjectCollection.Count%2A> property is always one more than the largest possible <xref:System.Windows.Forms.ComboBox.SelectedIndex%2A> value because <xref:System.Windows.Forms.ComboBox.SelectedIndex%2A> is zero-based.
|
||||
|
||||
To add or delete items in a <xref:System.Windows.Forms.ComboBox> control, use the <xref:System.Windows.Forms.ComboBox.ObjectCollection.Add%2A>, <xref:System.Windows.Forms.ComboBox.ObjectCollection.Insert%2A>, <xref:System.Windows.Forms.ComboBox.ObjectCollection.Clear%2A> or <xref:System.Windows.Forms.ComboBox.ObjectCollection.Remove%2A> method. Alternatively, you can add items to the list by using the <xref:System.Windows.Forms.ComboBox.Items%2A> property in the designer.
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.ComboBox>
|
||||
- [ListBox Control Overview](listbox-control-overview-windows-forms.md)
|
||||
- [When to Use a Windows Forms ComboBox Instead of a ListBox](when-to-use-a-windows-forms-combobox-instead-of-a-listbox.md)
|
||||
- [How to: Add and Remove Items from a Windows Forms ComboBox, ListBox, or CheckedListBox Control](add-and-remove-items-from-a-wf-combobox.md)
|
||||
- [How to: Sort the Contents of a Windows Forms ComboBox, ListBox, or CheckedListBox Control](sort-the-contents-of-a-wf-combobox-listbox-or-checkedlistbox-control.md)
|
||||
- [How to: Access Specific Items in a Windows Forms ComboBox, ListBox, or CheckedListBox Control](access-specific-items-in-a-wf-combobox-listbox-or-checkedlistbox.md)
|
||||
- [How to: Bind a Windows Forms ComboBox or ListBox Control to Data](how-to-bind-a-windows-forms-combobox-or-listbox-control-to-data.md)
|
||||
- [Windows Forms Controls Used to List Options](windows-forms-controls-used-to-list-options.md)
|
||||
- [How to: Create a Lookup Table for a Windows Forms ComboBox, ListBox, or CheckedListBox Control](create-a-lookup-table-for-a-wf-combobox-listbox.md)
|
||||
@@ -0,0 +1,34 @@
|
||||
---
|
||||
title: "ComboBox Control"
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "drop-down lists [Windows Forms], Windows Forms"
|
||||
- "list controls [Windows Forms], Windows Forms"
|
||||
- "combo boxes [Windows Forms], Windows Forms controls"
|
||||
- "combo boxes"
|
||||
- "list items [Windows Forms], Windows Forms controls that display"
|
||||
- "drop-down lists [Windows Forms], ComboBox control"
|
||||
- "ComboBox control [Windows Forms]"
|
||||
ms.assetid: 86a1bbc3-0469-4290-91c5-99ff61b739ff
|
||||
---
|
||||
# ComboBox Control (Windows Forms)
|
||||
The Windows Forms `ComboBox` control is used to display data in a drop-down combo box. By default, the `ComboBox` control appears in two parts: the top part is a text box that allows the user to type a list item. The second part is a list box that displays a list of items from which the user can select one.
|
||||
|
||||
## In This Section
|
||||
[ComboBox Control Overview](combobox-control-overview-windows-forms.md)
|
||||
Explains what this control is and its key features and properties.
|
||||
|
||||
[How to: Create Variable Sized Text in a ComboBox Control](how-to-create-variable-sized-text-in-a-combobox-control.md)
|
||||
Demonstrates custom drawing of text in a `ComboBox` control.
|
||||
|
||||
## Reference
|
||||
<xref:System.Windows.Forms.ComboBox> class
|
||||
Describes this class and has links to all its members.
|
||||
|
||||
## Related Sections
|
||||
[Windows Forms Controls Used to List Options](windows-forms-controls-used-to-list-options.md)
|
||||
Provides a list of things you can do with list boxes, combo boxes, and checked list boxes.
|
||||
|
||||
## See also
|
||||
|
||||
- [Controls to Use on Windows Forms](controls-to-use-on-windows-forms.md)
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
---
|
||||
title: "Considerations When Hosting an ActiveX Control on a Windows Form"
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "Windows Forms controls, ActiveX controls"
|
||||
- "ActiveX controls [Windows Forms], hosting"
|
||||
- "Windows Forms, ActiveX controls"
|
||||
- "Windows Forms, hosting ActiveX controls"
|
||||
- "ActiveX controls [Windows Forms], adding"
|
||||
ms.assetid: 2509302d-a74e-484f-9890-2acdbfa67a68
|
||||
---
|
||||
# Considerations When Hosting an ActiveX Control on a Windows Form
|
||||
Although Windows Forms have been optimized to host Windows Forms controls, you can still use ActiveX controls. Keep the following considerations in mind when planning an application that uses ActiveX controls:
|
||||
|
||||
- **Security** The common language runtime has been enhanced with regard to code access security. Applications featuring Windows Forms can run in a fully trusted environment without issue and in a semi-trusted environment with most of the functionality accessible. Windows Forms controls can be hosted in a browser with no complications. However, ActiveX controls on Windows Forms cannot take advantage of these security enhancements. Running an ActiveX control requires unmanaged code permission, which is set with the <xref:System.Security.Permissions.SecurityPermissionAttribute.UnmanagedCode%2A?displayProperty=nameWithType> property. For more information about security and unmanaged code permission, see <xref:System.Security.Permissions.SecurityPermissionAttribute>.
|
||||
|
||||
- **Total Cost of Ownership** ActiveX controls added to a Windows Form are deployed with that Windows Form in their entirety, which can add significantly to the size of the file(s) created. Additionally, using ActiveX controls on Windows Forms requires writing to the registry. This is more invasive to a user's computer than Windows Forms controls, which do not require this.
|
||||
|
||||
> [!NOTE]
|
||||
> Working with an ActiveX control requires the use of a COM interop wrapper. For more information, see [COM Interoperability in Visual Basic and Visual C#](https://docs.microsoft.com/dotnet/visual-basic/programming-guide/com-interop/com-interoperability-in-net-framework-applications).
|
||||
|
||||
> [!NOTE]
|
||||
> If the name of a member of the ActiveX control matches a name defined in the .NET Framework, then the ActiveX Control Importer will prefix the member name with **Ctl** when it creates the <xref:System.Windows.Forms.AxHost> derived class. For example, if your ActiveX control has a member named **Layout**, it is renamed **CtlLayout** in the AxHost-derived class because the **Layout** event is defined within the .NET Framework.
|
||||
|
||||
## See also
|
||||
|
||||
- [How to: Add ActiveX Controls to Windows Forms](how-to-add-activex-controls-to-windows-forms.md)
|
||||
- [Code Access Security](https://docs.microsoft.com/dotnet/framework/misc/code-access-security)
|
||||
- [Controls and Programmable Objects Compared in Various Languages and Libraries](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2010/0061wezk(v=vs.100))
|
||||
- [Putting Controls on Windows Forms](putting-controls-on-windows-forms.md)
|
||||
- [Windows Forms Controls](index.md)
|
||||
@@ -0,0 +1,46 @@
|
||||
---
|
||||
title: "Constituent Controls"
|
||||
ms.date: "03/30/2017"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
helpviewer_keywords:
|
||||
- "custom controls [Windows Forms], constituent controls"
|
||||
- "constituent controls [Windows Forms]"
|
||||
- "user controls [Windows Forms], constituent controls"
|
||||
ms.assetid: 5565e720-198b-4bbd-a2bd-c447ba641798
|
||||
---
|
||||
# Constituent Controls
|
||||
The controls that make up a user control, or *constituent controls* as they are termed, are relatively inflexible when it comes to custom graphics rendering. All Windows Forms controls handle their own rendering through their own <xref:System.Windows.Forms.Control.OnPaint%2A> method. Because this method is protected, it is not accessible to the developer, and thus cannot be prevented from executing when the control is painted. This does not mean, however, that you cannot add code to affect the appearance of constituent controls. Additional rendering can be accomplished by adding an event handler. For example, suppose you were authoring a <xref:System.Windows.Forms.UserControl> with a button named `MyButton`. If you wished to have additional rendering beyond what was provided by the <xref:System.Web.UI.WebControls.Button>, you would add code to your user control similar to the following:
|
||||
|
||||
```vb
|
||||
Public Sub MyPaint(ByVal sender as Object, e as PaintEventArgs) Handles _
|
||||
MyButton.Paint
|
||||
'Additional rendering code goes here
|
||||
End Sub
|
||||
```
|
||||
|
||||
```csharp
|
||||
// Add the event handler to the button's Paint event.
|
||||
MyButton.Paint +=
|
||||
new System.Windows.Forms.PaintEventHandler (this.MyPaint);
|
||||
// Create the custom painting method.
|
||||
protected void MyPaint (object sender,
|
||||
System.Windows.Forms.PaintEventArgs e)
|
||||
{
|
||||
// Additional rendering code goes here.
|
||||
}
|
||||
```
|
||||
|
||||
> [!NOTE]
|
||||
> Some Windows Forms controls, such as <xref:System.Windows.Forms.TextBox>, are painted directly by Windows. In these instances, the <xref:System.Windows.Forms.Control.OnPaint%2A> method is never called, and thus the above example will never be called.
|
||||
|
||||
This creates a method that executes every time the `MyButton.Paint` event executes, thereby adding additional graphical representation to your control. Note that this does not prevent the execution of `MyButton.OnPaint`, and thus all of the painting usually performed by a button will still be performed in addition to your custom painting. For details about GDI+ technology and custom rendering, see the [Creating Graphical Images with GDI+](../advanced/how-to-create-graphics-objects-for-drawing.md). If you wish to have a unique representation of your control, your best course of action is to create an inherited control, and to write custom rendering code for it. For details, see [User-Drawn Controls](user-drawn-controls.md).
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.UserControl>
|
||||
- <xref:System.Windows.Forms.Control.OnPaint%2A>
|
||||
- [User-Drawn Controls](user-drawn-controls.md)
|
||||
- [How to: Create Graphics Objects for Drawing](../advanced/how-to-create-graphics-objects-for-drawing.md)
|
||||
- [Varieties of Custom Controls](varieties-of-custom-controls.md)
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
---
|
||||
title: "ContextMenu Component Overview"
|
||||
ms.date: "03/30/2017"
|
||||
f1_keywords:
|
||||
- "ContextMenu"
|
||||
helpviewer_keywords:
|
||||
- "ContextMenu component [Windows Forms], about ContextMenu component"
|
||||
- "context menus [Windows Forms], ContextMenu component"
|
||||
- "shortcut menus [Windows Forms], ContextMenu component"
|
||||
ms.assetid: 49d6398f-d3c4-4679-84fa-1de07b68b05e
|
||||
---
|
||||
# ContextMenu Component Overview (Windows Forms)
|
||||
> [!IMPORTANT]
|
||||
> Although <xref:System.Windows.Forms.MenuStrip> and <xref:System.Windows.Forms.ContextMenuStrip> replace and add functionality to the <xref:System.Windows.Forms.MainMenu> and <xref:System.Windows.Forms.ContextMenu> controls of previous versions, <xref:System.Windows.Forms.MainMenu> and <xref:System.Windows.Forms.ContextMenu> are retained for both backward compatibility and future use if you choose.
|
||||
|
||||
With the Windows Forms <xref:System.Windows.Forms.ContextMenu> component, you can provide users with an easily accessible shortcut menu of frequently used commands that are associated with the selected object. The items in a shortcut menu are frequently a subset of the items from main menus that appear elsewhere in the application. A user can typically access a shortcut menu by right-clicking the mouse. On Windows Forms, shortcut menus are associated with controls.
|
||||
|
||||
## Key Properties
|
||||
You can associate a shortcut menu with a control by setting the control's <xref:System.Windows.Forms.Control.ContextMenu%2A> property to the <xref:System.Windows.Forms.ContextMenu> component. A single shortcut menu can be associated with multiple controls, but each control can have only one shortcut menu.
|
||||
|
||||
The key property of the <xref:System.Windows.Forms.ContextMenu> component is the <xref:System.Windows.Forms.Menu.MenuItems%2A> property. You can add menu items by programmatically creating <xref:System.Windows.Forms.MenuItem> objects and adding them to the <xref:System.Windows.Forms.Menu.MenuItemCollection> of the shortcut menu. Because the items in a shortcut menu are usually drawn from other menus, you will most frequently add items to a shortcut menu by copying them.
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.ContextMenu>
|
||||
- <xref:System.Windows.Forms.MenuStrip>
|
||||
- <xref:System.Windows.Forms.ContextMenuStrip>
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
---
|
||||
title: "ContextMenu Component"
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "menus [Windows Forms], shortcut menus"
|
||||
- "Windows Forms, menus"
|
||||
- "pop-up menus"
|
||||
- "ContextMenu component [Windows Forms]"
|
||||
- "menus [Windows Forms], context menus"
|
||||
- "context menus [Windows Forms], for Windows Forms"
|
||||
- "shortcut menus [Windows Forms], ContextMenu component"
|
||||
ms.assetid: 9b93ccbc-c0fe-44a2-968e-faac25971831
|
||||
---
|
||||
# ContextMenu Component (Windows Forms)
|
||||
> [!NOTE]
|
||||
> Although `MenuStrip` and `ContextMenuStrip` replace and add functionality to the <xref:System.Windows.Forms.MainMenu> and <xref:System.Windows.Forms.ContextMenu> controls of previous versions, <xref:System.Windows.Forms.MainMenu> and <xref:System.Windows.Forms.ContextMenu> are retained for both backward compatibility and future use if you choose.
|
||||
|
||||
The Windows Forms `ContextMenu` component is used to provide users with an easily accessible shortcut menu of frequently used commands that are associated with the selected object. The items in a shortcut menu are frequently a subset of the items from main menus that appear elsewhere in the application. Shortcut menus are usually available by right-clicking the mouse. On Windows Forms they are associated with other controls.
|
||||
|
||||
## In This Section
|
||||
[ContextMenu Component Overview](contextmenu-component-overview-windows-forms.md)
|
||||
Introduces the general concepts of the `ContextMenu` component, which allows users to create menus of frequently used commands associated with a selected object.
|
||||
|
||||
[How to: Add and Remove Menu Items with the Windows Forms ContextMenu Component](add-and-remove-menu-items-with-wf-contextmenu-component.md)
|
||||
Explains how to add and remove shortcut menu items in Windows Forms.
|
||||
|
||||
## Reference
|
||||
<xref:System.Windows.Forms.ContextMenu>
|
||||
Provides reference information on the <xref:System.Windows.Forms.ContextMenu> class and its members.
|
||||
|
||||
## Related Sections
|
||||
[Controls to Use on Windows Forms](controls-to-use-on-windows-forms.md)
|
||||
Provides a complete list of Windows Forms controls, with links to information on their use.
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.MenuStrip>
|
||||
- <xref:System.Windows.Forms.ContextMenuStrip>
|
||||
@@ -0,0 +1,34 @@
|
||||
---
|
||||
title: "ContextMenuStrip Control Overview"
|
||||
ms.date: "03/30/2017"
|
||||
f1_keywords:
|
||||
- "ContextMenuStrip"
|
||||
helpviewer_keywords:
|
||||
- "context menus [Windows Forms], ContextMenuStrip control [Windows Forms]"
|
||||
- "shortcut menus [Windows Forms], ContextMenuStrip control [Windows Forms]"
|
||||
- "ContextMenuStrip control [Windows Forms], about ContextMenuStrip control"
|
||||
ms.assetid: 9787cdb3-88f1-4198-972f-eefd9524ce39
|
||||
---
|
||||
# ContextMenuStrip Control Overview
|
||||
> [!NOTE]
|
||||
> The <xref:System.Windows.Forms.ContextMenuStrip> control replaces and adds functionality to the <xref:System.Windows.Forms.ContextMenu> control; however, the <xref:System.Windows.Forms.ContextMenu> control is retained for backward compatibility and future use if you choose.
|
||||
|
||||
Shortcut menus, also called context menus, appear at the mouse position when the user clicks the right mouse button. Shortcut *menus* provide options for the client area or the control at the mouse pointer location.
|
||||
|
||||
The <xref:System.Windows.Forms.ContextMenuStrip> control is designed to work seamlessly with the new <xref:System.Windows.Forms.ToolStrip> and related controls, but you can associate a <xref:System.Windows.Forms.ContextMenuStrip> with other controls just as easily.
|
||||
|
||||
The following table shows the important <xref:System.Windows.Forms.ContextMenuStrip> companion classes.
|
||||
|
||||
|Class|Description|
|
||||
|-----------|-----------------|
|
||||
|<xref:System.Windows.Forms.ToolStripMenuItem>|Represents a selectable option displayed on a <xref:System.Windows.Forms.MenuStrip> or <xref:System.Windows.Forms.ContextMenuStrip>.|
|
||||
|<xref:System.Windows.Forms.ToolStripDropDown>|Represents a control that enables the user to select a single item from a list that is displayed when the user clicks a <xref:System.Windows.Forms.ToolStripDropDownButton> or a higher-level menu item.|
|
||||
|<xref:System.Windows.Forms.ToolStripDropDownItem>|Provides basic functionality for controls derived from <xref:System.Windows.Forms.ToolStripItem> that display drop-down items when clicked.|
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.ToolStrip>
|
||||
- <xref:System.Windows.Forms.MenuStrip>
|
||||
- <xref:System.Windows.Forms.ContextMenuStrip>
|
||||
- <xref:System.Windows.Forms.ToolStripMenuItem>
|
||||
- <xref:System.Windows.Forms.ToolStripDropDown>
|
||||
@@ -0,0 +1,46 @@
|
||||
---
|
||||
title: "ContextMenuStrip Control"
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "context menus"
|
||||
- "ContextMenuStrip control [Windows Forms]"
|
||||
- "shortcut menus"
|
||||
ms.assetid: 76e070b2-65d7-457f-8300-d104e4e01e5e
|
||||
---
|
||||
# ContextMenuStrip Control
|
||||
The <xref:System.Windows.Forms.ContextMenuStrip> control provides a shortcut menu that you associate with a control.
|
||||
|
||||
## In This Section
|
||||
[ContextMenuStrip Control Overview](contextmenustrip-control-overview.md)
|
||||
Explains what the control is and its key features and properties.
|
||||
|
||||
[How to: Associate a ContextMenuStrip with a Control](how-to-associate-a-contextmenustrip-with-a-control.md)
|
||||
Describes making a <xref:System.Windows.Forms.ContextMenuStrip> the shortcut menu for a specific control.
|
||||
|
||||
[How to: Add Menu Items to a ContextMenuStrip](how-to-add-menu-items-to-a-contextmenustrip.md)
|
||||
Describes how to add selectable options to a <xref:System.Windows.Forms.ContextMenuStrip>.
|
||||
|
||||
[How to: Configure ContextMenuStrip Check Margins and Image Margins](how-to-configure-contextmenustrip-check-margins-and-image-margins.md)
|
||||
Describes how to customize a <xref:System.Windows.Forms.ContextMenuStrip> by setting check and image margin properties in various ways.
|
||||
|
||||
[How to: Enable Check Margins and Image Margins in ContextMenuStrip Controls](how-to-enable-check-margins-and-image-margins-in-contextmenustrip-controls.md)
|
||||
Describes how to turn <xref:System.Windows.Forms.ContextMenuStrip> check margins on and off.
|
||||
|
||||
[How to: Handle the ContextMenuStrip Opening Event](how-to-handle-the-contextmenustrip-opening-event.md)
|
||||
Describes how to customize the behavior of a <xref:System.Windows.Forms.ContextMenuStrip> control by handling the <xref:System.Windows.Forms.ToolStripDropDown.Opening> event.
|
||||
|
||||
Also see [ContextMenuStrip Tasks Dialog Box](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2010/ms233646(v=vs.100)) or [ContextMenuStrip Items Collection Editor](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2010/ms233641(v=vs.100)).
|
||||
|
||||
## Reference
|
||||
<xref:System.Windows.Forms.MenuStrip>
|
||||
Describes the features of the <xref:System.Windows.Forms.MenuStrip> class, which provides a menu system for a form.
|
||||
|
||||
<xref:System.Windows.Forms.ContextMenuStrip>
|
||||
Describes the features of the <xref:System.Windows.Forms.ContextMenuStrip>, which represents a shortcut menu.
|
||||
|
||||
<xref:System.Windows.Forms.ToolStripMenuItem>
|
||||
Describes the features of the <xref:System.Windows.Forms.ToolStripMenuItem> class, which represents a selectable option displayed on a <xref:System.Windows.Forms.MenuStrip> or <xref:System.Windows.Forms.ContextMenuStrip>.
|
||||
|
||||
## Related Sections
|
||||
[Controls to Use on Windows Forms](controls-to-use-on-windows-forms.md)
|
||||
Provides a complete list of Windows Forms controls, with links to information on their use.
|
||||
@@ -0,0 +1,81 @@
|
||||
---
|
||||
title: "Control Type Recommendations"
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "inheritance [Windows Forms], Windows Forms custom controls"
|
||||
- "user controls [Windows Forms], when to use"
|
||||
- "custom controls [Windows Forms], types"
|
||||
- "controls [Windows Forms], creating"
|
||||
ms.assetid: 5235fe9d-c36a-4c08-ae76-6cb90b50085e
|
||||
---
|
||||
# Control Type Recommendations
|
||||
|
||||
The .NET Framework gives you power to develop and implement new controls. In addition to the familiar user control, you will now find that you are able to write custom controls that perform their own painting, and are even able to extend the functionality of existing controls through inheritance. Deciding which type of control to create can be confusing. This section highlights the differences between the various types of controls from which you can inherit, and gives considerations regarding the type to choose for your project.
|
||||
|
||||
> [!NOTE]
|
||||
> If you want to author a control to use on Web Forms, see [Developing Custom ASP.NET Server Controls](https://docs.microsoft.com/previous-versions/aspnet/zt27tfhy(v=vs.100)).
|
||||
|
||||
## Inheriting from a Windows Forms Control
|
||||
|
||||
You can derive an inherited control from any existing Windows Forms control. This approach allows you to retain all of the inherent functionality of a Windows Forms control, and to then extend that functionality by adding custom properties, methods, or other functionality. For example, you might create a control derived from <xref:System.Windows.Forms.TextBox> that can accept only numbers and automatically converts input into a value. Such a control might contain validation code that was called whenever the text in the text box changed, and could have an additional property, Value. In some controls, you can also add a custom appearance to the graphical interface of your control by overriding the <xref:System.Windows.Forms.Control.OnPaint%2A> method of the base class.
|
||||
|
||||
Inherit from a Windows Forms control if:
|
||||
|
||||
- Most of the functionality you need is already identical to an existing Windows Forms control.
|
||||
|
||||
- You do not need a custom graphical interface, or you want to design a new graphical front end for an existing control.
|
||||
|
||||
## Inheriting from the UserControl Class
|
||||
|
||||
A user control is a collection of Windows Forms controls encapsulated into a common container. The container holds all of the inherent functionality associated with each of the Windows Forms controls and allows you to selectively expose and bind their properties. An example of a user control might be a control built to display customer address data from a database. This control would include several textboxes to display each field, and button controls to navigate through the records. Data-binding properties could be selectively exposed, and the entire control could be packaged and reused from application to application.
|
||||
|
||||
Inherit from the <xref:System.Windows.Forms.UserControl> class if:
|
||||
|
||||
- You want to combine the functionality of several Windows Forms controls into a single reusable unit.
|
||||
|
||||
## Inheriting from the Control Class
|
||||
|
||||
Another way to create a control is to create one substantially from scratch by inheriting from <xref:System.Windows.Forms.Control>. The <xref:System.Windows.Forms.Control> class provides all of the basic functionality required by controls (for example, events), but no control-specific functionality or graphical interface. Creating a control by inheriting from the <xref:System.Windows.Forms.Control> class requires a lot more thought and effort than inheriting from user control or an existing Windows Forms control. The author must write code for the <xref:System.Windows.Forms.Control.OnPaint%2A> event of the control, as well as any functionality specific code that is needed. Greater flexibility is allowed, however, and you can custom tailor a control to suit your exact needs. An example of a custom control is a clock control that duplicates the look and action of an analog clock. Custom painting would be invoked to cause the hands of the clock to move in response to <xref:System.Windows.Forms.Timer.Tick> events from an internal timer component.
|
||||
|
||||
Inherit from the <xref:System.Windows.Forms.Control> class if:
|
||||
|
||||
- You want to provide a custom graphical representation of your control.
|
||||
|
||||
- You need to implement custom functionality that is not available through standard controls.
|
||||
|
||||
## Related articles
|
||||
|
||||
- [How to: Display a Control in the Choose Toolbox Items Dialog Box](how-to-display-a-control-in-the-choose-toolbox-items-dialog-box.md)
|
||||
|
||||
- [Walkthrough: Serializing Collections of Standard Types with the DesignerSerializationVisibilityAttribute](serializing-collections-designerserializationvisibilityattribute.md)
|
||||
|
||||
- [Walkthrough: Inheriting from a Windows Forms Control](walkthrough-inheriting-from-a-windows-forms-control-with-visual-csharp.md)
|
||||
|
||||
- [How to: Provide a Toolbox Bitmap for a Control](how-to-provide-a-toolbox-bitmap-for-a-control.md)
|
||||
|
||||
- [How to: Inherit from Existing Windows Forms Controls](how-to-inherit-from-existing-windows-forms-controls.md)
|
||||
|
||||
- [Walkthrough: Debugging Custom Windows Forms Controls at Design Time](walkthrough-debugging-custom-windows-forms-controls-at-design-time.md)
|
||||
|
||||
- [How to: Inherit from the Control Class](how-to-inherit-from-the-control-class.md)
|
||||
|
||||
- [How to: Test the Run-Time Behavior of a UserControl](how-to-test-the-run-time-behavior-of-a-usercontrol.md)
|
||||
|
||||
- [How to: Align a Control to the Edges of Forms at Design Time](how-to-align-a-control-to-the-edges-of-forms-at-design-time.md)
|
||||
|
||||
- [How to: Inherit from the UserControl Class](how-to-inherit-from-the-usercontrol-class.md)
|
||||
|
||||
- [How to: Author Controls for Windows Forms](how-to-author-controls-for-windows-forms.md)
|
||||
|
||||
- [How to: Author Composite Controls](how-to-author-composite-controls.md)
|
||||
|
||||
- [Walkthrough: Authoring a Composite Control](walkthrough-authoring-a-composite-control-with-visual-csharp.md)
|
||||
|
||||
- [Walkthrough: Creating a Windows Forms Control That Takes Advantage of Visual Studio Design-Time Features](creating-a-wf-control-design-time-features.md)
|
||||
|
||||
- [How to: Create a Windows Forms Control That Takes Advantage of Design-Time Features](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2013/307hck25(v=vs.120))
|
||||
|
||||
## See also
|
||||
|
||||
- [How to: Develop a Simple Windows Forms Control](how-to-develop-a-simple-windows-forms-control.md)
|
||||
- [Varieties of Custom Controls](varieties-of-custom-controls.md)
|
||||
@@ -0,0 +1,232 @@
|
||||
---
|
||||
title: Controls to Use
|
||||
description: Learn about controls and components that can be used on Windows Forms. You can also add ActiveX and custom controls to Windows Forms.
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "controls [Windows Forms]"
|
||||
- "Windows Forms controls, list of"
|
||||
ms.assetid: dec51976-eae0-4398-a537-20bca7974722
|
||||
---
|
||||
# Controls to Use on Windows Forms
|
||||
The following is an alphabetic list of controls and components that can be used on Windows Forms. In addition to the Windows Forms controls covered in this section, you can add ActiveX and custom controls to Windows Forms. If you do not find the control you need listed here, you can also create your own. For details, see [Developing Windows Forms Controls at Design Time](developing-windows-forms-controls-at-design-time.md). For more information about choosing the control you need, see [Windows Forms Controls by Function](windows-forms-controls-by-function.md).
|
||||
|
||||
> [!NOTE]
|
||||
> Visual Basic controls are based on classes provided by the .NET Framework.
|
||||
|
||||
## In This Section
|
||||
[Windows Forms Controls by Function](windows-forms-controls-by-function.md)
|
||||
Lists and describes Windows Forms controls based on the .NET Framework.
|
||||
|
||||
[Controls with Built-In Owner-Drawing Support](controls-with-built-in-owner-drawing-support.md)
|
||||
Describes how to alter aspects of a control's appearance that are not available through properties.
|
||||
|
||||
[BackgroundWorker Component](backgroundworker-component.md)
|
||||
Enables a form or control to run an operation asynchronously.
|
||||
|
||||
[BindingNavigator Control](bindingnavigator-control-windows-forms.md)
|
||||
Provides the navigation and manipulation user interface (UI) for controls that are bound to data.
|
||||
|
||||
[BindingSource Component](bindingsource-component.md)
|
||||
Encapsulates a data source for binding to controls.
|
||||
|
||||
[Button Control](button-control-windows-forms.md)
|
||||
Presents a standard button that the user can click to perform actions.
|
||||
|
||||
[CheckBox Control](checkbox-control-windows-forms.md)
|
||||
Indicates whether a condition is on or off.
|
||||
|
||||
[CheckedListBox Control](checkedlistbox-control-windows-forms.md)
|
||||
Displays a list of items with a check box next to each item.
|
||||
|
||||
[ColorDialog Component](colordialog-component-windows-forms.md)
|
||||
Allows the user to select a color from a palette in a pre-configured dialog box and to add custom colors to that palette.
|
||||
|
||||
[ComboBox Control](combobox-control-windows-forms.md)
|
||||
Displays data in a drop-down combo box.
|
||||
|
||||
[ContextMenu Component](contextmenu-component-windows-forms.md)
|
||||
Provides users with an easily accessible menu of frequently used commands that are associated with the selected object. Although <xref:System.Windows.Forms.ContextMenuStrip> replaces and adds functionality to the <xref:System.Windows.Forms.ContextMenu> control of previous versions, <xref:System.Windows.Forms.ContextMenu> is retained for both backward compatibility and future use if so desired.
|
||||
|
||||
[ContextMenuStrip Control](contextmenustrip-control.md)
|
||||
Represents a shortcut menu. Although <xref:System.Windows.Forms.ContextMenuStrip> replaces and adds functionality to the <xref:System.Windows.Forms.ContextMenu> control of previous versions, <xref:System.Windows.Forms.ContextMenu> is retained for both backward compatibility and future use if so desired.
|
||||
|
||||
[DataGrid Control](datagrid-control-windows-forms.md)
|
||||
Displays tabular data from a dataset and allows for updates to the data source.
|
||||
|
||||
[DataGridView Control](datagridview-control-windows-forms.md)
|
||||
Provides a flexible, extensible system for displaying and editing tabular data.
|
||||
|
||||
[DateTimePicker Control](datetimepicker-control-windows-forms.md)
|
||||
Allows the user to select a single item from a list of dates or times.
|
||||
|
||||
[Dialog-Box Controls and Components](dialog-box-controls-and-components-windows-forms.md)
|
||||
Describes a set of controls that allow users to perform standard interactions with the application or system.
|
||||
|
||||
[DomainUpDown Control](domainupdown-control-windows-forms.md)
|
||||
Displays text strings that a user can browse through and select from.
|
||||
|
||||
[ErrorProvider Component](errorprovider-component-windows-forms.md)
|
||||
Displays error information to the user in a non-intrusive way.
|
||||
|
||||
[FileDialog Class](filedialog-class.md)
|
||||
Provides base-class functionality for file dialog boxes.
|
||||
|
||||
[FlowLayoutPanel Control](flowlayoutpanel-control-windows-forms.md)
|
||||
Represents a panel that dynamically lays out its contents horizontally or vertically.
|
||||
|
||||
[FolderBrowserDialog Component](folderbrowserdialog-component-windows-forms.md)
|
||||
Displays an interface with which users can browse and select a directory or create a new one.
|
||||
|
||||
[FontDialog Component](fontdialog-component-windows-forms.md)
|
||||
Exposes the fonts that are currently installed on the system.
|
||||
|
||||
[GroupBox Control](groupbox-control-windows-forms.md)
|
||||
Provides an identifiable grouping for other controls.
|
||||
|
||||
[HelpProvider Component](helpprovider-component-windows-forms.md)
|
||||
Associates an HTML Help file with a Windows-based application.
|
||||
|
||||
[HScrollBar and VScrollBar Controls](hscrollbar-and-vscrollbar-controls-windows-forms.md)
|
||||
Provide navigation through a list of items or a large amount of information by scrolling either horizontally or vertically within an application or control.
|
||||
|
||||
[ImageList Component](imagelist-component-windows-forms.md)
|
||||
Displays images on other controls.
|
||||
|
||||
[Label Control](label-control-windows-forms.md)
|
||||
Displays text that cannot be edited by the user.
|
||||
|
||||
[LinkLabel Control](linklabel-control-windows-forms.md)
|
||||
Allows you to add Web-style links to Windows Forms applications.
|
||||
|
||||
[ListBox Control](listbox-control-windows-forms.md)
|
||||
Allows the user to select one or more items from a predefined list.
|
||||
|
||||
[ListView Control](listview-control-windows-forms.md)
|
||||
Displays a list of items with icons, in the manner of Windows Explorer.
|
||||
|
||||
[MainMenu Component](mainmenu-component-windows-forms.md)
|
||||
Displays a menu at run time. Although <xref:System.Windows.Forms.MenuStrip> replaces and adds functionality to the <xref:System.Windows.Forms.MainMenu> control of previous versions, <xref:System.Windows.Forms.MainMenu> is retained for both backward compatibility and future use if you choose.
|
||||
|
||||
[MaskedTextBox Control](maskedtextbox-control-windows-forms.md)
|
||||
Constrains the format of user input in a form.
|
||||
|
||||
[MenuStrip Control](menustrip-control-windows-forms.md)
|
||||
Provides a menu system for a form. Although <xref:System.Windows.Forms.MenuStrip> replaces and adds functionality to the <xref:System.Windows.Forms.MainMenu> control of previous versions, <xref:System.Windows.Forms.MainMenu> is retained for both backward compatibility and future use if you choose.
|
||||
|
||||
[MonthCalendar Control](monthcalendar-control-windows-forms.md)
|
||||
Presents an intuitive graphical interface for users to view and set date information.
|
||||
|
||||
[NotifyIcon Component](notifyicon-component-windows-forms.md)
|
||||
Displays icons for processes that run in the background and would not otherwise have user interfaces.
|
||||
|
||||
[NumericUpDown Control](numericupdown-control-windows-forms.md)
|
||||
Displays numerals that a user can browse through and select from.
|
||||
|
||||
[OpenFileDialog Component](openfiledialog-component-windows-forms.md)
|
||||
Allows users to open files by using a pre-configured dialog box.
|
||||
|
||||
[PageSetupDialog Component](pagesetupdialog-component-windows-forms.md)
|
||||
Sets page details for printing through a pre-configured dialog box.
|
||||
|
||||
[Panel Control](panel-control-windows-forms.md)
|
||||
Provide an identifiable grouping for other controls, and allows for scrolling.
|
||||
|
||||
[PictureBox Control](picturebox-control-windows-forms.md)
|
||||
Displays graphics in bitmap, GIF, JPEG, metafile, or icon format.
|
||||
|
||||
[PrintDialog Component](printdialog-component-windows-forms.md)
|
||||
Selects a printer, chooses the pages to print, and determines other print-related settings.
|
||||
|
||||
[PrintDocument Component](printdocument-component-windows-forms.md)
|
||||
Sets the properties that describe what to print, and prints the document in Windows-based applications.
|
||||
|
||||
[PrintPreviewControl Control](printpreviewcontrol-control-windows-forms.md)
|
||||
Allows you to create your own `PrintPreview` component or dialog box instead of using the pre-configured version.
|
||||
|
||||
[PrintPreviewDialog Control](printpreviewdialog-control-windows-forms.md)
|
||||
Displays a document as it will appear when it is printed.
|
||||
|
||||
[ProgressBar Control](progressbar-control-windows-forms.md)
|
||||
Graphically indicates the progress of an action towards completion.
|
||||
|
||||
[RadioButton Control](radiobutton-control-windows-forms.md)
|
||||
Presents a set of two or more mutually exclusive options to the user.
|
||||
|
||||
[RichTextBox Control](richtextbox-control-windows-forms.md)
|
||||
Allows users to enter, display, and manipulate text with formatting.
|
||||
|
||||
[SaveFileDialog Component](savefiledialog-component-windows-forms.md)
|
||||
Selects files to save and where to save them.
|
||||
|
||||
[SoundPlayer Class](soundplayer-class.md)
|
||||
Enables you to easily include sounds in your applications.
|
||||
|
||||
[SplitContainer Control](splitcontainer-control-windows-forms.md)
|
||||
Allows the user to resize a docked control.
|
||||
|
||||
[Splitter Control](splitter-control-windows-forms.md)
|
||||
Allows the user to resize a docked control (.NET Framework version 1.x).
|
||||
|
||||
[StatusBar Control](statusbar-control-windows-forms.md)
|
||||
Displays status information related to the control that has focus. Although <xref:System.Windows.Forms.StatusStrip> replaces and extends the <xref:System.Windows.Forms.StatusBar> control of previous versions, <xref:System.Windows.Forms.StatusBar> is retained for both backward compatibility and future use if you choose.
|
||||
|
||||
[StatusStrip Control](statusstrip-control.md)
|
||||
Represents a Windows status bar control. Although <xref:System.Windows.Forms.StatusStrip> replaces and extends the <xref:System.Windows.Forms.StatusBar> control of previous versions, <xref:System.Windows.Forms.StatusBar> is retained for both backward compatibility and future use if you choose.
|
||||
|
||||
[TabControl Control](tabcontrol-control-windows-forms.md)
|
||||
Displays multiple tabs that can contain pictures or other controls.
|
||||
|
||||
[TableLayoutPanel Control](tablelayoutpanel-control-windows-forms.md)
|
||||
Represents a panel that dynamically lays out its contents in a grid composed of rows and columns.
|
||||
|
||||
[TextBox Control](textbox-control-windows-forms.md)
|
||||
Allows editable, multiline input from the user.
|
||||
|
||||
[Timer Component](timer-component-windows-forms.md)
|
||||
Raises an event at regular intervals.
|
||||
|
||||
[ToolBar Control](toolbar-control-windows-forms.md)
|
||||
Displays menus and bitmapped buttons that activate commands. You can extend the functionality of the control and modify its appearance and behavior. Although <xref:System.Windows.Forms.ToolStrip> replaces and adds functionality to the <xref:System.Windows.Forms.ToolBar> control of previous versions, <xref:System.Windows.Forms.ToolBar> is retained for both backward compatibility and future use if you choose.
|
||||
|
||||
[ToolStrip Control](toolstrip-control-windows-forms.md)
|
||||
Creates custom toolbars and menus in your Windows Forms applications. Although <xref:System.Windows.Forms.ToolStrip> replaces and adds functionality to the <xref:System.Windows.Forms.ToolBar> control of previous versions, <xref:System.Windows.Forms.ToolBar> is retained for both backward compatibility and future use if you choose.
|
||||
|
||||
[ToolStripContainer Control](toolstripcontainer-control.md)
|
||||
Provides panels on each side of a form for docking, rafting, and arranging <xref:System.Windows.Forms.ToolStrip> controls, and a central <xref:System.Windows.Forms.ToolStripContentPanel> for traditional controls.
|
||||
|
||||
[ToolStripPanel Control](toolstrippanel-control.md)
|
||||
Provides one panel for docking, rafting and arranging <xref:System.Windows.Forms.ToolStrip> controls.
|
||||
|
||||
[ToolStripProgressBar Control Overview](toolstripprogressbar-control-overview.md)
|
||||
Graphically indicates the progress of an action towards completion. The <xref:System.Windows.Forms.ToolStripProgressBar> is typically contained in a <xref:System.Windows.Forms.StatusStrip>.
|
||||
|
||||
[ToolStripStatusLabel Control](toolstripstatuslabel-control.md)
|
||||
Represents a panel in a <xref:System.Windows.Forms.StatusStrip> control.
|
||||
|
||||
[ToolTip Component](tooltip-component-windows-forms.md)
|
||||
Displays text when the user points at other controls.
|
||||
|
||||
[TrackBar Control](trackbar-control-windows-forms.md)
|
||||
Allows navigation through a large amount of information or visually adjusting a numeric setting.
|
||||
|
||||
[TreeView Control](treeview-control-windows-forms.md)
|
||||
Displays a hierarchy of nodes that can be expanded or collapsed.
|
||||
|
||||
[WebBrowser Control](webbrowser-control-windows-forms.md)
|
||||
Hosts Web pages and provides Internet Web browsing capabilities to your application.
|
||||
|
||||
[Windows Forms Controls Used to List Options](windows-forms-controls-used-to-list-options.md)
|
||||
Describes a set of controls used to provide users with a list of options to choose from.
|
||||
|
||||
## Related Sections
|
||||
[Windows Forms Controls](index.md)
|
||||
Explains the use of Windows Forms controls, and describes important concepts for working with them.
|
||||
|
||||
[Developing Windows Forms Controls at Design Time](developing-windows-forms-controls-at-design-time.md)
|
||||
Provides links to step-by-step topics, recommendations for which kind of control to create, and other information about creating your own control.
|
||||
|
||||
[Controls and Programmable Objects Compared in Various Languages and Libraries](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2010/0061wezk(v=vs.100))
|
||||
Provides a table that maps controls in Visual Basic 6.0 to the corresponding control in Visual Basic .NET. Note that controls are now classes in the .NET Framework.
|
||||
|
||||
[How to: Add ActiveX Controls to Windows Forms](how-to-add-activex-controls-to-windows-forms.md)
|
||||
Describes how to use ActiveX controls on Windows Forms.
|
||||
+188
@@ -0,0 +1,188 @@
|
||||
---
|
||||
title: "Controls with Built-In Owner-Drawing Support"
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "drawing [Windows Forms], owner"
|
||||
- "drawing [Windows Forms], custom"
|
||||
- "controls [Windows Forms], changing appearance"
|
||||
- "custom drawing"
|
||||
- "owner drawing"
|
||||
ms.assetid: 3823d01e-9610-43e6-864d-99f9b7c2b351
|
||||
---
|
||||
# Controls with Built-In Owner-Drawing Support
|
||||
Owner drawing in Windows Forms, which is also known as custom drawing, is a technique for changing the visual appearance of certain controls.
|
||||
|
||||
> [!NOTE]
|
||||
> The word "control" in this topic is used to mean classes that derive from either <xref:System.Windows.Forms.Control> or <xref:System.ComponentModel.Component>.
|
||||
|
||||
Typically, Windows handles painting automatically by using property settings such as <xref:System.Windows.Forms.Control.BackColor%2A> to determine the appearance of a control. With owner drawing, you take over the painting process, changing elements of appearance that are not available by using properties. For example, many controls let you set the color of the text that is displayed, but you are limited to a single color. Owner drawing enables you to do things like display part of the text in black and part in red.
|
||||
|
||||
In practice, owner drawing is similar to drawing graphics on a form. For example, you could use graphics methods in a handler for the form's <xref:System.Windows.Forms.Control.Paint> event to emulate a `ListBox` control, but you would have to write your own code to handle all user interaction. With owner drawing, the control uses your code to draw its contents but otherwise retains all its intrinsic capabilities. You can use graphics methods to draw each item in the control or to customize some aspects of each item while you use the default appearance for other aspects of each item.
|
||||
|
||||
## Owner Drawing in Windows Forms Controls
|
||||
To perform owner drawing in controls that support it, you will typically set one property and handle one or more events.
|
||||
|
||||
Most controls that support owner drawing have an `OwnerDraw` or `DrawMode` property that indicates whether the control will raise its drawing-related event or events when it paints itself.
|
||||
|
||||
Controls that do not have an `OwnerDraw` or `DrawMode` property include the `DataGridView` control, which provides drawing events that occur automatically, and the `ToolStrip` control, which is drawn using an external rendering class that has its own drawing-related events.
|
||||
|
||||
There are many different kinds of drawing events, but a typical drawing event occurs in order to draw a single item within a control. The event handler receives an `EventArgs` object that contains information about the item being drawn and tools you can use to draw it. For example, this object typically contains the item's index number within its parent collection, a <xref:System.Drawing.Rectangle> that indicates the item's display boundaries, and a <xref:System.Drawing.Graphics> object for calling paint methods. For some events, the `EventArgs` object provides additional information about the item and methods that you can call to paint some aspects of the item by default, such as the background or a focus rectangle.
|
||||
|
||||
To create a reusable control that contains your owner-drawn customizations, create a new class that derives from a control class that supports owner drawing. Rather than handling drawing events, include your owner-drawing code in overrides for the appropriate `On`*EventName* method or methods in the new class. Make sure that you call the base class `On`*EventName* method or methods in this case so that users of your control can handle owner-drawing events and provide additional customization.
|
||||
|
||||
The following Windows Forms controls support owner drawing in all versions of the .NET Framework:
|
||||
|
||||
- <xref:System.Windows.Forms.ListBox>
|
||||
|
||||
- <xref:System.Windows.Forms.ComboBox>
|
||||
|
||||
- <xref:System.Windows.Forms.MenuItem> (used by <xref:System.Windows.Forms.MainMenu> and <xref:System.Windows.Forms.ContextMenu>)
|
||||
|
||||
- <xref:System.Windows.Forms.TabControl>
|
||||
|
||||
The following controls support owner drawing only in .NET Framework 2.0:
|
||||
|
||||
- <xref:System.Windows.Forms.ToolTip>
|
||||
|
||||
- <xref:System.Windows.Forms.ListView>
|
||||
|
||||
- <xref:System.Windows.Forms.TreeView>
|
||||
|
||||
The following controls support owner drawing and are new in .NET Framework 2.0:
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridView>
|
||||
|
||||
- <xref:System.Windows.Forms.ToolStrip>
|
||||
|
||||
The following sections provide additional details for each of these controls.
|
||||
|
||||
### ListBox and ComboBox Controls
|
||||
The <xref:System.Windows.Forms.ListBox> and <xref:System.Windows.Forms.ComboBox> controls enable you to draw individual items in the control either all in one size, or in varying sizes.
|
||||
|
||||
> [!NOTE]
|
||||
> Although the <xref:System.Windows.Forms.CheckedListBox> control is derived from the <xref:System.Windows.Forms.ListBox> control, it does not support owner drawing.
|
||||
|
||||
To draw each item the same size, set the `DrawMode` property to <xref:System.Windows.Forms.DrawMode.OwnerDrawFixed> and handle the `DrawItem` event.
|
||||
|
||||
To draw each item using a different size, set the `DrawMode` property to <xref:System.Windows.Forms.DrawMode.OwnerDrawVariable> and handle both the `MeasureItem` and `DrawItem` events. The `MeasureItem` event lets you indicate the size of an item before the `DrawItem` event occurs for that item.
|
||||
|
||||
For more information, including code examples, see the following topics:
|
||||
|
||||
- <xref:System.Windows.Forms.ListBox.DrawMode%2A?displayProperty=nameWithType>
|
||||
|
||||
- <xref:System.Windows.Forms.ListBox.MeasureItem?displayProperty=nameWithType>
|
||||
|
||||
- <xref:System.Windows.Forms.ListBox.DrawItem?displayProperty=nameWithType>
|
||||
|
||||
- <xref:System.Windows.Forms.ComboBox.DrawMode%2A?displayProperty=nameWithType>
|
||||
|
||||
- <xref:System.Windows.Forms.ComboBox.MeasureItem?displayProperty=nameWithType>
|
||||
|
||||
- <xref:System.Windows.Forms.ComboBox.DrawItem?displayProperty=nameWithType>
|
||||
|
||||
- [How to: Create Variable Sized Text in a ComboBox Control](how-to-create-variable-sized-text-in-a-combobox-control.md)
|
||||
|
||||
### MenuItem Component
|
||||
The <xref:System.Windows.Forms.MenuItem> component represents a single menu item in a <xref:System.Windows.Forms.MainMenu> or <xref:System.Windows.Forms.ContextMenu> component.
|
||||
|
||||
To draw a <xref:System.Windows.Forms.MenuItem>, set its `OwnerDraw` property to `true` and handle its `DrawItem` event. To customize the size of the menu item before the `DrawItem` event occurs, handle the item's `MeasureItem` event.
|
||||
|
||||
For more information, including code examples, see the following reference topics:
|
||||
|
||||
- <xref:System.Windows.Forms.MenuItem.OwnerDraw%2A?displayProperty=nameWithType>
|
||||
|
||||
- <xref:System.Windows.Forms.MenuItem.DrawItem?displayProperty=nameWithType>
|
||||
|
||||
- <xref:System.Windows.Forms.MenuItem.MeasureItem?displayProperty=nameWithType>
|
||||
|
||||
### TabControl Control
|
||||
The <xref:System.Windows.Forms.TabControl> control enables you to draw individual tabs in the control. Owner drawing affects only the tabs; the <xref:System.Windows.Forms.TabPage> contents are not affected.
|
||||
|
||||
To draw each tab in a <xref:System.Windows.Forms.TabControl>, set the `DrawMode` property to <xref:System.Windows.Forms.TabDrawMode.OwnerDrawFixed> and handle the `DrawItem` event. This event occurs once for each tab only when the tab is visible in the control.
|
||||
|
||||
For more information, including code examples, see the following reference topics:
|
||||
|
||||
- <xref:System.Windows.Forms.TabControl.DrawMode%2A?displayProperty=nameWithType>
|
||||
|
||||
- <xref:System.Windows.Forms.TabControl.DrawItem?displayProperty=nameWithType>
|
||||
|
||||
### ToolTip Component
|
||||
The <xref:System.Windows.Forms.ToolTip> component enables you to draw the entire ToolTip when it is displayed.
|
||||
|
||||
To draw a <xref:System.Windows.Forms.ToolTip>, set its `OwnerDraw` property to `true` and handle its `Draw` event. To customize the size of the <xref:System.Windows.Forms.ToolTip> before the `Draw` event occurs, handle the `Popup` event and set the <xref:System.Windows.Forms.PopupEventArgs.ToolTipSize%2A> property in the event handler.
|
||||
|
||||
For more information, including code examples, see the following reference topics:
|
||||
|
||||
- <xref:System.Windows.Forms.ToolTip.OwnerDraw%2A?displayProperty=nameWithType>
|
||||
|
||||
- <xref:System.Windows.Forms.ToolTip.Draw?displayProperty=nameWithType>
|
||||
|
||||
- <xref:System.Windows.Forms.ToolTip.Popup?displayProperty=nameWithType>
|
||||
|
||||
### ListView Control
|
||||
The <xref:System.Windows.Forms.ListView> control enables you to draw individual items, subitems, and column headers in the control.
|
||||
|
||||
To enable owner drawing in the control, set the `OwnerDraw` property to `true`.
|
||||
|
||||
To draw each item in the control, handle the `DrawItem` event.
|
||||
|
||||
To draw each subitem or column header in the control when the <xref:System.Windows.Forms.ListView.View%2A> property is set to <xref:System.Windows.Forms.View.Details>, handle the `DrawSubItem` and `DrawColumnHeader` events.
|
||||
|
||||
For more information, including code examples, see the following reference topics:
|
||||
|
||||
- <xref:System.Windows.Forms.ListView.OwnerDraw%2A?displayProperty=nameWithType>
|
||||
|
||||
- <xref:System.Windows.Forms.ListView.DrawItem?displayProperty=nameWithType>
|
||||
|
||||
- <xref:System.Windows.Forms.ListView.DrawSubItem?displayProperty=nameWithType>
|
||||
|
||||
- <xref:System.Windows.Forms.ListView.DrawColumnHeader?displayProperty=nameWithType>
|
||||
|
||||
### TreeView Control
|
||||
The <xref:System.Windows.Forms.TreeView> control enables you to draw individual nodes in the control.
|
||||
|
||||
To draw only the text displayed in each node, set the `DrawMode` property to <xref:System.Windows.Forms.TreeViewDrawMode.OwnerDrawText> and handle the `DrawNode` event to draw the text.
|
||||
|
||||
To draw all elements of each node, set the `DrawMode` property to <xref:System.Windows.Forms.TreeViewDrawMode.OwnerDrawAll> and handle the `DrawNode` event to draw whichever elements you need, such as text, icons, check boxes, plus and minus signs, and lines connecting the nodes.
|
||||
|
||||
For more information, including code examples, see the following reference topics:
|
||||
|
||||
- <xref:System.Windows.Forms.TreeView.DrawMode%2A?displayProperty=nameWithType>
|
||||
|
||||
- <xref:System.Windows.Forms.TreeView.DrawNode?displayProperty=nameWithType>
|
||||
|
||||
### DataGridView Control
|
||||
The <xref:System.Windows.Forms.DataGridView> control enables you to draw individual cells and rows in the control.
|
||||
|
||||
To draw individual cells, handle the `CellPainting` event.
|
||||
|
||||
To draw individual rows or elements of rows, handle one or both of the `RowPrePaint` and `RowPostPaint` events. The `RowPrePaint` event occurs before the cells in a row are painted, and the `RowPostPaint` event occurs after the cells are painted. You can handle both events and the `CellPainting` event to paint row background, individual cells, and row foreground separately, or you can provide specific customizations where you need them and use the default display for other elements of the row.
|
||||
|
||||
For more information, including code examples, see the following topics:
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridView.CellPainting>
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridView.RowPrePaint>
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridView.RowPostPaint>
|
||||
|
||||
- [How to: Customize the Appearance of Cells in the Windows Forms DataGridView Control](customize-the-appearance-of-cells-in-the-datagrid.md)
|
||||
|
||||
- [How to: Customize the Appearance of Rows in the Windows Forms DataGridView Control](customize-the-appearance-of-rows-in-the-datagrid.md)
|
||||
|
||||
### ToolStrip Control
|
||||
<xref:System.Windows.Forms.ToolStrip> and derived controls enable you to customize any aspect of their appearance.
|
||||
|
||||
To provide custom rendering for <xref:System.Windows.Forms.ToolStrip> controls, set the `Renderer` property of a <xref:System.Windows.Forms.ToolStrip>, <xref:System.Windows.Forms.ToolStripManager>, <xref:System.Windows.Forms.ToolStripPanel>, or <xref:System.Windows.Forms.ToolStripContentPanel> to a `ToolStripRenderer` object and handle one or more of the many drawing events provided by the `ToolStripRenderer` class. Alternatively, set a `Renderer` property to an instance of your own class derived from `ToolStripRenderer`, <xref:System.Windows.Forms.ToolStripProfessionalRenderer>, or <xref:System.Windows.Forms.ToolStripSystemRenderer> that implements or overrides specific `On`*EventName* methods.
|
||||
|
||||
For more information, including code examples, see the following topics:
|
||||
|
||||
- <xref:System.Windows.Forms.ToolStripRenderer>
|
||||
|
||||
- [How to: Create and Set a Custom Renderer for the ToolStrip Control in Windows Forms](create-and-set-a-custom-renderer-for-the-toolstrip-control-in-wf.md)
|
||||
|
||||
- [How to: Custom Draw a ToolStrip Control](how-to-custom-draw-a-toolstrip-control.md)
|
||||
|
||||
## See also
|
||||
|
||||
- [Controls to Use on Windows Forms](controls-to-use-on-windows-forms.md)
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
---
|
||||
title: Create a Basic ToolStrip with Standard Items Using the Designer
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "ToolStrip control [Windows Forms], creating"
|
||||
- "toolbars [Windows Forms], creating"
|
||||
- "toolbars [Windows Forms], adding items"
|
||||
- "ToolStrip control [Windows Forms], adding standard items"
|
||||
ms.assetid: 028deb1a-acf8-4911-979a-ba8431cd9d82
|
||||
---
|
||||
# How to: Create a Basic Windows Forms ToolStrip with Standard Items Using the Designer
|
||||
The following procedure demonstrates how to create a <xref:System.Windows.Forms.ToolStrip> and add seven <xref:System.Windows.Forms.ToolStripButton> controls that represent typical tasks.
|
||||
|
||||
## To add standard items in the designer
|
||||
|
||||
1. Create a <xref:System.Windows.Forms.ToolStrip> control.
|
||||
|
||||
2. In the upper right corner of the <xref:System.Windows.Forms.ToolStrip>, click the smart task arrow to display the **ToolStrip Tasks** pane.
|
||||
|
||||
3. In the **ToolStrip Tasks** pane, choose **Insert Standard Items**.
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.ToolStrip>
|
||||
- [ToolStrip Control Overview](toolstrip-control-overview-windows-forms.md)
|
||||
- [ToolStrip Control](toolstrip-control-windows-forms.md)
|
||||
- [ToolStrip Control Architecture](toolstrip-control-architecture.md)
|
||||
- [ToolStrip Technology Summary](toolstrip-technology-summary.md)
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
---
|
||||
title: Create a Lookup Table for ComboBox, ListBox, or CheckedListBox Control
|
||||
ms.date: "03/30/2017"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
helpviewer_keywords:
|
||||
- "CheckedListBox control [Windows Forms], creating lookup tables"
|
||||
- "lookup tables"
|
||||
- "list boxes [Windows Forms], lookup tables"
|
||||
- "ListBox control [Windows Forms], lookup tables"
|
||||
- "ComboBox control [Windows Forms], lookup table"
|
||||
- "lookup tables [Windows Forms], creating for controls"
|
||||
- "combo boxes [Windows Forms], lookup tables"
|
||||
- "ListBox control [Windows Forms], creating lookup tables"
|
||||
ms.assetid: 4ce35f12-1f4e-4317-92d1-af8686a8cfaa
|
||||
---
|
||||
# How to: Create a Lookup Table for a Windows Forms ComboBox, ListBox, or CheckedListBox Control
|
||||
Sometimes it is useful to display data in a user-friendly format on a Windows Form, but store the data in a format that is more meaningful to your program. For example, an order form for food might display the menu items by name in a list box. However, the data table recording the order would contain the unique ID numbers representing the food. The following tables show an example of how to store and display order-form data for food.
|
||||
|
||||
### OrderDetailsTable
|
||||
|
||||
|OrderID|ItemID|Quantity|
|
||||
|-------------|------------|--------------|
|
||||
|4085|12|1|
|
||||
|4086|13|3|
|
||||
|
||||
### ItemTable
|
||||
|
||||
|ID|Name|
|
||||
|--------|----------|
|
||||
|12|Potato|
|
||||
|13|Chicken|
|
||||
|
||||
In this scenario, one table, **OrderDetailsTable**, stores the actual information you are concerned with displaying and saving. But to save space, it does so in a fairly cryptic fashion. The other table, **ItemTable**, contains only appearance-related information about which ID number is equivalent to which food name, and nothing about the actual food orders.
|
||||
|
||||
The **ItemTable** is connected to the <xref:System.Windows.Forms.ComboBox>, <xref:System.Windows.Forms.ListBox>, or <xref:System.Windows.Forms.CheckedListBox> control through three properties. The `DataSource` property contains the name of this table. The `DisplayMember` property contains the data column of that table that you want to display in the control (the food name). The `ValueMember` property contains the data column of that table with the stored information (the ID number).
|
||||
|
||||
The **OrderDetailsTable** is connected to the control by its bindings collection, accessed through the <xref:System.Windows.Forms.Control.DataBindings%2A> property. When you add a binding object to the collection, you connect a control property to a specific data member (the column of ID numbers) in a data source (the **OrderDetailsTable**). When a selection is made in the control, this table is where the form input is saved.
|
||||
|
||||
### To create a lookup table
|
||||
|
||||
1. Add a <xref:System.Windows.Forms.ComboBox>, <xref:System.Windows.Forms.ListBox>, or <xref:System.Windows.Forms.CheckedListBox> control to the form.
|
||||
|
||||
2. Connect to your data source.
|
||||
|
||||
3. Establish a data relation between the two tables. See [Introduction to DataRelation Objects](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2013/0k21zcyx(v=vs.120)).
|
||||
|
||||
4. Set the following properties. They can be set in code or in the designer.
|
||||
|
||||
|Property|Setting|
|
||||
|--------------|-------------|
|
||||
|<xref:System.Windows.Forms.ListControl.DataSource%2A>|The table that contains information about which ID number is equivalent to which item. In the previous scenario, this is `ItemTable`.|
|
||||
|<xref:System.Windows.Forms.ListControl.DisplayMember%2A>|The column of the data source table that you want to display in the control. In the previous scenario, this is `"Name"` (to set in code, use quotation marks).|
|
||||
|<xref:System.Windows.Forms.ListControl.ValueMember%2A>|The column of the data source table that contains the stored information. In the previous scenario, this is `"ID"` (to set in code, use quotation marks).|
|
||||
|
||||
5. In a procedure, call the <xref:System.Windows.Forms.ControlBindingsCollection.Add%2A> method of the <xref:System.Windows.Forms.ControlBindingsCollection> class to bind the control's <xref:System.Windows.Forms.ListControl.SelectedValue%2A> property to the table recording the form input. You can also do this in the Designer instead of in code, by accessing the control's <xref:System.Windows.Forms.Control.DataBindings%2A> property in the **Properties** window. In the previous scenario, this is `OrderDetailsTable`, and the column is `"ItemID"`.
|
||||
|
||||
```vb
|
||||
ListBox1.DataBindings.Add("SelectedValue", OrderDetailsTable, "ItemID")
|
||||
```
|
||||
|
||||
```csharp
|
||||
listBox1.DataBindings.Add("SelectedValue", OrderDetailsTable, "ItemID");
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [Data Binding and Windows Forms](../data-binding-and-windows-forms.md)
|
||||
- [ListBox Control Overview](listbox-control-overview-windows-forms.md)
|
||||
- [ComboBox Control Overview](combobox-control-overview-windows-forms.md)
|
||||
- [CheckedListBox Control Overview](checkedlistbox-control-overview-windows-forms.md)
|
||||
- [Windows Forms Controls Used to List Options](windows-forms-controls-used-to-list-options.md)
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
---
|
||||
title: Create a Master-Detail Form Using Two DataGridView Controls
|
||||
ms.date: "03/30/2017"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
helpviewer_keywords:
|
||||
- "DataGridView control [Windows Forms], master/detail form"
|
||||
- "parent-child tables [Windows Forms], displaying on Windows Forms"
|
||||
- "master-details lists [Windows Forms], creating"
|
||||
ms.assetid: 99f6e876-3f7f-4139-9063-e36587c95b02
|
||||
---
|
||||
# How to: Create a Master/Detail Form Using Two Windows Forms DataGridView Controls
|
||||
The following code example creates a master/detail form using two <xref:System.Windows.Forms.DataGridView> controls bound to two <xref:System.Windows.Forms.BindingSource> components. The data source is a <xref:System.Data.DataSet> that contains the `Customers` and `Orders` tables from the Northwind SQL Server sample database along with a <xref:System.Data.DataRelation> that relates the two through the `CustomerID` column.
|
||||
|
||||
One <xref:System.Windows.Forms.BindingSource> is bound to the parent `Customers` table in the data set. This data is displayed in the master <xref:System.Windows.Forms.DataGridView> control. The other <xref:System.Windows.Forms.BindingSource> is bound to the first data connector. The <xref:System.Windows.Forms.BindingSource.DataMember%2A> property of the second <xref:System.Windows.Forms.BindingSource> is set to the <xref:System.Data.DataRelation> name. This causes the associated detail <xref:System.Windows.Forms.DataGridView> control to display the rows of the child `Orders` table that correspond to the current row in the master <xref:System.Windows.Forms.DataGridView> control.
|
||||
|
||||
For a complete explanation of this code example, see [Walkthrough: Creating a Master/Detail Form Using Two Windows Forms DataGridView Controls](creating-a-master-detail-form-using-two-datagridviews.md).
|
||||
|
||||
## Example
|
||||
[!code-csharp[System.Windows.Forms.DataGridViewMasterDetails#00](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewMasterDetails/CS/masterdetails.cs#00)]
|
||||
[!code-vb[System.Windows.Forms.DataGridViewMasterDetails#00](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewMasterDetails/VB/masterdetails.vb#00)]
|
||||
|
||||
## Compiling the Code
|
||||
This example requires:
|
||||
|
||||
References to the System, System.Data, System.Windows.Forms, and System.XML assemblies.
|
||||
|
||||
## .NET Framework Security
|
||||
Storing sensitive information, such as a password, within the connection string can affect the security of your application. Using Windows Authentication (also known as integrated security) is a more secure way to control access to a database. For more information, see [Protecting Connection Information](https://docs.microsoft.com/dotnet/framework/data/adonet/protecting-connection-information).
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridView>
|
||||
- <xref:System.Windows.Forms.BindingSource>
|
||||
- [Walkthrough: Creating a Master/Detail Form Using Two Windows Forms DataGridView Controls](creating-a-master-detail-form-using-two-datagridviews.md)
|
||||
- [Displaying Data in the Windows Forms DataGridView Control](displaying-data-in-the-windows-forms-datagridview-control.md)
|
||||
- [Protecting Connection Information](https://docs.microsoft.com/dotnet/framework/data/adonet/protecting-connection-information)
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
---
|
||||
title: Create a Multipane User Interface Using the Designer
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "user interface [Windows Forms], multipane"
|
||||
- "SplitContainer control [Windows Forms], using the designer"
|
||||
- "multipane user interface"
|
||||
ms.assetid: c3f9294d-a26c-4198-9242-f237f55f7573
|
||||
---
|
||||
# How to: Create a Multipane User Interface with Windows Forms Using the Designer
|
||||
In the following procedure, you will create a multipane user interface that is similar to the one used in Microsoft Outlook, with a **Folder** list, a **Messages** pane, and a **Preview** pane. This arrangement is achieved chiefly through docking controls with the form.
|
||||
|
||||
When you dock a control, you determine which edge of the parent container a control is fastened to. Thus, if you set the <xref:System.Windows.Forms.SplitContainer.Dock%2A> property to <xref:System.Windows.Forms.DockStyle.Right>, the right edge of the control will be docked to the right edge of its parent control. Additionally, the docked edge of the control is resized to match that of its container control. For more information about how the <xref:System.Windows.Forms.SplitContainer.Dock%2A> property works, see [How to: Dock Controls on Windows Forms](how-to-dock-controls-on-windows-forms.md).
|
||||
|
||||
This procedure focuses on arranging the <xref:System.Windows.Forms.SplitContainer> and the other controls on the form, not on adding functionality to make the application mimic Microsoft Outlook.
|
||||
|
||||
To create this user interface, you place all the controls within a <xref:System.Windows.Forms.SplitContainer> control, which contains a <xref:System.Windows.Forms.TreeView> control in the left-hand panel. The right-hand panel of the <xref:System.Windows.Forms.SplitContainer> control contains a second <xref:System.Windows.Forms.SplitContainer> control with a <xref:System.Windows.Forms.ListView> control above a <xref:System.Windows.Forms.RichTextBox> control. These <xref:System.Windows.Forms.SplitContainer> controls enable independent resizing of the other controls on the form. You can adapt the techniques in this procedure to craft custom user interfaces of your own.
|
||||
|
||||
## To create an Outlook-style user interface at design time
|
||||
|
||||
1. Create a new Windows Application project (**File** > **New** > **Project** > **Visual C#** or **Visual Basic** > **Classic Desktop** > **Windows Forms Application**).
|
||||
|
||||
2. Drag a <xref:System.Windows.Forms.SplitContainer> control from the **Toolbox** to the form. In the **Properties** window, set the <xref:System.Windows.Forms.SplitContainer.Dock%2A> property to <xref:System.Windows.Forms.DockStyle.Fill>.
|
||||
|
||||
3. Drag a <xref:System.Windows.Forms.TreeView> control from the **Toolbox** to the left-hand panel of the <xref:System.Windows.Forms.SplitContainer> control. In the **Properties** window, set the <xref:System.Windows.Forms.SplitContainer.Dock%2A> property to <xref:System.Windows.Forms.DockStyle.Left> by clicking the left hand panel in the value editor shown when the down arrow is clicked.
|
||||
|
||||
4. Drag another <xref:System.Windows.Forms.SplitContainer> control from the **Toolbox**; place it in the right-hand panel of the <xref:System.Windows.Forms.SplitContainer> control you added to your form. In the **Properties** window, set the <xref:System.Windows.Forms.SplitContainer.Dock%2A> property to <xref:System.Windows.Forms.DockStyle.Fill> and the <xref:System.Windows.Forms.SplitContainer.Orientation%2A> property to <xref:System.Windows.Forms.Orientation.Horizontal>.
|
||||
|
||||
5. Drag a <xref:System.Windows.Forms.ListView> control from the **Toolbox** to the upper panel of the second <xref:System.Windows.Forms.SplitContainer> control you added to your form. Set the <xref:System.Windows.Forms.SplitContainer.Dock%2A> property of the <xref:System.Windows.Forms.ListView> control to <xref:System.Windows.Forms.DockStyle.Fill>.
|
||||
|
||||
6. Drag a <xref:System.Windows.Forms.RichTextBox> control from the **Toolbox** to the lower panel of the second <xref:System.Windows.Forms.SplitContainer> control. Set the <xref:System.Windows.Forms.SplitContainer.Dock%2A> property of the <xref:System.Windows.Forms.RichTextBox> control to <xref:System.Windows.Forms.DockStyle.Fill>.
|
||||
|
||||
At this point, if you press F5 to run the application, the form displays a three-part user interface, similar to that of Microsoft Outlook.
|
||||
|
||||
> [!NOTE]
|
||||
> When you put the mouse pointer over either of the splitters within the <xref:System.Windows.Forms.SplitContainer> controls, you can resize the internal dimensions.
|
||||
|
||||
At this point in application development, you have crafted a sophisticated user interface. The next step is proceeding with the programming of the application itself, perhaps by connecting the <xref:System.Windows.Forms.TreeView> control and <xref:System.Windows.Forms.ListView> controls to some kind of data source. For more information about connecting controls to data, see [Data Binding and Windows Forms](../data-binding-and-windows-forms.md).
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.SplitContainer>
|
||||
- [SplitContainer Control](splitcontainer-control-windows-forms.md)
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
---
|
||||
title: "How to: Create and Set a Custom Renderer for the ToolStrip Control"
|
||||
ms.date: "03/30/2017"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
helpviewer_keywords:
|
||||
- "ToolStrip control [Windows Forms], custom rendering"
|
||||
- "toolbars [Windows Forms], rendering"
|
||||
- "examples [Windows Forms], toolbars"
|
||||
- "ToolStrip control [Windows Forms], rendering"
|
||||
ms.assetid: 88a804ba-679f-4ba3-938a-0dc396199c5b
|
||||
---
|
||||
# How to: Create and Set a Custom Renderer for the ToolStrip Control in Windows Forms
|
||||
<xref:System.Windows.Forms.ToolStrip> controls give easy support to themes and styles. You can achieve completely custom appearance and behavior (look and feel) by setting either the <xref:System.Windows.Forms.ToolStrip.Renderer%2A?displayProperty=nameWithType> property or the <xref:System.Windows.Forms.ToolStripManager.Renderer%2A?displayProperty=nameWithType> property to a custom renderer.
|
||||
|
||||
You can assign renderers to each individual <xref:System.Windows.Forms.ToolStrip>, <xref:System.Windows.Forms.MenuStrip>, <xref:System.Windows.Forms.ContextMenuStrip>, or <xref:System.Windows.Forms.StatusStrip> control, or you can use the <xref:System.Windows.Forms.ToolStripManager.Renderer%2A> property to affect all objects by setting the <xref:System.Windows.Forms.ToolStrip.RenderMode%2A?displayProperty=nameWithType> property to <xref:System.Windows.Forms.ToolStripRenderMode.ManagerRenderMode?displayProperty=nameWithType>.
|
||||
|
||||
> [!NOTE]
|
||||
> <xref:System.Windows.Forms.ToolStrip.RenderMode%2A> returns <xref:System.Windows.Forms.ToolStripRenderMode.Custom> only if the value of <xref:System.Windows.Forms.ToolStrip.Renderer%2A?displayProperty=nameWithType> is not `null`.
|
||||
|
||||
### To create a custom renderer
|
||||
|
||||
1. Extend the <xref:System.Windows.Forms.ToolStripRenderer> class.
|
||||
|
||||
2. Implement desired custom rendering by overriding appropriate *On…* members
|
||||
|
||||
```vb
|
||||
Public Class RedTextRenderer
|
||||
Inherits System.Windows.Forms.ToolStripRenderer
|
||||
Protected Overrides Sub OnRenderItemText(ByVal e As _
|
||||
ToolStripItemTextRenderEventArgs)
|
||||
e.TextColor = Color.Red
|
||||
e.TextFont = New Font("Helvetica", 7, FontStyle.Bold)
|
||||
MyBase.OnRenderItemText(e)
|
||||
End Sub
|
||||
End Class
|
||||
```
|
||||
|
||||
```csharp
|
||||
public class RedTextRenderer : _
|
||||
System.Windows.Forms.ToolStripRenderer
|
||||
{
|
||||
protected override void _
|
||||
OnRenderItemText(ToolStripItemTextRenderEventArgs e)
|
||||
{
|
||||
e.TextColor = Color.Red;
|
||||
e.TextFont = new Font("Helvetica", 7, FontStyle.Bold);
|
||||
base.OnRenderItemText(e);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### To set the custom renderer to be the current renderer
|
||||
|
||||
1. To set the custom renderer for one <xref:System.Windows.Forms.ToolStrip>, set the <xref:System.Windows.Forms.ToolStrip.Renderer%2A?displayProperty=nameWithType> property to the custom renderer.
|
||||
|
||||
```vb
|
||||
toolStrip1.Renderer = New RedTextRenderer()
|
||||
```
|
||||
|
||||
```csharp
|
||||
toolStrip1.Renderer = new RedTextRenderer();
|
||||
```
|
||||
|
||||
2. Or to set the custom renderer for all <xref:System.Windows.Forms.ToolStrip> classes contained in your application: Set the <xref:System.Windows.Forms.ToolStripManager.Renderer%2A?displayProperty=nameWithType> property to the custom renderer and set the <xref:System.Windows.Forms.ToolStrip.RenderMode%2A> property to <xref:System.Windows.Forms.ToolStripRenderMode.ManagerRenderMode>.
|
||||
|
||||
```vb
|
||||
toolStrip1.RenderMode = ToolStripRenderMode.ManagerRenderMode
|
||||
ToolStripManager.Renderer = New RedTextRenderer()
|
||||
```
|
||||
|
||||
```csharp
|
||||
toolStrip1.RenderMode = ToolStripRenderMode.ManagerRenderMode;
|
||||
ToolStripManager.Renderer = new RedTextRenderer();
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.ToolStripManager.Renderer%2A>
|
||||
- <xref:System.Windows.Forms.ToolStripRenderer>
|
||||
- <xref:System.Windows.Forms.ToolStrip.RenderMode%2A>
|
||||
- [ToolStrip Control Overview](toolstrip-control-overview-windows-forms.md)
|
||||
- [ToolStrip Control Architecture](toolstrip-control-architecture.md)
|
||||
- [ToolStrip Technology Summary](toolstrip-technology-summary.md)
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
---
|
||||
title: Create Master-Details Lists with DataGrid Control Using the Designer
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "master-details lists"
|
||||
- "DataGrid control [Windows Forms], master-details lists"
|
||||
- "related tables [Windows Forms], displaying in DataGrid control"
|
||||
ms.assetid: 19438ba2-f687-4417-a2fb-ab1cd69d4ded
|
||||
---
|
||||
# How to: Create Master-Details Lists with the Windows Forms DataGrid Control Using the Designer
|
||||
|
||||
> [!NOTE]
|
||||
> The <xref:System.Windows.Forms.DataGridView> control replaces and adds functionality to the <xref:System.Windows.Forms.DataGrid> control; however, the <xref:System.Windows.Forms.DataGrid> control is retained for both backward compatibility and future use, if you choose. For more information, see [Differences Between the Windows Forms DataGridView and DataGrid Controls](differences-between-the-windows-forms-datagridview-and-datagrid-controls.md).
|
||||
|
||||
If your <xref:System.Data.DataSet> contains a series of related tables, you can use two <xref:System.Windows.Forms.DataGrid> controls to display the data in a master-detail format. One <xref:System.Windows.Forms.DataGrid> is designated to be the master grid, and the second is designated to be the details grid. When you select an entry in the master list, all of the related child entries are shown in the details list. For example, if your <xref:System.Data.DataSet> contains a Customers table and a related Orders table, you would specify the Customers table to be the master grid and the Orders table to be the details grid. When a customer is selected from the master grid, all of the orders associated with that customer in the Orders table would be displayed in the details grid.
|
||||
|
||||
The following procedure requires a **Windows Application** project (**File** > **New** > **Project** > **Visual C#** or **Visual Basic** > **Classic Desktop** > **Windows Forms Application**).
|
||||
|
||||
## To create a master-details list in the designer
|
||||
|
||||
1. Add two <xref:System.Windows.Forms.DataGrid> controls to the form. For more information, see [How to: Add Controls to Windows Forms](how-to-add-controls-to-windows-forms.md). In Visual Studio 2005, the <xref:System.Windows.Forms.DataGrid> control is not in the **Toolbox** by default. For more information, see [How to: Add Items to the Toolbox](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2010/ms165355(v=vs.100)).
|
||||
|
||||
> [!NOTE]
|
||||
> The following steps are not applicable to Visual Studio 2005, which uses the **Data Sources** window for design-time data binding. For more information, see [Bind controls to data in Visual Studio](/visualstudio/data-tools/bind-controls-to-data-in-visual-studio) and [How to: Display Related Data in a Windows Forms Application](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2013/57tx3hhe(v=vs.120)).
|
||||
|
||||
2. Drag two or more tables from **Server Explorer** to the form.
|
||||
|
||||
3. From the **Data** menu, select **Generate DataSet**.
|
||||
|
||||
4. Set the relationships between the tables using the XML Designer. For details, see "How to: Create One-to-Many Relationships in XML Schemas and Datasets" on MSDN.
|
||||
|
||||
5. Save the relationships by selecting **Save All** from the **File** menu.
|
||||
|
||||
6. Configure the <xref:System.Windows.Forms.DataGrid> control that you want to designate the master grid, as follows:
|
||||
|
||||
1. Select the <xref:System.Data.DataSet> from the drop-down list in the <xref:System.Windows.Forms.DataGrid.DataSource%2A> property.
|
||||
|
||||
2. Select the master table (for example, "Customers") from the drop-down list in the <xref:System.Windows.Forms.DataGrid.DataMember%2A> property.
|
||||
|
||||
7. Configure the <xref:System.Windows.Forms.DataGrid> control that you want to designate the details grid, as follows:
|
||||
|
||||
1. Select the <xref:System.Data.DataSet> from the drop-down list in the <xref:System.Windows.Forms.DataGrid.DataSource%2A> property.
|
||||
|
||||
2. Select the relationship (for example, "Customers.CustOrd") between the master and detail tables from the drop-down list in the <xref:System.Windows.Forms.DataGrid.DataMember%2A> property. In order to see the relationship, expand the node by clicking on the plus (**+**) sign next to the master table in the drop-down list.
|
||||
|
||||
## See also
|
||||
|
||||
- [DataGrid Control](datagrid-control-windows-forms.md)
|
||||
- [DataGrid Control Overview](datagrid-control-overview-windows-forms.md)
|
||||
- [How to: Bind the Windows Forms DataGrid Control to a Data Source](how-to-bind-the-windows-forms-datagrid-control-to-a-data-source.md)
|
||||
- [Bind controls to data in Visual Studio](/visualstudio/data-tools/bind-controls-to-data-in-visual-studio)
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
---
|
||||
title: "Walkthrough: Create a master-detail form using two DataGridView controls"
|
||||
ms.date: "03/30/2017"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
helpviewer_keywords:
|
||||
- "DataGridView control [Windows Forms], master/detail form"
|
||||
- "parent-child tables [Windows Forms], displaying on Windows Forms"
|
||||
- "master-details lists [Windows Forms], displaying on Windows Forms"
|
||||
- "walkthroughs [Windows Forms], DataGridView control"
|
||||
ms.assetid: c5fa29e8-47f7-4691-829b-0e697a691f36
|
||||
---
|
||||
# Walkthrough: Creating a Master/Detail Form Using Two Windows Forms DataGridView Controls
|
||||
|
||||
One of the most common scenarios for using the <xref:System.Windows.Forms.DataGridView> control is the *master/detail* form, in which a parent/child relationship between two database tables is displayed. Selecting rows in the master table causes the detail table to update with the corresponding child data.
|
||||
|
||||
Implementing a master/detail form is easy using the interaction between the <xref:System.Windows.Forms.DataGridView> control and the <xref:System.Windows.Forms.BindingSource> component. In this walkthrough, you will build the form using two <xref:System.Windows.Forms.DataGridView> controls and two <xref:System.Windows.Forms.BindingSource> components. The form will show two related tables in the Northwind SQL Server sample database: `Customers` and `Orders`. When you are finished, you will have a form that shows all the customers in the database in the master <xref:System.Windows.Forms.DataGridView> and all the orders for the selected customer in the detail <xref:System.Windows.Forms.DataGridView>.
|
||||
|
||||
To copy the code in this topic as a single listing, see [How to: Create a Master/Detail Form Using Two Windows Forms DataGridView Controls](create-a-master-detail-form-using-two-datagridviews.md).
|
||||
|
||||
## Prerequisites
|
||||
|
||||
In order to complete this walkthrough, you will need:
|
||||
|
||||
- Access to a server that has the Northwind SQL Server sample database.
|
||||
|
||||
## Creating the form
|
||||
|
||||
#### To create a master/detail form
|
||||
|
||||
1. Create a class that derives from <xref:System.Windows.Forms.Form> and contains two <xref:System.Windows.Forms.DataGridView> controls and two <xref:System.Windows.Forms.BindingSource> components. The following code provides basic form initialization and includes a `Main` method. If you use the Visual Studio designer to create your form, you can use the designer generated code instead of this code, but be sure to use the names shown in the variable declarations here.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.DataGridViewMasterDetails#01](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewMasterDetails/CS/masterdetails.cs#01)]
|
||||
[!code-vb[System.Windows.Forms.DataGridViewMasterDetails#01](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewMasterDetails/VB/masterdetails.vb#01)]
|
||||
[!code-csharp[System.Windows.Forms.DataGridViewMasterDetails#02](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewMasterDetails/CS/masterdetails.cs#02)]
|
||||
[!code-vb[System.Windows.Forms.DataGridViewMasterDetails#02](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewMasterDetails/VB/masterdetails.vb#02)]
|
||||
|
||||
2. Implement a method in your form's class definition for handling the detail of connecting to the database. This example uses a `GetData` method that populates a <xref:System.Data.DataSet> object, adds a <xref:System.Data.DataRelation> object to the data set, and binds the <xref:System.Windows.Forms.BindingSource> components. Be sure to set the `connectionString` variable to a value that is appropriate for your database.
|
||||
|
||||
> [!IMPORTANT]
|
||||
> Storing sensitive information, such as a password, within the connection string can affect the security of your application. Using Windows Authentication (also known as integrated security) is a more secure way to control access to a database. For more information, see [Protecting Connection Information](https://docs.microsoft.com/dotnet/framework/data/adonet/protecting-connection-information).
|
||||
|
||||
[!code-csharp[System.Windows.Forms.DataGridViewMasterDetails#20](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewMasterDetails/CS/masterdetails.cs#20)]
|
||||
[!code-vb[System.Windows.Forms.DataGridViewMasterDetails#20](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewMasterDetails/VB/masterdetails.vb#20)]
|
||||
|
||||
3. Implement a handler for your form's <xref:System.Windows.Forms.Form.Load> event that binds the <xref:System.Windows.Forms.DataGridView> controls to the <xref:System.Windows.Forms.BindingSource> components and calls the `GetData` method. The following example includes code that resizes <xref:System.Windows.Forms.DataGridView> columns to fit the displayed data.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.DataGridViewMasterDetails#10](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewMasterDetails/CS/masterdetails.cs#10)]
|
||||
[!code-vb[System.Windows.Forms.DataGridViewMasterDetails#10](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewMasterDetails/VB/masterdetails.vb#10)]
|
||||
|
||||
## Testing the Application
|
||||
|
||||
You can now test the form to make sure it behaves as expected.
|
||||
|
||||
#### To test the form
|
||||
|
||||
- Compile and run the application.
|
||||
|
||||
You will see two <xref:System.Windows.Forms.DataGridView> controls, one above the other. On top are the customers from the Northwind `Customers` table, and at the bottom are the `Orders` corresponding to the selected customer. As you select different rows in the upper <xref:System.Windows.Forms.DataGridView>, the contents of the lower <xref:System.Windows.Forms.DataGridView> change accordingly.
|
||||
|
||||
## Next Steps
|
||||
|
||||
This application gives you a basic understanding of the <xref:System.Windows.Forms.DataGridView> control's capabilities. You can customize the appearance and behavior of the <xref:System.Windows.Forms.DataGridView> control in several ways:
|
||||
|
||||
- Change border and header styles. For more information, see [How to: Change the Border and Gridline Styles in the Windows Forms DataGridView Control](change-the-border-and-gridline-styles-in-the-datagrid.md).
|
||||
|
||||
- Enable or restrict user input to the <xref:System.Windows.Forms.DataGridView> control. For more information, see [How to: Prevent Row Addition and Deletion in the Windows Forms DataGridView Control](prevent-row-addition-and-deletion-datagridview.md), and [How to: Make Columns Read-Only in the Windows Forms DataGridView Control](how-to-make-columns-read-only-in-the-windows-forms-datagridview-control.md).
|
||||
|
||||
- Validate user input to the <xref:System.Windows.Forms.DataGridView> control. For more information, see [Walkthrough: Validating Data in the Windows Forms DataGridView Control](walkthrough-validating-data-in-the-windows-forms-datagridview-control.md).
|
||||
|
||||
- Handle very large data sets using virtual mode. For more information, see [Walkthrough: Implementing Virtual Mode in the Windows Forms DataGridView Control](implementing-virtual-mode-wf-datagridview-control.md).
|
||||
|
||||
- Customize the appearance of cells. For more information, see [How to: Customize the Appearance of Cells in the Windows Forms DataGridView Control](customize-the-appearance-of-cells-in-the-datagrid.md) and [How to: Set Default Cell Styles for the Windows Forms DataGridView Control](how-to-set-default-cell-styles-for-the-windows-forms-datagridview-control.md).
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridView>
|
||||
- <xref:System.Windows.Forms.BindingSource>
|
||||
- [Displaying Data in the Windows Forms DataGridView Control](displaying-data-in-the-windows-forms-datagridview-control.md)
|
||||
- [How to: Create a Master/Detail Form Using Two Windows Forms DataGridView Controls](create-a-master-detail-form-using-two-datagridviews.md)
|
||||
- [Protecting Connection Information](https://docs.microsoft.com/dotnet/framework/data/adonet/protecting-connection-information)
|
||||
+614
@@ -0,0 +1,614 @@
|
||||
---
|
||||
title: Create a Control That Takes Advantage of Visual Studio Design-Time Features
|
||||
description: Learn how to create a custom designer for a custom control in Windows Forms that takes advantage of design-time features.
|
||||
ms.date: "03/30/2017"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
helpviewer_keywords:
|
||||
- "Windows Forms controls, creating"
|
||||
- "design-time functionality [Windows Forms], Windows Forms"
|
||||
- "DocumentDesigner class [Windows Forms]"
|
||||
- "walkthroughs [Windows Forms], controls"
|
||||
ms.assetid: 6f487c59-cb38-4afa-ad2e-95edacb1d626
|
||||
author: jillre
|
||||
ms.author: jillfra
|
||||
manager: jillfra
|
||||
---
|
||||
# Walkthrough: Create a control that takes advantage of design-time features
|
||||
|
||||
The design-time experience for a custom control can be enhanced by authoring an associated custom designer.
|
||||
|
||||
This article illustrates how to create a custom designer for a custom control. You'll implement a `MarqueeControl` type and an associated designer class called `MarqueeControlRootDesigner`.
|
||||
|
||||
The `MarqueeControl` type implements a display similar to a theater marquee with animated lights and flashing text.
|
||||
|
||||
The designer for this control interacts with the design environment to provide a custom design-time experience. With the custom designer, you can assemble a custom `MarqueeControl` implementation with animated lights and flashing text in many combinations. You can use the assembled control on a form like any other Windows Forms control.
|
||||
|
||||
When you're finished with this walkthrough, your custom control will look something like the following:
|
||||
|
||||

|
||||
|
||||
For the complete code listing, see [How to: Create a Windows Forms Control That Takes Advantage of Design-Time Features](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2013/307hck25(v=vs.120)).
|
||||
|
||||
## Prerequisites
|
||||
|
||||
In order to complete this walkthrough, you'll need Visual Studio.
|
||||
|
||||
## Create the project
|
||||
|
||||
The first step is to create the application project. You will use this project to build the application that hosts the custom control.
|
||||
|
||||
In Visual Studio, create a new Windows Forms Application project, and name it **MarqueeControlTest**.
|
||||
|
||||
## Create the control library project
|
||||
|
||||
1. Add a Windows Forms Control Library project to the solution. Name the project **MarqueeControlLibrary**.
|
||||
|
||||
2. Using **Solution Explorer**, delete the project's default control by deleting the source file named "UserControl1.cs" or "UserControl1.vb", depending on your language of choice.
|
||||
|
||||
3. Add a new <xref:System.Windows.Forms.UserControl> item to the `MarqueeControlLibrary` project. Give the new source file a base name of **MarqueeControl**.
|
||||
|
||||
4. Using **Solution Explorer**, create a new folder in the `MarqueeControlLibrary` project.
|
||||
|
||||
5. Right-click the **Design** folder and add a new class. Name it **MarqueeControlRootDesigner**.
|
||||
|
||||
6. You'll need to use types from the System.Design assembly, so add this reference to the `MarqueeControlLibrary` project.
|
||||
|
||||
## Reference the Custom Control Project
|
||||
|
||||
You will use the `MarqueeControlTest` project to test the custom control. The test project will become aware of the custom control when you add a project reference to the `MarqueeControlLibrary` assembly.
|
||||
|
||||
In the `MarqueeControlTest` project, add a project reference to the `MarqueeControlLibrary` assembly. Be sure to use the **Projects** tab in the **Add Reference** dialog box instead of referencing the `MarqueeControlLibrary` assembly directly.
|
||||
|
||||
## Define a Custom Control and Its Custom Designer
|
||||
|
||||
Your custom control will derive from the <xref:System.Windows.Forms.UserControl> class. This allows your control to contain other controls, and it gives your control a great deal of default functionality.
|
||||
|
||||
Your custom control will have an associated custom designer. This allows you to create a unique design experience tailored specifically for your custom control.
|
||||
|
||||
You associate the control with its designer by using the <xref:System.ComponentModel.DesignerAttribute> class. Because you are developing the entire design-time behavior of your custom control, the custom designer will implement the <xref:System.ComponentModel.Design.IRootDesigner> interface.
|
||||
|
||||
### To define a custom control and its custom designer
|
||||
|
||||
1. Open the `MarqueeControl` source file in the **Code Editor**. At the top of the file, import the following namespaces:
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#220](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueecontrol.cs#220)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#220](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueecontrol.vb#220)]
|
||||
|
||||
2. Add the <xref:System.ComponentModel.DesignerAttribute> to the `MarqueeControl` class declaration. This associates the custom control with its designer.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#240](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueecontrol.cs#240)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#240](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueecontrol.vb#240)]
|
||||
|
||||
3. Open the `MarqueeControlRootDesigner` source file in the **Code Editor**. At the top of the file, import the following namespaces:
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#520](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueecontrolrootdesigner.cs#520)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#520](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueecontrolrootdesigner.vb#520)]
|
||||
|
||||
4. Change the declaration of `MarqueeControlRootDesigner` to inherit from the <xref:System.Windows.Forms.Design.DocumentDesigner> class. Apply the <xref:System.ComponentModel.ToolboxItemFilterAttribute> to specify the designer interaction with the **Toolbox**.
|
||||
|
||||
> [!NOTE]
|
||||
> The definition for the `MarqueeControlRootDesigner` class has been enclosed in a namespace called MarqueeControlLibrary.Design. This declaration places the designer in a special namespace reserved for design-related types.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#530](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueecontrolrootdesigner.cs#530)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#530](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueecontrolrootdesigner.vb#530)]
|
||||
|
||||
5. Define the constructor for the `MarqueeControlRootDesigner` class. Insert a <xref:System.Diagnostics.Trace.WriteLine%2A> statement in the constructor body. This will be useful for debugging.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#540](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueecontrolrootdesigner.cs#540)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#540](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueecontrolrootdesigner.vb#540)]
|
||||
|
||||
## Create an instance of your custom control
|
||||
|
||||
1. Add a new <xref:System.Windows.Forms.UserControl> item to the `MarqueeControlTest` project. Give the new source file a base name of **DemoMarqueeControl**.
|
||||
|
||||
2. Open the `DemoMarqueeControl` file in the **Code Editor**. At the top of the file, import the `MarqueeControlLibrary` namespace:
|
||||
|
||||
```vb
|
||||
Imports MarqueeControlLibrary
|
||||
```
|
||||
|
||||
```csharp
|
||||
using MarqueeControlLibrary;
|
||||
```
|
||||
|
||||
3. Change the declaration of `DemoMarqueeControl` to inherit from the `MarqueeControl` class.
|
||||
|
||||
4. Build the project.
|
||||
|
||||
5. Open Form1 in the Windows Forms Designer.
|
||||
|
||||
6. Find the **MarqueeControlTest Components** tab in the **Toolbox** and open it. Drag a `DemoMarqueeControl` from the **Toolbox** onto your form.
|
||||
|
||||
7. Build the project.
|
||||
|
||||
## Set Up the Project for Design-Time Debugging
|
||||
|
||||
When you're developing a custom design-time experience, it will be necessary to debug your controls and components. There is a simple way to set up your project to allow debugging at design time. For more information, see [Walkthrough: Debugging Custom Windows Forms Controls at Design Time](walkthrough-debugging-custom-windows-forms-controls-at-design-time.md).
|
||||
|
||||
1. Right-click the `MarqueeControlLibrary` project and select **Properties**.
|
||||
|
||||
2. In the **MarqueeControlLibrary Property Pages** dialog box, select the **Debug** page.
|
||||
|
||||
3. In the **Start Action** section, select **Start External Program**. You will be debugging a separate instance of Visual Studio, so click the ellipsis () button to browse for the Visual Studio IDE. The name of the executable file is devenv.exe, and if you installed to the default location, its path is *%ProgramFiles(x86)%\Microsoft Visual Studio\2019\\\<edition>\Common7\IDE\devenv.exe*.
|
||||
|
||||
4. Select **OK** to close the dialog box.
|
||||
|
||||
5. Right-click the MarqueeControlLibrary project and select **Set as StartUp Project** to enable this debugging configuration.
|
||||
|
||||
## Checkpoint
|
||||
|
||||
You are now ready to debug the design-time behavior of your custom control. Once you've determined that the debugging environment is set up correctly, you'll test the association between the custom control and the custom designer.
|
||||
|
||||
### To test the debugging environment and the designer association
|
||||
|
||||
1. Open the MarqueeControlRootDesigner source file in the **Code Editor** and place a breakpoint on the <xref:System.Diagnostics.Trace.WriteLine%2A> statement.
|
||||
|
||||
2. Press **F5** to start the debugging session.
|
||||
|
||||
A new instance of Visual Studio is created.
|
||||
|
||||
3. In the new instance of Visual Studio, open the MarqueeControlTest solution. You can easily find the solution by selecting **Recent Projects** from the **File** menu. The MarqueeControlTest.sln solution file will be listed as the most recently used file.
|
||||
|
||||
4. Open the `DemoMarqueeControl` in the designer.
|
||||
|
||||
The debugging instance of Visual Studio obtains focus and execution stops at your breakpoint. Press **F5** to continue the debugging session.
|
||||
|
||||
At this point, everything is in place for you to develop and debug your custom control and its associated custom designer. The remainder of this article concentrates on the details of implementing features of the control and the designer.
|
||||
|
||||
## Implement the Custom Control
|
||||
|
||||
The `MarqueeControl` is a <xref:System.Windows.Forms.UserControl> with a little bit of customization. It exposes two methods: `Start`, which starts the marquee animation, and `Stop`, which stops the animation. Because the `MarqueeControl` contains child controls that implement the `IMarqueeWidget` interface, `Start` and `Stop` enumerate each child control and call the `StartMarquee` and `StopMarquee` methods, respectively, on each child control that implements `IMarqueeWidget`.
|
||||
|
||||
The appearance of the `MarqueeBorder` and `MarqueeText` controls is dependent on the layout, so `MarqueeControl` overrides the <xref:System.Windows.Forms.Control.OnLayout%2A> method and calls <xref:System.Windows.Forms.Control.PerformLayout%2A> on child controls of this type.
|
||||
|
||||
This is the extent of the `MarqueeControl` customizations. The run-time features are implemented by the `MarqueeBorder` and `MarqueeText` controls, and the design-time features are implemented by the `MarqueeBorderDesigner` and `MarqueeControlRootDesigner` classes.
|
||||
|
||||
### To implement your custom control
|
||||
|
||||
1. Open the `MarqueeControl` source file in the **Code Editor**. Implement the `Start` and `Stop` methods.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#260](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueecontrol.cs#260)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#260](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueecontrol.vb#260)]
|
||||
|
||||
2. Override the <xref:System.Windows.Forms.Control.OnLayout%2A> method.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#270](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueecontrol.cs#270)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#270](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueecontrol.vb#270)]
|
||||
|
||||
## Create a Child Control for Your Custom Control
|
||||
|
||||
The `MarqueeControl` will host two kinds of child control: the `MarqueeBorder` control and the `MarqueeText` control.
|
||||
|
||||
- `MarqueeBorder`: This control paints a border of "lights" around its edges. The lights flash in sequence, so they appear to be moving around the border. The speed at which the lights flash is controlled by a property called `UpdatePeriod`. Several other custom properties determine other aspects of the control's appearance. Two methods, called `StartMarquee` and `StopMarquee`, control when the animation starts and stops.
|
||||
|
||||
- `MarqueeText`: This control paints a flashing string. Like the `MarqueeBorder` control, the speed at which the text flashes is controlled by the `UpdatePeriod` property. The `MarqueeText` control also has the `StartMarquee` and `StopMarquee` methods in common with the `MarqueeBorder` control.
|
||||
|
||||
At design time, the `MarqueeControlRootDesigner` allows these two control types to be added to a `MarqueeControl` in any combination.
|
||||
|
||||
Common features of the two controls are factored into an interface called `IMarqueeWidget`. This allows the `MarqueeControl` to discover any Marquee-related child controls and give them special treatment.
|
||||
|
||||
To implement the periodic animation feature, you will use <xref:System.ComponentModel.BackgroundWorker> objects from the <xref:System.ComponentModel?displayProperty=nameWithType> namespace. You could use <xref:System.Windows.Forms.Timer> objects, but when many `IMarqueeWidget` objects are present, the single UI thread may be unable to keep up with the animation.
|
||||
|
||||
### To create a child control for your custom control
|
||||
|
||||
1. Add a new class item to the `MarqueeControlLibrary` project. Give the new source file a base name of "IMarqueeWidget."
|
||||
|
||||
2. Open the `IMarqueeWidget` source file in the **Code Editor** and change the declaration from `class` to `interface`:
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#2](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/imarqueewidget.cs#2)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#2](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/imarqueewidget.vb#2)]
|
||||
|
||||
3. Add the following code to the `IMarqueeWidget` interface to expose two methods and a property that manipulate the marquee animation:
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#3](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/imarqueewidget.cs#3)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#3](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/imarqueewidget.vb#3)]
|
||||
|
||||
4. Add a new **Custom Control** item to the `MarqueeControlLibrary` project. Give the new source file a base name of "MarqueeText."
|
||||
|
||||
5. Drag a <xref:System.ComponentModel.BackgroundWorker> component from the **Toolbox** onto your `MarqueeText` control. This component will allow the `MarqueeText` control to update itself asynchronously.
|
||||
|
||||
6. In the **Properties** window, set the <xref:System.ComponentModel.BackgroundWorker> component's `WorkerReportsProgress` and <xref:System.ComponentModel.BackgroundWorker.WorkerSupportsCancellation%2A> properties to **true**. These settings allow the <xref:System.ComponentModel.BackgroundWorker> component to periodically raise the <xref:System.ComponentModel.BackgroundWorker.ProgressChanged> event and to cancel asynchronous updates.
|
||||
|
||||
For more information, see [BackgroundWorker Component](backgroundworker-component.md).
|
||||
|
||||
7. Open the `MarqueeText` source file in the **Code Editor**. At the top of the file, import the following namespaces:
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#120](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueetext.cs#120)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#120](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueetext.vb#120)]
|
||||
|
||||
8. Change the declaration of `MarqueeText` to inherit from <xref:System.Windows.Forms.Label> and to implement the `IMarqueeWidget` interface:
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#130](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueetext.cs#130)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#130](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueetext.vb#130)]
|
||||
|
||||
9. Declare the instance variables that correspond to the exposed properties, and initialize them in the constructor. The `isLit` field determines if the text is to be painted in the color given by the `LightColor` property.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#140](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueetext.cs#140)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#140](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueetext.vb#140)]
|
||||
|
||||
10. Implement the `IMarqueeWidget` interface.
|
||||
|
||||
The `StartMarquee` and `StopMarquee` methods invoke the <xref:System.ComponentModel.BackgroundWorker> component's <xref:System.ComponentModel.BackgroundWorker.RunWorkerAsync%2A> and <xref:System.ComponentModel.BackgroundWorker.CancelAsync%2A> methods to start and stop the animation.
|
||||
|
||||
The <xref:System.ComponentModel.CategoryAttribute.Category%2A> and <xref:System.ComponentModel.BrowsableAttribute.Browsable%2A> attributes are applied to the `UpdatePeriod` property so it appears in a custom section of the Properties window called "Marquee."
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#150](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueetext.cs#150)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#150](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueetext.vb#150)]
|
||||
|
||||
11. Implement the property accessors. You'll expose two properties to clients: `LightColor` and `DarkColor`. The <xref:System.ComponentModel.CategoryAttribute.Category%2A> and <xref:System.ComponentModel.BrowsableAttribute.Browsable%2A> attributes are applied to these properties, so the properties appear in a custom section of the Properties window called "Marquee."
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#160](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueetext.cs#160)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#160](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueetext.vb#160)]
|
||||
|
||||
12. Implement the handlers for the <xref:System.ComponentModel.BackgroundWorker> component's <xref:System.ComponentModel.BackgroundWorker.DoWork> and <xref:System.ComponentModel.BackgroundWorker.ProgressChanged> events.
|
||||
|
||||
The <xref:System.ComponentModel.BackgroundWorker.DoWork> event handler sleeps for the number of milliseconds specified by `UpdatePeriod` then raises the <xref:System.ComponentModel.BackgroundWorker.ProgressChanged> event, until your code stops the animation by calling <xref:System.ComponentModel.BackgroundWorker.CancelAsync%2A>.
|
||||
|
||||
The <xref:System.ComponentModel.BackgroundWorker.ProgressChanged> event handler toggles the text between its light and dark state to give the appearance of flashing.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#180](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueetext.cs#180)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#180](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueetext.vb#180)]
|
||||
|
||||
13. Override the <xref:System.Windows.Forms.Control.OnPaint%2A> method to enable the animation.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#170](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueetext.cs#170)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#170](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueetext.vb#170)]
|
||||
|
||||
14. Press **F6** to build the solution.
|
||||
|
||||
## Create the MarqueeBorder Child Control
|
||||
|
||||
The `MarqueeBorder` control is slightly more sophisticated than the `MarqueeText` control. It has more properties and the animation in the <xref:System.Windows.Forms.Control.OnPaint%2A> method is more involved. In principle, it is quite similar to the `MarqueeText` control.
|
||||
|
||||
Because the `MarqueeBorder` control can have child controls, it needs to be aware of <xref:System.Windows.Forms.Control.Layout> events.
|
||||
|
||||
### To create the MarqueeBorder control
|
||||
|
||||
1. Add a new **Custom Control** item to the `MarqueeControlLibrary` project. Give the new source file a base name of "MarqueeBorder."
|
||||
|
||||
2. Drag a <xref:System.ComponentModel.BackgroundWorker> component from the **Toolbox** onto your `MarqueeBorder` control. This component will allow the `MarqueeBorder` control to update itself asynchronously.
|
||||
|
||||
3. In the **Properties** window, set the <xref:System.ComponentModel.BackgroundWorker> component's `WorkerReportsProgress` and <xref:System.ComponentModel.BackgroundWorker.WorkerSupportsCancellation%2A> properties to **true**. These settings allow the <xref:System.ComponentModel.BackgroundWorker> component to periodically raise the <xref:System.ComponentModel.BackgroundWorker.ProgressChanged> event and to cancel asynchronous updates. For more information, see [BackgroundWorker Component](backgroundworker-component.md).
|
||||
|
||||
4. In the **Properties** window, select the **Events** button. Attach handlers for the <xref:System.ComponentModel.BackgroundWorker.DoWork> and <xref:System.ComponentModel.BackgroundWorker.ProgressChanged> events.
|
||||
|
||||
5. Open the `MarqueeBorder` source file in the **Code Editor**. At the top of the file, import the following namespaces:
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#20](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueeborder.cs#20)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#20](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueeborder.vb#20)]
|
||||
|
||||
6. Change the declaration of `MarqueeBorder` to inherit from <xref:System.Windows.Forms.Panel> and to implement the `IMarqueeWidget` interface.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#30](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueeborder.cs#30)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#30](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueeborder.vb#30)]
|
||||
|
||||
7. Declare two enumerations for managing the `MarqueeBorder` control's state: `MarqueeSpinDirection`, which determines the direction in which the lights "spin" around the border, and `MarqueeLightShape`, which determines the shape of the lights (square or circular). Place these declarations before the `MarqueeBorder` class declaration.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#97](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueeborder.cs#97)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#97](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueeborder.vb#97)]
|
||||
|
||||
8. Declare the instance variables that correspond to the exposed properties, and initialize them in the constructor.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#40](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueeborder.cs#40)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#40](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueeborder.vb#40)]
|
||||
|
||||
9. Implement the `IMarqueeWidget` interface.
|
||||
|
||||
The `StartMarquee` and `StopMarquee` methods invoke the <xref:System.ComponentModel.BackgroundWorker> component's <xref:System.ComponentModel.BackgroundWorker.RunWorkerAsync%2A> and <xref:System.ComponentModel.BackgroundWorker.CancelAsync%2A> methods to start and stop the animation.
|
||||
|
||||
Because the `MarqueeBorder` control can contain child controls, the `StartMarquee` method enumerates all child controls and calls `StartMarquee` on those that implement `IMarqueeWidget`. The `StopMarquee` method has a similar implementation.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#50](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueeborder.cs#50)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#50](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueeborder.vb#50)]
|
||||
|
||||
10. Implement the property accessors. The `MarqueeBorder` control has several properties for controlling its appearance.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#60](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueeborder.cs#60)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#60](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueeborder.vb#60)]
|
||||
|
||||
11. Implement the handlers for the <xref:System.ComponentModel.BackgroundWorker> component's <xref:System.ComponentModel.BackgroundWorker.DoWork> and <xref:System.ComponentModel.BackgroundWorker.ProgressChanged> events.
|
||||
|
||||
The <xref:System.ComponentModel.BackgroundWorker.DoWork> event handler sleeps for the number of milliseconds specified by `UpdatePeriod` then raises the <xref:System.ComponentModel.BackgroundWorker.ProgressChanged> event, until your code stops the animation by calling <xref:System.ComponentModel.BackgroundWorker.CancelAsync%2A>.
|
||||
|
||||
The <xref:System.ComponentModel.BackgroundWorker.ProgressChanged> event handler increments the position of the "base" light, from which the light/dark state of the other lights is determined, and calls the <xref:System.Windows.Forms.Control.Refresh%2A> method to cause the control to repaint itself.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#90](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueeborder.cs#90)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#90](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueeborder.vb#90)]
|
||||
|
||||
12. Implement the helper methods, `IsLit` and `DrawLight`.
|
||||
|
||||
The `IsLit` method determines the color of a light at a given position. Lights that are "lit" are drawn in the color given by the `LightColor` property, and those that are "dark" are drawn in the color given by the `DarkColor` property.
|
||||
|
||||
The `DrawLight` method draws a light using the appropriate color, shape, and position.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#80](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueeborder.cs#80)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#80](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueeborder.vb#80)]
|
||||
|
||||
13. Override the <xref:System.Windows.Forms.Control.OnLayout%2A> and <xref:System.Windows.Forms.Control.OnPaint%2A> methods.
|
||||
|
||||
The <xref:System.Windows.Forms.Control.OnPaint%2A> method draws the lights along the edges of the `MarqueeBorder` control.
|
||||
|
||||
Because the <xref:System.Windows.Forms.Control.OnPaint%2A> method depends on the dimensions of the `MarqueeBorder` control, you need to call it whenever the layout changes. To achieve this, override <xref:System.Windows.Forms.Control.OnLayout%2A> and call <xref:System.Windows.Forms.Control.Refresh%2A>.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#70](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueeborder.cs#70)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#70](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueeborder.vb#70)]
|
||||
|
||||
## Create a Custom Designer to Shadow and Filter Properties
|
||||
|
||||
The `MarqueeControlRootDesigner` class provides the implementation for the root designer. In addition to this designer, which operates on the `MarqueeControl`, you'll need a custom designer that is specifically associated with the `MarqueeBorder` control. This designer provides custom behavior that is appropriate in the context of the custom root designer.
|
||||
|
||||
Specifically, the `MarqueeBorderDesigner` will "shadow" and filter certain properties on the `MarqueeBorder` control, changing their interaction with the design environment.
|
||||
|
||||
Intercepting calls to a component's property accessor is known as "shadowing." It allows a designer to track the value set by the user and optionally pass that value to the component being designed.
|
||||
|
||||
For this example, the <xref:System.Windows.Forms.Control.Visible%2A> and <xref:System.Windows.Forms.Control.Enabled%2A> properties will be shadowed by the `MarqueeBorderDesigner`, which prevents the user from making the `MarqueeBorder` control invisible or disabled during design time.
|
||||
|
||||
Designers can also add and remove properties. For this example, the <xref:System.Windows.Forms.Control.Padding%2A> property will be removed at design time, because the `MarqueeBorder` control programmatically sets the padding based on the size of the lights specified by the `LightSize` property.
|
||||
|
||||
The base class for `MarqueeBorderDesigner` is <xref:System.ComponentModel.Design.ComponentDesigner>, which has methods that can change the attributes, properties, and events exposed by a control at design time:
|
||||
|
||||
- <xref:System.ComponentModel.Design.ComponentDesigner.PreFilterProperties%2A>
|
||||
|
||||
- <xref:System.ComponentModel.Design.ComponentDesigner.PostFilterProperties%2A>
|
||||
|
||||
- <xref:System.ComponentModel.Design.ComponentDesigner.PreFilterAttributes%2A>
|
||||
|
||||
- <xref:System.ComponentModel.Design.ComponentDesigner.PostFilterAttributes%2A>
|
||||
|
||||
- <xref:System.ComponentModel.Design.ComponentDesigner.PreFilterEvents%2A>
|
||||
|
||||
- <xref:System.ComponentModel.Design.ComponentDesigner.PostFilterEvents%2A>
|
||||
|
||||
When changing the public interface of a component using these methods, follow these rules:
|
||||
|
||||
- Add or remove items in the `PreFilter` methods only
|
||||
|
||||
- Modify existing items in the `PostFilter` methods only
|
||||
|
||||
- Always call the base implementation first in the `PreFilter` methods
|
||||
|
||||
- Always call the base implementation last in the `PostFilter` methods
|
||||
|
||||
Adhering to these rules ensures that all designers in the design-time environment have a consistent view of all components being designed.
|
||||
|
||||
The <xref:System.ComponentModel.Design.ComponentDesigner> class provides a dictionary for managing the values of shadowed properties, which relieves you of the need to create specific instance variables.
|
||||
|
||||
### To create a custom designer to shadow and filter properties
|
||||
|
||||
1. Right-click the **Design** folder and add a new class. Give the source file a base name of **MarqueeBorderDesigner**.
|
||||
|
||||
2. Open the MarqueeBorderDesigner source file in the **Code Editor**. At the top of the file, import the following namespaces:
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#420](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueeborderdesigner.cs#420)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#420](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueeborderdesigner.vb#420)]
|
||||
|
||||
3. Change the declaration of `MarqueeBorderDesigner` to inherit from <xref:System.Windows.Forms.Design.ParentControlDesigner>.
|
||||
|
||||
Because the `MarqueeBorder` control can contain child controls, `MarqueeBorderDesigner` inherits from <xref:System.Windows.Forms.Design.ParentControlDesigner>, which handles the parent-child interaction.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#430](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueeborderdesigner.cs#430)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#430](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueeborderdesigner.vb#430)]
|
||||
|
||||
4. Override the base implementation of <xref:System.ComponentModel.Design.ComponentDesigner.PreFilterProperties%2A>.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#450](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueeborderdesigner.cs#450)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#450](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueeborderdesigner.vb#450)]
|
||||
|
||||
5. Implement the <xref:System.Windows.Forms.Control.Enabled%2A> and <xref:System.Windows.Forms.Control.Visible%2A> properties. These implementations shadow the control's properties.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#440](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueeborderdesigner.cs#440)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#440](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueeborderdesigner.vb#440)]
|
||||
|
||||
## Handle Component Changes
|
||||
|
||||
The `MarqueeControlRootDesigner` class provides the custom design-time experience for your `MarqueeControl` instances. Most of the design-time functionality is inherited from the <xref:System.Windows.Forms.Design.DocumentDesigner> class. Your code will implement two specific customizations: handling component changes, and adding designer verbs.
|
||||
|
||||
As users design their `MarqueeControl` instances, your root designer will track changes to the `MarqueeControl` and its child controls. The design-time environment offers a convenient service, <xref:System.ComponentModel.Design.IComponentChangeService>, for tracking changes to component state.
|
||||
|
||||
You acquire a reference to this service by querying the environment with the <xref:System.ComponentModel.Design.ComponentDesigner.GetService%2A> method. If the query is successful, your designer can attach a handler for the <xref:System.ComponentModel.Design.IComponentChangeService.ComponentChanged> event and perform whatever tasks are required to maintain a consistent state at design time.
|
||||
|
||||
In the case of the `MarqueeControlRootDesigner` class, you will call the <xref:System.Windows.Forms.Control.Refresh%2A> method on each `IMarqueeWidget` object contained by the `MarqueeControl`. This will cause the `IMarqueeWidget` object to repaint itself appropriately when properties like its parent's <xref:System.Windows.Forms.Control.Size%2A> are changed.
|
||||
|
||||
### To handle component changes
|
||||
|
||||
1. Open the `MarqueeControlRootDesigner` source file in the **Code Editor** and override the <xref:System.Windows.Forms.Design.DocumentDesigner.Initialize%2A> method. Call the base implementation of <xref:System.Windows.Forms.Design.DocumentDesigner.Initialize%2A> and query for the <xref:System.ComponentModel.Design.IComponentChangeService>.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#580](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueecontrolrootdesigner.cs#580)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#580](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueecontrolrootdesigner.vb#580)]
|
||||
|
||||
2. Implement the <xref:System.ComponentModel.Design.IComponentChangeService.OnComponentChanged%2A> event handler. Test the sending component's type, and if it is an `IMarqueeWidget`, call its <xref:System.Windows.Forms.Control.Refresh%2A> method.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#560](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueecontrolrootdesigner.cs#560)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#560](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueecontrolrootdesigner.vb#560)]
|
||||
|
||||
## Add Designer Verbs to your Custom Designer
|
||||
|
||||
A designer verb is a menu command linked to an event handler. Designer verbs are added to a component's shortcut menu at design time. For more information, see <xref:System.ComponentModel.Design.DesignerVerb>.
|
||||
|
||||
You will add two designer verbs to your designers: **Run Test** and **Stop Test**. These verbs will allow you to view the run-time behavior of the `MarqueeControl` at design time. These verbs will be added to `MarqueeControlRootDesigner`.
|
||||
|
||||
When **Run Test** is invoked, the verb event handler will call the `StartMarquee` method on the `MarqueeControl`. When **Stop Test** is invoked, the verb event handler will call the `StopMarquee` method on the `MarqueeControl`. The implementation of the `StartMarquee` and `StopMarquee` methods call these methods on contained controls that implement `IMarqueeWidget`, so any contained `IMarqueeWidget` controls will also participate in the test.
|
||||
|
||||
### To add designer verbs to your custom designers
|
||||
|
||||
1. In the `MarqueeControlRootDesigner` class, add event handlers named `OnVerbRunTest` and `OnVerbStopTest`.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#570](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueecontrolrootdesigner.cs#570)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#570](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueecontrolrootdesigner.vb#570)]
|
||||
|
||||
2. Connect these event handlers to their corresponding designer verbs. `MarqueeControlRootDesigner` inherits a <xref:System.ComponentModel.Design.DesignerVerbCollection> from its base class. You will create two new <xref:System.ComponentModel.Design.DesignerVerb> objects and add them to this collection in the <xref:System.Windows.Forms.Design.DocumentDesigner.Initialize%2A> method.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#590](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueecontrolrootdesigner.cs#590)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#590](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueecontrolrootdesigner.vb#590)]
|
||||
|
||||
## Create a Custom UITypeEditor
|
||||
|
||||
When you create a custom design-time experience for users, it is often desirable to create a custom interaction with the Properties window. You can accomplish this by creating a <xref:System.Drawing.Design.UITypeEditor>.
|
||||
|
||||
The `MarqueeBorder` control exposes several properties in the Properties window. Two of these properties, `MarqueeSpinDirection` and `MarqueeLightShape` are represented by enumerations. To illustrate the use of a UI type editor, the `MarqueeLightShape` property will have an associated <xref:System.Drawing.Design.UITypeEditor> class.
|
||||
|
||||
### To create a custom UI type editor
|
||||
|
||||
1. Open the `MarqueeBorder` source file in the **Code Editor**.
|
||||
|
||||
2. In the definition of the `MarqueeBorder` class, declare a class called `LightShapeEditor` that derives from <xref:System.Drawing.Design.UITypeEditor>.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#96](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueeborder.cs#96)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#96](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueeborder.vb#96)]
|
||||
|
||||
3. Declare an <xref:System.Windows.Forms.Design.IWindowsFormsEditorService> instance variable called `editorService`.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#92](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueeborder.cs#92)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#92](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueeborder.vb#92)]
|
||||
|
||||
4. Override the <xref:System.Drawing.Design.UITypeEditor.GetEditStyle%2A> method. This implementation returns <xref:System.Drawing.Design.UITypeEditorEditStyle.DropDown>, which tells the design environment how to display the `LightShapeEditor`.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#93](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueeborder.cs#93)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#93](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueeborder.vb#93)]
|
||||
|
||||
5. Override the <xref:System.Drawing.Design.UITypeEditor.EditValue%2A> method. This implementation queries the design environment for an <xref:System.Windows.Forms.Design.IWindowsFormsEditorService> object. If successful, it creates a `LightShapeSelectionControl`. The <xref:System.Windows.Forms.Design.IWindowsFormsEditorService.DropDownControl%2A> method is invoked to start the `LightShapeEditor`. The return value from this invocation is returned to the design environment.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#94](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/marqueeborder.cs#94)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#94](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/marqueeborder.vb#94)]
|
||||
|
||||
## Create a View Control for your Custom UITypeEditor
|
||||
|
||||
The `MarqueeLightShape` property supports two types of light shapes: `Square` and `Circle`. You will create a custom control used solely for the purpose of graphically displaying these values in the Properties window. This custom control will be used by your <xref:System.Drawing.Design.UITypeEditor> to interact with the Properties window.
|
||||
|
||||
### To create a view control for your custom UI type editor
|
||||
|
||||
1. Add a new <xref:System.Windows.Forms.UserControl> item to the `MarqueeControlLibrary` project. Give the new source file a base name of **LightShapeSelectionControl**.
|
||||
|
||||
2. Drag two <xref:System.Windows.Forms.Panel> controls from the **Toolbox** onto the `LightShapeSelectionControl`. Name them `squarePanel` and `circlePanel`. Arrange them side by side. Set the <xref:System.Windows.Forms.Control.Size%2A> property of both <xref:System.Windows.Forms.Panel> controls to **(60, 60)**. Set the <xref:System.Windows.Forms.Control.Location%2A> property of the `squarePanel` control to **(8, 10)**. Set the <xref:System.Windows.Forms.Control.Location%2A> property of the `circlePanel` control to **(80, 10)**. Finally, set the <xref:System.Windows.Forms.Control.Size%2A> property of the `LightShapeSelectionControl` to **(150, 80)**.
|
||||
|
||||
3. Open the `LightShapeSelectionControl` source file in the **Code Editor**. At the top of the file, import the <xref:System.Windows.Forms.Design?displayProperty=nameWithType> namespace:
|
||||
|
||||
```vb
|
||||
Imports System.Windows.Forms.Design
|
||||
```
|
||||
|
||||
```csharp
|
||||
using System.Windows.Forms.Design;
|
||||
```
|
||||
|
||||
4. Implement <xref:System.Windows.Forms.Control.Click> event handlers for the `squarePanel` and `circlePanel` controls. These methods invoke <xref:System.Windows.Forms.Design.IWindowsFormsEditorService.CloseDropDown%2A> to end the custom <xref:System.Drawing.Design.UITypeEditor> editing session.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#390](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/lightshapeselectioncontrol.cs#390)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#390](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/lightshapeselectioncontrol.vb#390)]
|
||||
|
||||
5. Declare an <xref:System.Windows.Forms.Design.IWindowsFormsEditorService> instance variable called `editorService`.
|
||||
|
||||
```vb
|
||||
Private editorService As IWindowsFormsEditorService
|
||||
```
|
||||
|
||||
```csharp
|
||||
private IWindowsFormsEditorService editorService;
|
||||
```
|
||||
|
||||
6. Declare a `MarqueeLightShape` instance variable called `lightShapeValue`.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#330](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/lightshapeselectioncontrol.cs#330)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#330](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/lightshapeselectioncontrol.vb#330)]
|
||||
|
||||
7. In the `LightShapeSelectionControl` constructor, attach the <xref:System.Windows.Forms.Control.Click> event handlers to the `squarePanel` and `circlePanel` controls' <xref:System.Windows.Forms.Control.Click> events. Also, define a constructor overload that assigns the `MarqueeLightShape` value from the design environment to the `lightShapeValue` field.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#340](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/lightshapeselectioncontrol.cs#340)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#340](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/lightshapeselectioncontrol.vb#340)]
|
||||
|
||||
8. In the <xref:System.ComponentModel.Component.Dispose%2A> method, detach the <xref:System.Windows.Forms.Control.Click> event handlers.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#350](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/lightshapeselectioncontrol.cs#350)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#350](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/lightshapeselectioncontrol.vb#350)]
|
||||
|
||||
9. In **Solution Explorer**, click the **Show All Files** button. Open the LightShapeSelectionControl.Designer.cs or LightShapeSelectionControl.Designer.vb file, and remove the default definition of the <xref:System.ComponentModel.Component.Dispose%2A> method.
|
||||
|
||||
10. Implement the `LightShape` property.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#360](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/lightshapeselectioncontrol.cs#360)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#360](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/lightshapeselectioncontrol.vb#360)]
|
||||
|
||||
11. Override the <xref:System.Windows.Forms.Control.OnPaint%2A> method. This implementation will draw a filled square and circle. It will also highlight the selected value by drawing a border around one shape or the other.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.Design.DocumentDesigner#380](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/CS/lightshapeselectioncontrol.cs#380)]
|
||||
[!code-vb[System.Windows.Forms.Design.DocumentDesigner#380](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Design.DocumentDesigner/VB/lightshapeselectioncontrol.vb#380)]
|
||||
|
||||
## Test your Custom Control in the Designer
|
||||
|
||||
At this point, you can build the `MarqueeControlLibrary` project. Test your implementation by creating a control that inherits from the `MarqueeControl` class and using it on a form.
|
||||
|
||||
### To create a custom MarqueeControl implementation
|
||||
|
||||
1. Open `DemoMarqueeControl` in the Windows Forms Designer. This creates an instance of the `DemoMarqueeControl` type and displays it in an instance of the `MarqueeControlRootDesigner` type.
|
||||
|
||||
2. In the **Toolbox**, open the **MarqueeControlLibrary Components** tab. You will see the `MarqueeBorder` and `MarqueeText` controls available for selection.
|
||||
|
||||
3. Drag an instance of the `MarqueeBorder` control onto the `DemoMarqueeControl` design surface. Dock this `MarqueeBorder` control to the parent control.
|
||||
|
||||
4. Drag an instance of the `MarqueeText` control onto the `DemoMarqueeControl` design surface.
|
||||
|
||||
5. Build the solution.
|
||||
|
||||
6. Right-click the `DemoMarqueeControl` and from the shortcut menu select the **Run Test** option to start the animation. Click **Stop Test** to stop the animation.
|
||||
|
||||
7. Open **Form1** in Design view.
|
||||
|
||||
8. Place two <xref:System.Windows.Forms.Button> controls on the form. Name them `startButton` and `stopButton`, and change the <xref:System.Windows.Forms.Control.Text%2A> property values to **Start** and **Stop**, respectively.
|
||||
|
||||
9. Implement <xref:System.Windows.Forms.Control.Click> event handlers for both <xref:System.Windows.Forms.Button> controls.
|
||||
|
||||
10. In the **Toolbox**, open the **MarqueeControlTest Components** tab. You will see the `DemoMarqueeControl` available for selection.
|
||||
|
||||
11. Drag an instance of `DemoMarqueeControl` onto the **Form1** design surface.
|
||||
|
||||
12. In the <xref:System.Windows.Forms.Control.Click> event handlers, invoke the `Start` and `Stop` methods on the `DemoMarqueeControl`.
|
||||
|
||||
```vb
|
||||
Private Sub startButton_Click(sender As Object, e As System.EventArgs)
|
||||
Me.demoMarqueeControl1.Start()
|
||||
End Sub 'startButton_Click
|
||||
|
||||
Private Sub stopButton_Click(sender As Object, e As System.EventArgs)
|
||||
Me.demoMarqueeControl1.Stop()
|
||||
End Sub 'stopButton_Click
|
||||
```
|
||||
|
||||
```csharp
|
||||
private void startButton_Click(object sender, System.EventArgs e)
|
||||
{
|
||||
this.demoMarqueeControl1.Start();
|
||||
}
|
||||
|
||||
private void stopButton_Click(object sender, System.EventArgs e)
|
||||
{
|
||||
this.demoMarqueeControl1.Stop();
|
||||
}
|
||||
```
|
||||
|
||||
13. Set the `MarqueeControlTest` project as the startup project and run it. You will see the form displaying your `DemoMarqueeControl`. Select the **Start** button to start the animation. You should see the text flashing and the lights moving around the border.
|
||||
|
||||
## Next steps
|
||||
|
||||
The `MarqueeControlLibrary` demonstrates a simple implementation of custom controls and associated designers. You can make this sample more sophisticated in several ways:
|
||||
|
||||
- Change the property values for the `DemoMarqueeControl` in the designer. Add more `MarqueBorder` controls and dock them within their parent instances to create a nested effect. Experiment with different settings for the `UpdatePeriod` and the light-related properties.
|
||||
|
||||
- Author your own implementations of `IMarqueeWidget`. You could, for example, create a flashing "neon sign" or an animated sign with multiple images.
|
||||
|
||||
- Further customize the design-time experience. You could try shadowing more properties than <xref:System.Windows.Forms.Control.Enabled%2A> and <xref:System.Windows.Forms.Control.Visible%2A>, and you could add new properties. Add new designer verbs to simplify common tasks like docking child controls.
|
||||
|
||||
- License the `MarqueeControl`.
|
||||
|
||||
- Control how your controls are serialized and how code is generated for them. For more information, see [Dynamic Source Code Generation and Compilation](https://docs.microsoft.com/dotnet/framework/reflection-and-codedom/dynamic-source-code-generation-and-compilation).
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.UserControl>
|
||||
- <xref:System.Windows.Forms.Design.ParentControlDesigner>
|
||||
- <xref:System.Windows.Forms.Design.DocumentDesigner>
|
||||
- <xref:System.ComponentModel.Design.IRootDesigner>
|
||||
- <xref:System.ComponentModel.Design.DesignerVerb>
|
||||
- <xref:System.Drawing.Design.UITypeEditor>
|
||||
- <xref:System.ComponentModel.BackgroundWorker>
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
---
|
||||
title: "Walkthrough: Creating an Explorer Style Interface with the ListView and TreeView Controls Using the Designer"
|
||||
description: Learn how to create an Explorer style interface with the Windows Forms ListView and TreeView controls using the Designer.
|
||||
ms.date: "03/30/2017"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
helpviewer_keywords:
|
||||
- "Explorer-style applications [Windows Forms], walkthroughs"
|
||||
- "TreeView control [Windows Forms], ListView controls used with"
|
||||
- "ListView control [Windows Forms], TreeView controls used with"
|
||||
- "Explorer-style applications"
|
||||
- "TreeView control [Windows Forms], using for explorer-style interface"
|
||||
- "ListView control [Windows Forms], explorer style interface"
|
||||
- "ListView control [Windows Forms], explorer-style interface"
|
||||
ms.assetid: 9e5e7721-19e2-4890-b273-a43589fe99ff
|
||||
---
|
||||
# Walkthrough: Creating an Explorer Style Interface with the ListView and TreeView Controls Using the Designer
|
||||
|
||||
One of the benefits of Visual Studio is the ability to create professional-looking Windows Forms applications in a short of amount of time. A common scenario is creating a user interface (UI) with <xref:System.Windows.Forms.ListView> and <xref:System.Windows.Forms.TreeView> controls that resembles the Windows Explorer feature of Windows operating systems. Windows Explorer displays a hierarchical structure of the files and folders on a user's computer.
|
||||
|
||||
### To create the form containing a ListView and TreeView control
|
||||
|
||||
1. On the **File** menu, point to **New**, and then click **Project**.
|
||||
|
||||
2. In the **New Project** dialog box, do the following:
|
||||
|
||||
1. In the categories, choose either **Visual Basic** or **Visual C#**.
|
||||
|
||||
2. In the list of templates, choose **Windows Forms Application**.
|
||||
|
||||
3. Click **OK**. A new Windows Forms project is created.
|
||||
|
||||
4. Add a <xref:System.Windows.Forms.SplitContainer> control to the form and set its <xref:System.Windows.Forms.SplitContainer.Dock%2A> property to <xref:System.Windows.Forms.DockStyle.Fill>.
|
||||
|
||||
5. Add an <xref:System.Windows.Forms.ImageList> named `imageList1` to the form and use the Properties window to add two images: a folder image and a document image, in that order.
|
||||
|
||||
6. Add a <xref:System.Windows.Forms.TreeView> control named `treeview1` to the form, and position it on the left side of the <xref:System.Windows.Forms.SplitContainer> control. In the Properties window for `treeView1` do the following:
|
||||
|
||||
1. Set the <xref:System.Windows.Forms.Control.Dock%2A> property to <xref:System.Windows.Forms.DockStyle.Fill>.
|
||||
|
||||
2. Set the <xref:System.Windows.Forms.TreeView.ImageList%2A> property to `imagelist1.`
|
||||
|
||||
7. Add a <xref:System.Windows.Forms.ListView> control named `listView1` to the form, and position it on the right side of the <xref:System.Windows.Forms.SplitContainer> control. In the Properties window for `listview1` do the following:
|
||||
|
||||
1. Set the <xref:System.Windows.Forms.Control.Dock%2A> property to <xref:System.Windows.Forms.DockStyle.Fill>.
|
||||
|
||||
2. Set the <xref:System.Windows.Forms.ListView.View%2A> property to <xref:System.Windows.Forms.View.Details>.
|
||||
|
||||
3. Open the ColumnHeader Collection Editor by clicking the ellipses () in the <xref:System.Windows.Forms.ListView.Columns%2A> property**.** Add three columns and set their <xref:System.Windows.Forms.ColumnHeader.Text%2A> property to `Name`, `Type`, and `Last Modified`, respectively. Click **OK** to close the dialog box.
|
||||
|
||||
4. Set the <xref:System.Windows.Forms.ListView.SmallImageList%2A> property to `imageList1.`
|
||||
|
||||
8. Implement the code to populate the <xref:System.Windows.Forms.TreeView> with nodes and subnodes. Add this code to the `Form1` class.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.ExplorerStyleInterface#1](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.ExplorerStyleInterface/CS/Form1.cs#1)]
|
||||
[!code-vb[System.Windows.Forms.ExplorerStyleInterface#1](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.ExplorerStyleInterface/VB/Form1.vb#1)]
|
||||
|
||||
9. Since the previous code uses the System.IO namespace, add the appropriate using or import statement at the top of the form.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.ExplorerStyleInterface#4](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.ExplorerStyleInterface/CS/Form1.cs#4)]
|
||||
[!code-vb[System.Windows.Forms.ExplorerStyleInterface#4](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.ExplorerStyleInterface/VB/Form1.vb#4)]
|
||||
|
||||
10. Call the set-up method from the previous step in the form's constructor or <xref:System.Windows.Forms.Form.Load> event-handling method. Add this code to the form constructor.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.ExplorerStyleInterface#2](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.ExplorerStyleInterface/CS/Form1.cs#2)]
|
||||
[!code-vb[System.Windows.Forms.ExplorerStyleInterface#2](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.ExplorerStyleInterface/VB/Form1.vb#2)]
|
||||
|
||||
11. Handle the <xref:System.Windows.Forms.TreeView.NodeMouseClick> event for `treeview1`**,** and implement the code to populate `listview1` with a node's contents when a node is clicked. Add this code to the `Form1` class.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.ExplorerStyleInterface#3](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.ExplorerStyleInterface/CS/Form1.cs#3)]
|
||||
[!code-vb[System.Windows.Forms.ExplorerStyleInterface#3](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.ExplorerStyleInterface/VB/Form1.vb#3)]
|
||||
|
||||
If you are using C#, make sure you have the <xref:System.Windows.Forms.TreeView.NodeMouseClick> event associated with its event-handling method. Add this code to the form constructor.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.ExplorerStyleInterface#5](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.ExplorerStyleInterface/CS/Form1.cs#5)]
|
||||
|
||||
## Testing the Application
|
||||
|
||||
You can now test the form to make sure it behaves as expected.
|
||||
|
||||
#### To test the form
|
||||
|
||||
- Press F5 to run the application.
|
||||
|
||||
You will see a split form containing a <xref:System.Windows.Forms.TreeView> control that displays your project directory on the left side, and a <xref:System.Windows.Forms.ListView> control on the right side with three columns. You can traverse the <xref:System.Windows.Forms.TreeView> by selecting directory nodes, and the <xref:System.Windows.Forms.ListView> is populated with the contents of the selected directory.
|
||||
|
||||
## Next Steps
|
||||
|
||||
This application gives you an example of a way you can use <xref:System.Windows.Forms.TreeView> and <xref:System.Windows.Forms.ListView> controls together. For more information on these controls, see the following topics:
|
||||
|
||||
- [How to: Add Custom Information to a TreeView or ListView Control (Windows Forms)](add-custom-information-to-a-treeview-or-listview-control-wf.md)
|
||||
|
||||
- [How to: Add Search Capabilities to a ListView Control](how-to-add-search-capabilities-to-a-listview-control.md)
|
||||
|
||||
- [How to: Attach a ShortCut Menu to a TreeView Node](how-to-attach-a-shortcut-menu-to-a-treeview-node.md)
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.ListView>
|
||||
- <xref:System.Windows.Forms.TreeView>
|
||||
- [ListView Control](listview-control-windows-forms.md)
|
||||
- [How to: Add and Remove Nodes with the Windows Forms TreeView Control](how-to-add-and-remove-nodes-with-the-windows-forms-treeview-control.md)
|
||||
- [How to: Add and Remove Items with the Windows Forms ListView Control](how-to-add-and-remove-items-with-the-windows-forms-listview-control.md)
|
||||
- [How to: Add Columns to the Windows Forms ListView Control](how-to-add-columns-to-the-windows-forms-listview-control.md)
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
---
|
||||
title: "Custom Control Painting and Rendering"
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "custom controls [Windows Forms], rendering"
|
||||
- "custom controls [Windows Forms], painting"
|
||||
- "user controls [Windows Forms], painting"
|
||||
ms.assetid: a09dbf76-0966-4cbf-a66a-2083ba98e068
|
||||
---
|
||||
# Custom Control Painting and Rendering
|
||||
Custom painting of controls is one of the many complicated tasks made easy by the .NET Framework. When authoring a custom control, you have many options regarding your control's graphical appearance. If you are authoring a control that inherits from the `Control`, you must provide code that allows your control to render its graphical representation. If you are creating a user control by inheriting from the `UserControl`, or are inheriting from one of the Windows Forms controls, you may override the standard graphical representation and provide your own graphics code. If you want to provide custom rendering for the constituent controls of a `UserControl` you are authoring, your options become more limited, but still allow a wide range of graphical possibilities for your controls and applications.
|
||||
|
||||
## In This Section
|
||||
[Rendering a Windows Forms Control](rendering-a-windows-forms-control.md)
|
||||
Shows how to program the logic that displays a control.
|
||||
|
||||
[User-Drawn Controls](user-drawn-controls.md)
|
||||
Gives an overview of the steps involved in writing and overriding rendering code for your control.
|
||||
|
||||
[Constituent Controls](constituent-controls.md)
|
||||
Describes how to implement custom rendering code for constituent controls in your user controls and forms.
|
||||
|
||||
[How to: Make Your Control Invisible at Run Time](how-to-make-your-control-invisible-at-run-time.md)
|
||||
Shows how to use the <xref:System.Windows.Forms.Control.Visible%2A> property to hide and show a control.
|
||||
|
||||
[How to: Give Your Control a Transparent Background](how-to-give-your-control-a-transparent-background.md)
|
||||
Shows how to use the <xref:System.Windows.Forms.Control.SetStyle%2A> method to create a background color that is opaque, transparent, or partially transparent.
|
||||
|
||||
[Rendering Controls with Visual Styles](rendering-controls-with-visual-styles.md)
|
||||
Shows how to render controls using visual styles in operating systems that support them.
|
||||
|
||||
## Reference
|
||||
<xref:System.Windows.Forms.Control>
|
||||
Describes this class and has links to all of its members.
|
||||
|
||||
<xref:System.Windows.Forms.UserControl>
|
||||
Describes this class and has links to all of its members.
|
||||
|
||||
<xref:System.Windows.Forms.Control.OnPaint%2A>
|
||||
Describes this method.
|
||||
|
||||
## Related Sections
|
||||
[How to: Create Graphics Objects for Drawing](../advanced/how-to-create-graphics-objects-for-drawing.md)
|
||||
Introduces GDI+ graphics functionality from a Visual Studio perspective and gives links to more information.
|
||||
|
||||
[Varieties of Custom Controls](varieties-of-custom-controls.md)
|
||||
Describes the kinds of custom controls you can author.
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
---
|
||||
title: Customize Cells and Columns in DataGridView Control by Extending Their Behavior and Appearance
|
||||
ms.date: "03/30/2017"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
helpviewer_keywords:
|
||||
- "DataGridView control [Windows Forms], cell customization"
|
||||
- "columns [Windows Forms], customizing in DataGridView control"
|
||||
- "cells [Windows Forms], customizing in DataGridView control"
|
||||
ms.assetid: 9b7dc7b6-5ce6-4566-9949-902f74f17a81
|
||||
---
|
||||
# How to: Customize Cells and Columns in the Windows Forms DataGridView Control by Extending Their Behavior and Appearance
|
||||
The <xref:System.Windows.Forms.DataGridView> control provides a number of ways to customize its appearance and behavior using properties, events, and companion classes. Occasionally, you may have requirements for your cells that go beyond what these features can provide. You can create your own custom <xref:System.Windows.Forms.DataGridViewCell> class to provide extended functionality.
|
||||
|
||||
You create a custom <xref:System.Windows.Forms.DataGridViewCell> class by deriving from the <xref:System.Windows.Forms.DataGridViewCell> base class or one of its derived classes. Although you can display any type of cell in any type of column, you will typically also create a custom <xref:System.Windows.Forms.DataGridViewColumn> class specialized for displaying your cell type. Column classes derive from <xref:System.Windows.Forms.DataGridViewColumn> or one of its derived types.
|
||||
|
||||
In the following code example, you will create a custom cell class called `DataGridViewRolloverCell` that detects when the mouse enters and leaves the cell boundaries. While the mouse is within the cell's bounds, an inset rectangle is drawn. This new type derives from <xref:System.Windows.Forms.DataGridViewTextBoxCell> and behaves in all other respects as its base class. The companion column class is called `DataGridViewRolloverColumn`.
|
||||
|
||||
To use these classes, create a form containing a <xref:System.Windows.Forms.DataGridView> control, add one or more `DataGridViewRolloverColumn` objects to the <xref:System.Windows.Forms.DataGridView.Columns%2A> collection, and populate the control with rows containing values.
|
||||
|
||||
> [!NOTE]
|
||||
> This example will not work correctly if you add empty rows. Empty rows are created, for example, when you add rows to the control by setting the <xref:System.Windows.Forms.DataGridView.RowCount%2A> property. This is because the rows added in this case are automatically shared, which means that `DataGridViewRolloverCell` objects are not instantiated until you click on individual cells, thereby causing the associated rows to become unshared.
|
||||
|
||||
Because this type of cell customization requires unshared rows, it is not appropriate for use with large data sets. For more information about row sharing, see [Best Practices for Scaling the Windows Forms DataGridView Control](best-practices-for-scaling-the-windows-forms-datagridview-control.md).
|
||||
|
||||
> [!NOTE]
|
||||
> When you derive from <xref:System.Windows.Forms.DataGridViewCell> or <xref:System.Windows.Forms.DataGridViewColumn> and add new properties to the derived class, be sure to override the `Clone` method to copy the new properties during cloning operations. You should also call the base class's `Clone` method so that the properties of the base class are copied to the new cell or column.
|
||||
|
||||
### To customize cells and columns in the DataGridView control
|
||||
|
||||
1. Derive a new cell class, called `DataGridViewRolloverCell`, from the <xref:System.Windows.Forms.DataGridViewTextBoxCell> type.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.DataGridViewRolloverCell#201](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewRolloverCell/CS/rollovercell.cs#201)]
|
||||
[!code-vb[System.Windows.Forms.DataGridViewRolloverCell#201](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewRolloverCell/VB/rollovercell.vb#201)]
|
||||
[!code-csharp[System.Windows.Forms.DataGridViewRolloverCell#202](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewRolloverCell/CS/rollovercell.cs#202)]
|
||||
[!code-vb[System.Windows.Forms.DataGridViewRolloverCell#202](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewRolloverCell/VB/rollovercell.vb#202)]
|
||||
|
||||
2. Override the <xref:System.Windows.Forms.DataGridViewTextBoxCell.Paint%2A> method in the `DataGridViewRolloverCell` class. In the override, first call the base class implementation, which handles the hosted text box functionality. Then use the control's <xref:System.Windows.Forms.Control.PointToClient%2A> method to transform the cursor position (in screen coordinates) to the <xref:System.Windows.Forms.DataGridView> client area's coordinates. If the mouse coordinates fall within the bounds of the cell, draw the inset rectangle.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.DataGridViewRolloverCell#210](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewRolloverCell/CS/rollovercell.cs#210)]
|
||||
[!code-vb[System.Windows.Forms.DataGridViewRolloverCell#210](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewRolloverCell/VB/rollovercell.vb#210)]
|
||||
|
||||
3. Override the <xref:System.Windows.Forms.DataGridViewCell.OnMouseEnter%2A> and <xref:System.Windows.Forms.DataGridViewCell.OnMouseLeave%2A> methods in the `DataGridViewRolloverCell` class to force cells to repaint themselves when the mouse pointer enters or leaves them.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.DataGridViewRolloverCell#220](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewRolloverCell/CS/rollovercell.cs#220)]
|
||||
[!code-vb[System.Windows.Forms.DataGridViewRolloverCell#220](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewRolloverCell/VB/rollovercell.vb#220)]
|
||||
|
||||
4. Derive a new class, called `DataGridViewRolloverCellColumn`, from the <xref:System.Windows.Forms.DataGridViewColumn> type. In the constructor, assign a new `DataGridViewRolloverCell` object to its <xref:System.Windows.Forms.DataGridViewColumn.CellTemplate%2A> property.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.DataGridViewRolloverCell#300](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewRolloverCell/CS/rollovercell.cs#300)]
|
||||
[!code-vb[System.Windows.Forms.DataGridViewRolloverCell#300](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewRolloverCell/VB/rollovercell.vb#300)]
|
||||
|
||||
## Example
|
||||
The complete code example includes a small test form that demonstrates the behavior of the custom cell type.
|
||||
|
||||
[!code-csharp[System.Windows.Forms.DataGridViewRolloverCell#000](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewRolloverCell/CS/rollovercell.cs#000)]
|
||||
[!code-vb[System.Windows.Forms.DataGridViewRolloverCell#000](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewRolloverCell/VB/rollovercell.vb#000)]
|
||||
|
||||
## Compiling the Code
|
||||
This example requires:
|
||||
|
||||
- References to the System, System.Windows.Forms, and System.Drawing assemblies.
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridView>
|
||||
- <xref:System.Windows.Forms.DataGridViewCell>
|
||||
- <xref:System.Windows.Forms.DataGridViewColumn>
|
||||
- [Customizing the Windows Forms DataGridView Control](customizing-the-windows-forms-datagridview-control.md)
|
||||
- [DataGridView Control Architecture](datagridview-control-architecture-windows-forms.md)
|
||||
- [Column Types in the Windows Forms DataGridView Control](column-types-in-the-windows-forms-datagridview-control.md)
|
||||
- [Best Practices for Scaling the Windows Forms DataGridView Control](best-practices-for-scaling-the-windows-forms-datagridview-control.md)
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
---
|
||||
title: Customize the Appearance of Cells in DataGridView Control
|
||||
ms.date: "03/30/2017"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
helpviewer_keywords:
|
||||
- "data grids [Windows Forms], customizing cells"
|
||||
- "DataGridView control [Windows Forms], customizing cells"
|
||||
- "cells [Windows Forms], customizing in DataGridView control"
|
||||
ms.assetid: 478b20c9-625c-4116-9c5c-5a16e6f4ec67
|
||||
---
|
||||
# How to: Customize the Appearance of Cells in the Windows Forms DataGridView Control
|
||||
You can customize the appearance of any cell by handling the <xref:System.Windows.Forms.DataGridView> control's <xref:System.Windows.Forms.DataGridView.CellPainting> event. You can extract the <xref:System.Windows.Forms.DataGridView> control's <xref:System.Drawing.Graphics> from the <xref:System.Windows.Forms.DataGridViewCellPaintingEventArgs.Graphics%2A> property of the <xref:System.Windows.Forms.DataGridViewCellPaintingEventArgs>. With this <xref:System.Drawing.Graphics>, you can affect the appearance of the entire <xref:System.Windows.Forms.DataGridView> control, but you will usually want to affect only the appearance of the cell that is currently being painted. The <xref:System.Windows.Forms.DataGridViewCellPaintingEventArgs.ClipBounds%2A> property of the <xref:System.Windows.Forms.DataGridViewCellPaintingEventArgs> enables you to restrict your painting operations to the cell that is currently being painted.
|
||||
|
||||
In the following code example, you will paint all the cells in a `ContactName` column using the <xref:System.Windows.Forms.DataGridView> control's color scheme. Each cell's text content is painted in <xref:System.Drawing.Color.Crimson%2A>, and an inset rectangle is drawn in the same color as the <xref:System.Windows.Forms.DataGridView> control's <xref:System.Windows.Forms.DataGridView.GridColor%2A> property.
|
||||
|
||||
## Example
|
||||
[!code-csharp[System.Windows.Forms.DataGridViewCellPainting#10](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewCellPainting/CS/form1.cs#10)]
|
||||
[!code-vb[System.Windows.Forms.DataGridViewCellPainting#10](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewCellPainting/VB/form1.vb#10)]
|
||||
|
||||
## Compiling the Code
|
||||
This example requires:
|
||||
|
||||
- A <xref:System.Windows.Forms.DataGridView> control named `dataGridView1` with a `ContactName` column such as the one in the Customers table in the Northwind sample database.
|
||||
|
||||
- References to the System, System.Windows.Forms, and System.Drawing assemblies.
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridView>
|
||||
- <xref:System.Windows.Forms.DataGridView.CellPainting>
|
||||
- [Customizing the Windows Forms DataGridView Control](customizing-the-windows-forms-datagridview-control.md)
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
---
|
||||
title: Customize the Appearance of Rows in DataGridView Control
|
||||
ms.date: "03/30/2017"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
helpviewer_keywords:
|
||||
- "data grids [Windows Forms], customizing rows"
|
||||
- "rows [Windows Forms], customizing in DataGridView control"
|
||||
- "DataGridView control [Windows Forms], customizing rows"
|
||||
ms.assetid: d40b53d2-7e7c-48c5-8570-6e79d15c3bbb
|
||||
---
|
||||
# How to: Customize the Appearance of Rows in the Windows Forms DataGridView Control
|
||||
You can control the appearance of <xref:System.Windows.Forms.DataGridView> rows by handling one or both of the <xref:System.Windows.Forms.DataGridView.RowPrePaint?displayProperty=nameWithType> and <xref:System.Windows.Forms.DataGridView.RowPostPaint?displayProperty=nameWithType> events. These events are designed so that you can paint only what you want to while letting the <xref:System.Windows.Forms.DataGridView> control paint the rest. For example, if you want to paint a custom background, you can handle the <xref:System.Windows.Forms.DataGridView.RowPrePaint?displayProperty=nameWithType> event and let the individual cells paint their own foreground content. Alternately, you can let the cells paint themselves and add custom foreground content in a handler for the <xref:System.Windows.Forms.DataGridView.RowPostPaint?displayProperty=nameWithType> event. You can also disable cell painting and paint everything yourself in a <xref:System.Windows.Forms.DataGridView.RowPrePaint?displayProperty=nameWithType> event handler.
|
||||
|
||||
The following code example implements handlers for both events in order to provide a gradient selection background and some custom foreground content that spans multiple columns.
|
||||
|
||||
## Example
|
||||
[!code-csharp[System.Windows.Forms.DataGridViewRowPainting#00](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewRowPainting/CS/datagridviewrowpainting.cs#00)]
|
||||
[!code-vb[System.Windows.Forms.DataGridViewRowPainting#00](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewRowPainting/VB/datagridviewrowpainting.vb#00)]
|
||||
|
||||
## Compiling the Code
|
||||
This example requires:
|
||||
|
||||
- References to the System, System.Drawing, and System.Windows.Forms assemblies.
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridView>
|
||||
- <xref:System.Windows.Forms.DataGridView.RowPrePaint?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridView.RowPostPaint?displayProperty=nameWithType>
|
||||
- [Customizing the Windows Forms DataGridView Control](customizing-the-windows-forms-datagridview-control.md)
|
||||
- [DataGridView Control Architecture](datagridview-control-architecture-windows-forms.md)
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
---
|
||||
title: Customize DataGridView Control
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "data grids [Windows Forms], customization"
|
||||
- "DataGridView control [Windows Forms], customization"
|
||||
ms.assetid: 01ea5d4c-a736-4596-b0e9-a67a1b86e15f
|
||||
---
|
||||
# Customizing the Windows Forms DataGridView Control
|
||||
The `DataGridView` control provides several properties that you can use to adjust the appearance and basic behavior (look and feel) of its cells, rows, and columns. If you have special needs that go beyond the capabilities of the <xref:System.Windows.Forms.DataGridViewCellStyle> class, however, you can also implement owner drawing for the control or extend its capabilities by creating custom cells, columns, and rows.
|
||||
|
||||
To paint cells and rows yourself, you can handle various `DataGridView` painting events. To modify existing functionality or provide new functionality, you can create your own types derived from the existing `DataGridViewCell`, `DataGridViewColumn`, and `DataGridViewRow` types. You can also provide new editing capabilities by creating derived types that display a control of your choosing when a cell is in edit mode.
|
||||
|
||||
## In This Section
|
||||
[How to: Customize the Appearance of Cells in the Windows Forms DataGridView Control](customize-the-appearance-of-cells-in-the-datagrid.md)
|
||||
Describes how to handle the <xref:System.Windows.Forms.DataGridView.CellPainting> event in order to paint cells manually.
|
||||
|
||||
[How to: Customize the Appearance of Rows in the Windows Forms DataGridView Control](customize-the-appearance-of-rows-in-the-datagrid.md)
|
||||
Describes how to handle the <xref:System.Windows.Forms.DataGridView.RowPrePaint> and <xref:System.Windows.Forms.DataGridView.RowPostPaint> events in order to paint rows with a custom, gradient background and content that spans multiple columns.
|
||||
|
||||
[How to: Customize Cells and Columns in the Windows Forms DataGridView Control by Extending Their Behavior and Appearance](customize-cells-and-columns-in-the-datagrid-by-extending-behavior.md)
|
||||
Describes how to create custom types derived from `DataGridViewCell` and `DataGridViewColumn` in order to highlight cells when the mouse pointer rests on them.
|
||||
|
||||
[How to: Disable Buttons in a Button Column in the Windows Forms DataGridView Control](disable-buttons-in-a-button-column-in-the-datagrid.md)
|
||||
Describes how to create custom types derived from <xref:System.Windows.Forms.DataGridViewButtonCell> and <xref:System.Windows.Forms.DataGridViewButtonColumn> in order to display disabled buttons in a button column.
|
||||
|
||||
[How to: Host Controls in Windows Forms DataGridView Cells](how-to-host-controls-in-windows-forms-datagridview-cells.md)
|
||||
Describes how to implement the `IDataGridViewEditingControl` interface and create custom types derived from `DataGridViewCell` and `DataGridViewColumn` in order to display a <xref:System.Windows.Forms.DateTimePicker> control when a cell is in edit mode.
|
||||
|
||||
## Reference
|
||||
<xref:System.Windows.Forms.DataGridView>
|
||||
Provides reference documentation for the <xref:System.Windows.Forms.DataGridView> control.
|
||||
|
||||
<xref:System.Windows.Forms.DataGridViewCell>
|
||||
Provides reference documentation for the <xref:System.Windows.Forms.DataGridViewCell> class.
|
||||
|
||||
<xref:System.Windows.Forms.DataGridViewRow>
|
||||
Provides reference documentation for the <xref:System.Windows.Forms.DataGridViewRow> class.
|
||||
|
||||
<xref:System.Windows.Forms.DataGridViewColumn>
|
||||
Provides reference documentation for the <xref:System.Windows.Forms.DataGridViewColumn> class.
|
||||
|
||||
<xref:System.Windows.Forms.IDataGridViewEditingControl>
|
||||
Provides reference documentation for the <xref:System.Windows.Forms.IDataGridViewEditingControl> interface.
|
||||
|
||||
## Related Sections
|
||||
[Basic Formatting and Styling in the Windows Forms DataGridView Control](basic-formatting-and-styling-in-the-windows-forms-datagridview-control.md)
|
||||
Provides topics that describe how to modify the basic appearance of the control and the display formatting of cell data.
|
||||
|
||||
## See also
|
||||
|
||||
- [DataGridView Control](datagridview-control-windows-forms.md)
|
||||
- [Column Types in the Windows Forms DataGridView Control](column-types-in-the-windows-forms-datagridview-control.md)
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
---
|
||||
title: Data Display Modes in DataGridView Control
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "data [Windows Forms], display modes"
|
||||
- "data grids [Windows Forms], display modes"
|
||||
- "DataGridView control [Windows Forms], display modes"
|
||||
ms.assetid: 9755a030-3f3f-4705-a661-ba5a48a81875
|
||||
---
|
||||
# Data Display Modes in the Windows Forms DataGridView Control
|
||||
The <xref:System.Windows.Forms.DataGridView> control can display data in three distinct modes: bound, unbound, and virtual. Choose the most suitable mode based on your requirements.
|
||||
|
||||
## Unbound
|
||||
Unbound mode is suitable for displaying relatively small amounts of data that you manage programmatically. You do not attach the <xref:System.Windows.Forms.DataGridView> control directly to a data source as in bound mode. Instead, you must populate the control yourself, typically by using the <xref:System.Windows.Forms.DataGridViewRowCollection.Add%2A?displayProperty=nameWithType> method.
|
||||
|
||||
Unbound mode can be particularly useful for static, read-only data, or when you want to provide your own code that interacts with an external data store. When you want your users to interact with an external data source, however, you will typically use bound mode.
|
||||
|
||||
For an example that uses a read-only unbound <xref:System.Windows.Forms.DataGridView>, see [How to: Create an Unbound Windows Forms DataGridView Control](how-to-create-an-unbound-windows-forms-datagridview-control.md).
|
||||
|
||||
## Bound
|
||||
Bound mode is suitable for managing data using automatic interaction with the data store. You can attach the <xref:System.Windows.Forms.DataGridView> control directly to its data source by setting the <xref:System.Windows.Forms.DataGridView.DataSource%2A> property. When the control is data bound, data rows are pushed and pulled without the need of explicit management on your part. When the <xref:System.Windows.Forms.DataGridView.AutoGenerateColumns%2A> property is `true`, each column in your data source will cause a corresponding column to be created in the control. If you prefer to create your own columns, you can set this property to `false` and use the <xref:System.Windows.Forms.DataGridViewColumn.DataPropertyName%2A> property to bind each column when you configure it. This is useful when you want to use a column type other than the types that are generated by default. For more information, see [Column Types in the Windows Forms DataGridView Control](column-types-in-the-windows-forms-datagridview-control.md).
|
||||
|
||||
For an example that uses a bound <xref:System.Windows.Forms.DataGridView> control, see [Walkthrough: Validating Data in the Windows Forms DataGridView Control](walkthrough-validating-data-in-the-windows-forms-datagridview-control.md).
|
||||
|
||||
You can also add unbound columns to a <xref:System.Windows.Forms.DataGridView> control in bound mode. This is useful when you want to display a column of buttons or links that enable users to perform actions on specific rows. It is also useful to display columns with values calculated from bound columns. You can populate the cell values for calculated columns in a handler for the <xref:System.Windows.Forms.DataGridView.CellFormatting> event. If you are using a <xref:System.Data.DataSet> or <xref:System.Data.DataTable> as the data source, however, you might want to use the <xref:System.Data.DataColumn.Expression%2A?displayProperty=nameWithType> property to create a calculated column instead. In this case, the <xref:System.Windows.Forms.DataGridView> control will treat calculated column just like any other column in the data source.
|
||||
|
||||
Sorting by unbound columns in bound mode is not supported. If you create an unbound column in bound mode that contains user-editable values, you must implement virtual mode to maintain these values when the control is sorted by a bound column.
|
||||
|
||||
## Virtual
|
||||
With virtual mode, you can implement your own data management operations. This is necessary to maintain the values of unbound columns in bound mode when the control is sorted by bound columns. The primary use of virtual mode, however, is to optimize performance when interacting with large amounts of data.
|
||||
|
||||
You attach the <xref:System.Windows.Forms.DataGridView> control to a cache that you manage, and your code controls when data rows are pushed and pulled. To keep the memory footprint small, the cache should be similar in size to the number of rows currently displayed. When the user scrolls new rows into view, your code requests new data from the cache and optionally flushes old data from memory.
|
||||
|
||||
When you are implementing virtual mode, you will need to track when a new row is needed in the data model and when to rollback the addition of the new row. The exact implementation of this functionality will depend on the implementation of the data model and the transaction semantics of the data model; whether commit scope is at the cell or row level.
|
||||
|
||||
For more information about virtual mode, see [Virtual Mode in the Windows Forms DataGridView Control](virtual-mode-in-the-windows-forms-datagridview-control.md). For an example that shows how to use virtual mode events, see [Walkthrough: Implementing Virtual Mode in the Windows Forms DataGridView Control](implementing-virtual-mode-wf-datagridview-control.md).
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridView>
|
||||
- <xref:System.Windows.Forms.DataGridView.DataSource%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridView.VirtualMode%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.BindingSource>
|
||||
- <xref:System.Windows.Forms.DataGridViewColumn.DataPropertyName%2A?displayProperty=nameWithType>
|
||||
- [Displaying Data in the Windows Forms DataGridView Control](displaying-data-in-the-windows-forms-datagridview-control.md)
|
||||
- [Column Types in the Windows Forms DataGridView Control](column-types-in-the-windows-forms-datagridview-control.md)
|
||||
- [Walkthrough: Creating an Unbound Windows Forms DataGridView Control](walkthrough-creating-an-unbound-windows-forms-datagridview-control.md)
|
||||
- [How to: Bind Data to the Windows Forms DataGridView Control](how-to-bind-data-to-the-windows-forms-datagridview-control.md)
|
||||
- [Virtual Mode in the Windows Forms DataGridView Control](virtual-mode-in-the-windows-forms-datagridview-control.md)
|
||||
- [Walkthrough: Implementing Virtual Mode in the Windows Forms DataGridView Control](implementing-virtual-mode-wf-datagridview-control.md)
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
---
|
||||
title: Data Entry in DataGridView Control
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "DataGridView control [Windows Forms], data entry"
|
||||
- "data entry [Windows Forms], dataGridView control"
|
||||
- "data grids [Windows Forms], data entry"
|
||||
ms.assetid: 4a6d4676-d4e7-4b0e-9c22-50ce65ffe0d6
|
||||
---
|
||||
# Data Entry in the Windows Forms DataGridView Control
|
||||
The `DataGridView` control provides several features that let you change how users add or modify data in the control. For example, you can make data entry more efficient by providing default values for new rows and by alerting users when errors occur.
|
||||
|
||||
## In This Section
|
||||
[How to: Specify the Edit Mode for the Windows Forms DataGridView Control](how-to-specify-the-edit-mode-for-the-windows-forms-datagridview-control.md)
|
||||
Describes how to change the way users start editing cells.
|
||||
|
||||
[How to: Specify Default Values for New Rows in the Windows Forms DataGridView Control](specify-default-values-for-new-rows-in-the-datagrid.md)
|
||||
Describes how to prepopulate the row for new records to save data-entry time.
|
||||
|
||||
[Using the Row for New Records in the Windows Forms DataGridView Control](using-the-row-for-new-records-in-the-windows-forms-datagridview-control.md)
|
||||
Describes the row for new records in detail, including information on hiding it, on customizing its appearance, and on how it relates to the <xref:System.Windows.Forms.DataGridView.Rows%2A> collection.
|
||||
|
||||
[Walkthrough: Validating Data in the Windows Forms DataGridView Control](walkthrough-validating-data-in-the-windows-forms-datagridview-control.md)
|
||||
Describes how to validate user input to prevent data-entry formatting errors.
|
||||
|
||||
[Walkthrough: Handling Errors that Occur During Data Entry in the Windows Forms DataGridView Control](handling-errors-that-occur-during-data-entry-in-the-datagrid.md)
|
||||
Describes how to handle data-entry errors that originate from the data source when the user attempts to commit a new value.
|
||||
|
||||
## Reference
|
||||
<xref:System.Windows.Forms.DataGridView>
|
||||
Provides reference documentation for the <xref:System.Windows.Forms.DataGridView> control.
|
||||
|
||||
<xref:System.Windows.Forms.DataGridView.EditMode%2A?displayProperty=nameWithType>
|
||||
Provides reference documentation for the <xref:System.Windows.Forms.DataGridView.EditMode%2A> property.
|
||||
|
||||
<xref:System.Windows.Forms.DataGridView.DefaultValuesNeeded?displayProperty=nameWithType>
|
||||
Provides reference documentation for the <xref:System.Windows.Forms.DataGridView.DefaultValuesNeeded> event.
|
||||
|
||||
<xref:System.Windows.Forms.DataGridView.DataError?displayProperty=nameWithType>
|
||||
Provides reference documentation for the <xref:System.Windows.Forms.DataGridView.DataError> event.
|
||||
|
||||
<xref:System.Windows.Forms.DataGridView.CellValidating?displayProperty=nameWithType>
|
||||
Provides reference documentation for the <xref:System.Windows.Forms.DataGridView.CellValidating> event.
|
||||
|
||||
## Related Sections
|
||||
[Displaying Data in the Windows Forms DataGridView Control](displaying-data-in-the-windows-forms-datagridview-control.md)
|
||||
Provides topics that describe how to populate the control with data either manually or from an external data source.
|
||||
|
||||
## See also
|
||||
|
||||
- [DataGridView Control](datagridview-control-windows-forms.md)
|
||||
- [Column Types in the Windows Forms DataGridView Control](column-types-in-the-windows-forms-datagridview-control.md)
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
---
|
||||
title: Data Formatting in DataGridView Control
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "DataGridView control [Windows Forms], formatting data"
|
||||
- "data [Windows Forms], formatting in grids"
|
||||
- "data grids [Windows Forms], formatting data"
|
||||
ms.assetid: 07bf558d-3748-42ba-8ba0-37fdef924081
|
||||
---
|
||||
# Data Formatting in the Windows Forms DataGridView Control
|
||||
The <xref:System.Windows.Forms.DataGridView> control provides automatic conversion between cell values and the data types that the parent columns display. Text box columns, for example, display string representations of date, time, number, and enumeration values, and convert user-entered string values to the types required by the data store.
|
||||
|
||||
## Formatting with the DataGridViewCellStyle class
|
||||
The <xref:System.Windows.Forms.DataGridView> control provides basic data formatting of cell values through the <xref:System.Windows.Forms.DataGridViewCellStyle> class. You can use the <xref:System.Windows.Forms.DataGridViewCellStyle.Format%2A> property to format date, time, number, and enumeration values for the current default culture using the format specifiers described in [Formatting Types](https://docs.microsoft.com/dotnet/standard/base-types/formatting-types). You can also format these values for specific cultures using the <xref:System.Windows.Forms.DataGridViewCellStyle.FormatProvider%2A> property. The specified format is used both to display data and to parse data that the user enters in the specified format.
|
||||
|
||||
The <xref:System.Windows.Forms.DataGridViewCellStyle> class provides additional formatting properties for wordwrap, text alignment, and the custom display of null database values. For more information, see [How to: Format Data in the Windows Forms DataGridView Control](how-to-format-data-in-the-windows-forms-datagridview-control.md).
|
||||
|
||||
## Formatting with the CellFormatting Event
|
||||
If the basic formatting does not meet your needs, you can provide custom data formatting in a handler for the <xref:System.Windows.Forms.DataGridView.CellFormatting?displayProperty=nameWithType> event. The <xref:System.Windows.Forms.DataGridViewCellFormattingEventArgs> passed to the handler has a <xref:System.Windows.Forms.ConvertEventArgs.Value%2A> property that initially contains the cell value. Normally, this value is automatically converted to the display type. To convert the value yourself, set the <xref:System.Windows.Forms.ConvertEventArgs.Value%2A> property to a value of the display type.
|
||||
|
||||
> [!NOTE]
|
||||
> If a format string is in effect for the cell, it overrides your change of the <xref:System.Windows.Forms.ConvertEventArgs.Value%2A> property value unless you set the <xref:System.Windows.Forms.DataGridViewCellFormattingEventArgs.FormattingApplied%2A> property to `true`.
|
||||
|
||||
The <xref:System.Windows.Forms.DataGridView.CellFormatting> event is also useful when you want to set <xref:System.Windows.Forms.DataGridViewCellStyle> properties for individual cells based on their values. For more information, see [How to: Customize Data Formatting in the Windows Forms DataGridView Control](how-to-customize-data-formatting-in-the-windows-forms-datagridview-control.md).
|
||||
|
||||
If the default parsing of user-specified values does not meet your needs, you can handle the <xref:System.Windows.Forms.DataGridView.CellParsing> event of the <xref:System.Windows.Forms.DataGridView> control to provide custom parsing.
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridView>
|
||||
- <xref:System.Windows.Forms.DataGridViewCellStyle>
|
||||
- [Displaying Data in the Windows Forms DataGridView Control](displaying-data-in-the-windows-forms-datagridview-control.md)
|
||||
- [Cell Styles in the Windows Forms DataGridView Control](cell-styles-in-the-windows-forms-datagridview-control.md)
|
||||
- [How to: Format Data in the Windows Forms DataGridView Control](how-to-format-data-in-the-windows-forms-datagridview-control.md)
|
||||
- [How to: Customize Data Formatting in the Windows Forms DataGridView Control](how-to-customize-data-formatting-in-the-windows-forms-datagridview-control.md)
|
||||
+149
@@ -0,0 +1,149 @@
|
||||
---
|
||||
title: "DataGrid Control Overview"
|
||||
ms.date: "03/30/2017"
|
||||
f1_keywords:
|
||||
- "DataGrid"
|
||||
helpviewer_keywords:
|
||||
- "datasets [Windows Forms], binding to DataGrid control"
|
||||
- "data binding [Windows Forms], DataGrid control"
|
||||
- "columns [Windows Forms], DataGrid control"
|
||||
- "data sources [Windows Forms], binding to DataGrid control"
|
||||
- "tables [Windows Forms], binding to DataGrid control"
|
||||
- "DataGrid control [Windows Forms], data binding"
|
||||
- "DataGrid control [Windows Forms], about DataGrid control"
|
||||
- "parent tables in DataGrid control"
|
||||
- "tables [Windows Forms], displaying in DataGrid control"
|
||||
- "data grids [Windows Forms], about data grids"
|
||||
- "multiple tables in DataGrid control"
|
||||
- "data [Windows Forms], resorting"
|
||||
- "data [Windows Forms], navigating"
|
||||
- "parent table navigation in DataGrid"
|
||||
- "child tables [Windows Forms], dataGrid control"
|
||||
ms.assetid: 85604bce-bc03-49d9-9030-dda8896c44b1
|
||||
---
|
||||
# DataGrid Control Overview (Windows Forms)
|
||||
|
||||
> [!NOTE]
|
||||
> The <xref:System.Windows.Forms.DataGridView> control replaces and adds functionality to the <xref:System.Windows.Forms.DataGrid> control; however, the <xref:System.Windows.Forms.DataGrid> control is retained for both backward compatibility and future use, if you choose. For more information, see [Differences Between the Windows Forms DataGridView and DataGrid Controls](differences-between-the-windows-forms-datagridview-and-datagrid-controls.md).
|
||||
|
||||
The Windows Forms <xref:System.Windows.Forms.DataGrid> control displays data in a series of rows and columns. The simplest case is when the grid is bound to a data source with a single table that contains no relationships. In that case, the data appears in simple rows and columns, as in a spreadsheet. For more information about binding data to other controls, see [Data Binding and Windows Forms](../data-binding-and-windows-forms.md).
|
||||
|
||||
If the <xref:System.Windows.Forms.DataGrid> is bound to data with multiple related tables, and if navigation is enabled on the grid, the grid will display expanders in each row. With an expander, the user can move from a parent table to a child table. Clicking a node displays the child table, and clicking a back button displays the original parent table. In this manner, the grid displays the hierarchical relationships between tables.
|
||||
|
||||
The following screenshot shows a DataGrid bound to data with multiple tables:
|
||||
|
||||

|
||||
|
||||
The <xref:System.Windows.Forms.DataGrid> can provide a user interface for a dataset, navigation between related tables, and rich formatting and editing capabilities.
|
||||
|
||||
The display and manipulation of data are separate functions: The control handles the user interface, whereas data updates are handled by the Windows Forms data-binding architecture and by .NET Framework data providers. Therefore, multiple controls bound to the same data source will stay in sync.
|
||||
|
||||
> [!NOTE]
|
||||
> If you are familiar with the DataGrid control in Visual Basic 6.0, you will find some significant differences in the Windows Forms <xref:System.Windows.Forms.DataGrid> control.
|
||||
|
||||
When the grid is bound to a <xref:System.Data.DataSet>, the columns and rows are automatically created, formatted, and filled. For more information, see [Data Binding and Windows Forms](../data-binding-and-windows-forms.md). Following the generation of the <xref:System.Windows.Forms.DataGrid> control, you can add, delete, rearrange, and format columns and rows depending on your needs.
|
||||
|
||||
## Binding Data to the Control
|
||||
|
||||
For the <xref:System.Windows.Forms.DataGrid> control to work, it should be bound to a data source using the <xref:System.Windows.Forms.DataGrid.DataSource%2A> and <xref:System.Windows.Forms.DataGrid.DataMember%2A> properties at design time or the <xref:System.Windows.Forms.DataGrid.SetDataBinding%2A> method at run time. This binding points the <xref:System.Windows.Forms.DataGrid> to an instantiated data-source object, such as a <xref:System.Data.DataSet> or <xref:System.Data.DataTable>). The <xref:System.Windows.Forms.DataGrid> control shows the results of actions that are performed on the data. Most data-specific actions are not performed through the <xref:System.Windows.Forms.DataGrid> , but instead through the data source.
|
||||
|
||||
If the data in the bound dataset is updated through any mechanism, the <xref:System.Windows.Forms.DataGrid> control reflects the changes. If the data grid and its table styles and column styles have the `ReadOnly` property set to `false`, the data in the dataset can be updated through the <xref:System.Windows.Forms.DataGrid> control.
|
||||
|
||||
Only one table can be shown in the <xref:System.Windows.Forms.DataGrid> at a time. If a parent-child relationship is defined between tables, the user can move between the related tables to select the table to be displayed in the <xref:System.Windows.Forms.DataGrid> control. For information about binding a <xref:System.Windows.Forms.DataGrid> control to an ADO.NET data source at either design time or run time, see [How to: Bind the Windows Forms DataGrid Control to a Data Source](how-to-bind-the-windows-forms-datagrid-control-to-a-data-source.md).
|
||||
|
||||
Valid data sources for the <xref:System.Windows.Forms.DataGrid> include:
|
||||
|
||||
- <xref:System.Data.DataTable> class
|
||||
|
||||
- <xref:System.Data.DataView> class
|
||||
|
||||
- <xref:System.Data.DataSet> class
|
||||
|
||||
- <xref:System.Data.DataViewManager> class
|
||||
|
||||
If your source is a dataset, the dataset might be an object in the form or an object passed to the form by an XML Web service. You can bind to either typed or untyped datasets.
|
||||
|
||||
You can also bind a <xref:System.Windows.Forms.DataGrid> control to additional structures if the objects in the structure, such as the elements in an array, expose public properties. The grid will display all the public properties of the elements in the structure. For example, if you bind the <xref:System.Windows.Forms.DataGrid> control to an array of customer objects, the grid will display all the public properties of those customer objects. In some instances, this means that although you can bind to the structure, the resulting bound structure might not have practical application. For example, you can bind to an array of integers, but because the `Integer` data type does not support a public property, the grid cannot display any data.
|
||||
|
||||
You can bind to the following structures if their elements expose public properties:
|
||||
|
||||
- Any component that implements the <xref:System.Collections.IList> interface. This includes single-dimension arrays.
|
||||
|
||||
- Any component that implements the <xref:System.ComponentModel.IListSource> interface.
|
||||
|
||||
- Any component that implements the <xref:System.ComponentModel.IBindingList> interface.
|
||||
|
||||
For more information about possible data sources, see [Data Sources Supported by Windows Forms](../data-sources-supported-by-windows-forms.md).
|
||||
|
||||
## Grid Display
|
||||
|
||||
A common use of the <xref:System.Windows.Forms.DataGrid> control is to display a single table of data from a dataset. However, the control can also be used to display multiple tables, including related tables. The display of the grid is adjusted automatically according to the data source. The following table shows what is displayed for various configurations.
|
||||
|
||||
|Contents of data set|What is displayed|
|
||||
|--------------------------|-----------------------|
|
||||
|Single table.|Table is displayed in a grid.|
|
||||
|Multiple tables.|The grid can display a tree view that users can navigate to locate the table they want to display.|
|
||||
|Multiple related tables.|The grid can display a tree view to select tables with, or you can specify that the grid display the parent table. Records in the parent table let users navigate to related child rows.|
|
||||
|
||||
> [!NOTE]
|
||||
> Tables in a dataset are related using a <xref:System.Data.DataRelation>. Also see [Create relationships between datasets](/visualstudio/data-tools/relationships-in-datasets).
|
||||
|
||||
When the <xref:System.Windows.Forms.DataGrid> control is displaying a table and the <xref:System.Windows.Forms.DataGrid.AllowSorting%2A> property is set to `true`, data can be resorted by clicking the column headers. The user can also add rows and edit cells.
|
||||
|
||||
The relationships between a set of tables are displayed to users by using a parent/child structure of navigation. Parent tables are the highest level of data, and child tables are those data tables that are derived from the individual listings in the parent tables. Expanders are displayed in each parent row that contains a child table. Clicking an expander generates a list of Web-like links to the child tables. When the user selects a link, the child table is displayed. Clicking the show/hide parent rows icon () will hide the information about the parent table or cause it to reappear if the user has previously hidden it. The user can click a back button to move back to the previously viewed table.
|
||||
|
||||
## Columns and Rows
|
||||
|
||||
The <xref:System.Windows.Forms.DataGrid> consists of a collection of <xref:System.Windows.Forms.DataGridTableStyle> objects that are contained in the <xref:System.Windows.Forms.DataGrid> control's <xref:System.Windows.Forms.DataGrid.TableStyles%2A> property. A table style may contain a collection of <xref:System.Windows.Forms.DataGridColumnStyle> objects that are contained in the <xref:System.Windows.Forms.DataGridTableStyle.GridColumnStyles%2A> property of the <xref:System.Windows.Forms.DataGridTableStyle>.. You can edit the <xref:System.Windows.Forms.DataGrid.TableStyles%2A> and <xref:System.Windows.Forms.DataGridTableStyle.GridColumnStyles%2A> properties by using collection editors accessed through the **Properties** window.
|
||||
|
||||
Any <xref:System.Windows.Forms.DataGridTableStyle> associated with the <xref:System.Windows.Forms.DataGrid> control can be accessed through the <xref:System.Windows.Forms.GridTableStylesCollection>. The <xref:System.Windows.Forms.GridTableStylesCollection> can be edited in the designer with the <xref:System.Windows.Forms.DataGridTableStyle> collection editor, or programmatically through the <xref:System.Windows.Forms.DataGrid> control's <xref:System.Windows.Forms.DataGrid.TableStyles%2A> property.
|
||||
|
||||
The following illustration shows the objects included in the DataGrid control:
|
||||
|
||||

|
||||
|
||||
Table styles and column styles are synchronized with <xref:System.Data.DataTable> objects and <xref:System.Data.DataColumn> objects by setting their `MappingName` properties to the appropriate <xref:System.Data.DataTable.TableName%2A> and <xref:System.Data.DataColumn.ColumnName%2A> properties. When a <xref:System.Windows.Forms.DataGridTableStyle> that has no column styles is added to a <xref:System.Windows.Forms.DataGrid> control bound to a valid data source, and the <xref:System.Windows.Forms.DataGridTableStyle.MappingName%2A> property of that table style is set to a valid <xref:System.Data.DataTable.TableName%2A> property, a collection of <xref:System.Windows.Forms.DataGridColumnStyle> objects is created for that table style. For each <xref:System.Data.DataColumn> found in the <xref:System.Data.DataTable.Columns%2A> collection of the <xref:System.Data.DataTable>, a corresponding <xref:System.Windows.Forms.DataGridColumnStyle> is added to the <xref:System.Windows.Forms.GridColumnStylesCollection>. <xref:System.Windows.Forms.GridColumnStylesCollection> is accessed through the <xref:System.Windows.Forms.DataGridTableStyle.GridColumnStyles%2A> property of the <xref:System.Windows.Forms.DataGridTableStyle>. Columns can be added or deleted from the grid using the <xref:System.Windows.Forms.GridColumnStylesCollection.Add%2A> or <xref:System.Windows.Forms.GridColumnStylesCollection.Remove%2A> method on the <xref:System.Windows.Forms.GridColumnStylesCollection>. For more information, see [How to: Add Tables and Columns to the Windows Forms DataGrid Control](how-to-add-tables-and-columns-to-the-windows-forms-datagrid-control.md) and [How to: Delete or Hide Columns in the Windows Forms DataGrid Control](how-to-delete-or-hide-columns-in-the-windows-forms-datagrid-control.md).
|
||||
|
||||
A collection of column types extends the <xref:System.Windows.Forms.DataGridColumnStyle> class with rich formatting and editing capabilities. All column types inherit from the <xref:System.Windows.Forms.DataGridColumnStyle> base class. The class that is created depends on the <xref:System.Data.DataColumn.DataType%2A> property of the <xref:System.Data.DataColumn> from which the <xref:System.Web.UI.WebControls.DataGridColumn> is based. For example, a <xref:System.Data.DataColumn> that has its <xref:System.Data.DataColumn.DataType%2A> property set to <xref:System.Boolean> will be associated with the <xref:System.Windows.Forms.DataGridBoolColumn>. The following table describes each of these column types.
|
||||
|
||||
|Column Type|Description|
|
||||
|-----------------|-----------------|
|
||||
|<xref:System.Windows.Forms.DataGridTextBoxColumn>|Accepts and displays data as formatted or unformatted strings. Editing capabilities are the same as they are for editing data in a simple <xref:System.Windows.Forms.TextBox>. Inherits from <xref:System.Windows.Forms.DataGridColumnStyle>.|
|
||||
|<xref:System.Windows.Forms.DataGridBoolColumn>|Accepts and displays `true`, `false`, and null values. Inherits from <xref:System.Windows.Forms.DataGridColumnStyle>.|
|
||||
|
||||
Double-clicking the right edge of a column resizes the column to display its full caption and widest entry.
|
||||
|
||||
## Table Styles and Column Styles
|
||||
|
||||
As soon as you have established the default format of the <xref:System.Windows.Forms.DataGrid> control, you can customize the colors that will be used when certain tables are displayed within the data grid.
|
||||
|
||||
This is achieved by creating instances of the <xref:System.Windows.Forms.DataGridTableStyle> class. Table styles specify the formatting of specific tables, distinct from the default formatting of the <xref:System.Windows.Forms.DataGrid> control itself. Each table may have only one table style defined for it at a time.
|
||||
|
||||
Sometimes, you will want to have a specific column look different from the rest of the columns of a particular data table. You can create a customized set of column styles by using the <xref:System.Windows.Forms.DataGridTableStyle.GridColumnStyles%2A> property.
|
||||
|
||||
Column styles are related to columns in a dataset just like table styles are related to data tables. Just as each table may only have one table style defined for it at a time, so too can each column only have one column style defined for it, in a particular table style. This relationship is defined in the column's <xref:System.Windows.Forms.DataGridColumnStyle.MappingName%2A> property.
|
||||
|
||||
If you have created a table style without column styles added to it, Visual Studio will add default column styles when the form and grid are created at run time. However, if you have created a table style and added any column styles to it, Visual Studio will not create any column styles. Also, you will need to define column styles and assign them with the mapping name to have the columns that you want appear in the grid.
|
||||
|
||||
Because you specify which columns are included in the data grid by assigning them a column style and no column style has been assigned to the columns, you can include columns of data in the dataset that are not displayed in the grid. However, because the data column is included in the dataset, you can programmatically edit the data that is not displayed.
|
||||
|
||||
> [!NOTE]
|
||||
> In general, create column styles and add them to the column styles collection before adding table styles to the table styles collection. When you add an empty table style to the collection, column styles are automatically generated for you. Consequently, an exception will be thrown if you try to add new column styles with duplicate <xref:System.Windows.Forms.DataGridColumnStyle.MappingName%2A> values to the column styles collection.
|
||||
>
|
||||
> Sometimes, you will want to just tweak one column among many columns; for example, the dataset contains 50 columns and you only want 49 of them. In this case, it is easier to import all 50 columns and programmatically remove one, rather than programmatically adding each of the 49 individual columns you want.
|
||||
|
||||
## Formatting
|
||||
|
||||
Formatting that can be applied to the <xref:System.Windows.Forms.DataGrid> control includes border styles, gridline styles, fonts, caption properties, data alignment, and alternating background colors between rows. For more information, see [How to: Format the Windows Forms DataGrid Control](how-to-format-the-windows-forms-datagrid-control.md).
|
||||
|
||||
## Events
|
||||
|
||||
Besides the common control events such as <xref:System.Windows.Forms.Control.MouseDown>, <xref:System.Windows.Forms.Control.Enter>, and <xref:System.Windows.Forms.DataGrid.Scroll>, the <xref:System.Windows.Forms.DataGrid> control supports events associated with editing and navigation within the grid. The <xref:System.Windows.Forms.DataGrid.CurrentCell%2A> property determines which cell is selected. The <xref:System.Windows.Forms.DataGrid.CurrentCellChanged> event is raised when the user navigates to a new cell. When the user navigates to a new table through parent/child relations, the <xref:System.Windows.Forms.DataGrid.Navigate> event is raised. The <xref:System.Windows.Forms.DataGrid.BackButtonClick> event is raised when the user clicks the back button when the user is viewing a child table, and the <xref:System.Windows.Forms.DataGrid.ShowParentDetailsButtonClick> event is raised when the show/hide parent rows icon is clicked.
|
||||
|
||||
## See also
|
||||
|
||||
- [DataGrid Control](datagrid-control-windows-forms.md)
|
||||
- [How to: Bind the Windows Forms DataGrid Control to a Data Source](how-to-bind-the-windows-forms-datagrid-control-to-a-data-source.md)
|
||||
- [How to: Add Tables and Columns to the Windows Forms DataGrid Control](how-to-add-tables-and-columns-to-the-windows-forms-datagrid-control.md)
|
||||
- [How to: Delete or Hide Columns in the Windows Forms DataGrid Control](how-to-delete-or-hide-columns-in-the-windows-forms-datagrid-control.md)
|
||||
- [How to: Format the Windows Forms DataGrid Control](how-to-format-the-windows-forms-datagrid-control.md)
|
||||
@@ -0,0 +1,77 @@
|
||||
---
|
||||
title: "DataGrid Control"
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "datasets [Windows Forms], user interface"
|
||||
- "DataGrid control [Windows Forms]"
|
||||
- "datasets [Windows Forms], displaying in DataGrid control"
|
||||
- "displaying data [Windows Forms], on forms"
|
||||
- "data [Windows Forms], displaying on Windows Forms"
|
||||
ms.assetid: 1d9d5683-43d2-42dd-b6c3-e43f4cf0de99
|
||||
---
|
||||
# DataGrid Control (Windows Forms)
|
||||
> [!NOTE]
|
||||
> The <xref:System.Windows.Forms.DataGridView> control replaces and adds functionality to the `DataGrid` control; however, the `DataGrid` control is retained for both backward compatibility and future use, if you choose. For more information, see [Differences Between the Windows Forms DataGridView and DataGrid Controls](differences-between-the-windows-forms-datagridview-and-datagrid-controls.md).
|
||||
|
||||
The Windows Forms `DataGrid` control provides a user interface to ADO.NET datasets, displaying tabular data and enabling updates to the data source.
|
||||
|
||||
When the `DataGrid` control is set to a valid data source, the control is automatically populated, creating columns and rows based on the shape of the data. The `DataGrid` control can be used to display either a single table or the hierarchical relationships between a set of tables.
|
||||
|
||||
## In This Section
|
||||
[DataGrid Control Overview](datagrid-control-overview-windows-forms.md)
|
||||
Describes the basic features of the `DataGrid` control.
|
||||
|
||||
[How to: Add Tables and Columns to the Windows Forms DataGrid Control Using the Designer](add-tables-and-columns-to-wf-datagrid-control-using-the-designer.md)
|
||||
Describes how to add tables and columns to the `DataGrid` control using the designer.
|
||||
|
||||
[How to: Add Tables and Columns to the Windows Forms DataGrid Control](how-to-add-tables-and-columns-to-the-windows-forms-datagrid-control.md)
|
||||
Describes how to add tables and columns to the `DataGrid` control programmatically.
|
||||
|
||||
[How to: Bind the Windows Forms DataGrid Control to a Data Source Using the Designer](bind-wf-datagrid-control-to-a-data-source-using-the-designer.md)
|
||||
Describes how to bind an ADO.NET dataset to the `DataGrid` control using the designer.
|
||||
|
||||
[How to: Bind the Windows Forms DataGrid Control to a Data Source](how-to-bind-the-windows-forms-datagrid-control-to-a-data-source.md)
|
||||
Describes how to bind an ADO.NET dataset to the `DataGrid` control.
|
||||
|
||||
[How to: Change Displayed Data at Run Time in the Windows Forms DataGrid Control](change-displayed-data-at-run-time-wf-datagrid-control.md)
|
||||
Describes how to change data programmatically in the `DataGrid` control.
|
||||
|
||||
[How to: Create Master-Details Lists with the Windows Forms DataGrid Control Using the Designer](create-master-details-lists-with-wf-datagrid-control-using-the-designer.md)
|
||||
Describes how to display two tables, tied together with a parent/child relationship, in two separate `DataGrid` controls using the designer.
|
||||
|
||||
How to: Create Master-Details Lists with the Windows Forms DataGrid Control
|
||||
Describes how to display two tables, tied together with a parent/child relationship, in two separate `DataGrid` controls.
|
||||
|
||||
[How to: Delete or Hide Columns in the Windows Forms DataGrid Control](how-to-delete-or-hide-columns-in-the-windows-forms-datagrid-control.md)
|
||||
Describes how to remove columns in the `DataGrid` control.
|
||||
|
||||
[How to: Format the Windows Forms DataGrid Control Using the Designer](how-to-format-the-windows-forms-datagrid-control-using-the-designer.md)
|
||||
Describes how to change the appearance-related properties of the `DataGrid` control using the designer.
|
||||
|
||||
[How to: Format the Windows Forms DataGrid Control](how-to-format-the-windows-forms-datagrid-control.md)
|
||||
Describes how to change the appearance-related properties of the `DataGrid` control.
|
||||
|
||||
[Keyboard Shortcuts for the Windows Forms DataGrid Control](keyboard-shortcuts-for-the-windows-forms-datagrid-control.md)
|
||||
Lists shortcuts for navigating through the `DataGrid` control.
|
||||
|
||||
[How to: Respond to Clicks in the Windows Forms DataGrid Control](how-to-respond-to-clicks-in-the-windows-forms-datagrid-control.md)
|
||||
Describes how to determine which cell a user has clicked in the `DataGrid` control.
|
||||
|
||||
[How to: Validate Input with the Windows Forms DataGrid Control](how-to-validate-input-with-the-windows-forms-datagrid-control.md)
|
||||
Describes how to validate input in the dataset bound to the `DataGrid` control.
|
||||
|
||||
## Reference
|
||||
<xref:System.Windows.Forms.DataGrid>
|
||||
Provides an overview of the <xref:System.Windows.Forms.DataGrid> class.
|
||||
|
||||
<xref:System.Windows.Forms.DataGrid.DataSource%2A>
|
||||
Provides details about using this property to bind the <xref:System.Windows.Forms.DataGrid> control to data.
|
||||
|
||||
## Related Sections
|
||||
[Windows Forms Data Binding](../windows-forms-data-binding.md)
|
||||
Provides links to topics on data binding in Windows Forms.
|
||||
|
||||
## See also
|
||||
|
||||
- [DataGridView Control](datagridview-control-windows-forms.md)
|
||||
- [Differences Between the Windows Forms DataGridView and DataGrid Controls](differences-between-the-windows-forms-datagridview-and-datagrid-controls.md)
|
||||
+137
@@ -0,0 +1,137 @@
|
||||
---
|
||||
title: "DataGridView Control Architecture"
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "DataGridView control [Windows Forms], architecture"
|
||||
ms.assetid: 1c6cabf0-02ee-4bbc-9574-b54bb7f5b19e
|
||||
---
|
||||
# DataGridView Control Architecture (Windows Forms)
|
||||
The <xref:System.Windows.Forms.DataGridView> control and its related classes are designed to be a flexible, extensible system for displaying and editing tabular data. These classes are all contained in the <xref:System.Windows.Forms?displayProperty=nameWithType> namespace, and they are all named with the "DataGridView" prefix.
|
||||
|
||||
## Architecture Elements
|
||||
The primary <xref:System.Windows.Forms.DataGridView> companion classes derive from <xref:System.Windows.Forms.DataGridViewElement>. The following object model illustrates the <xref:System.Windows.Forms.DataGridViewElement> inheritance hierarchy.
|
||||
|
||||

|
||||
|
||||
The <xref:System.Windows.Forms.DataGridViewElement> class provides a reference to the parent <xref:System.Windows.Forms.DataGridView> control and has a <xref:System.Windows.Forms.DataGridViewElement.State%2A> property, which holds a value that represents a combination of values from the <xref:System.Windows.Forms.DataGridViewElementStates> enumeration.
|
||||
|
||||
The following sections describe the <xref:System.Windows.Forms.DataGridView> companion classes in more detail.
|
||||
|
||||
### DataGridViewElementStates
|
||||
The <xref:System.Windows.Forms.DataGridViewElementStates> enumeration contains the following values:
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewElementStates.None>
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewElementStates.Frozen>
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewElementStates.ReadOnly>
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewElementStates.Resizable>
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewElementStates.ResizableSet>
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewElementStates.Selected>
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewElementStates.Visible>
|
||||
|
||||
The values of this enumeration can be combined with the bitwise logical operators, so the <xref:System.Windows.Forms.DataGridViewElement.State%2A> property can express more than one state at once. For example, a <xref:System.Windows.Forms.DataGridViewElement> can be simultaneously <xref:System.Windows.Forms.DataGridViewElementStates.Frozen>, <xref:System.Windows.Forms.DataGridViewElementStates.Selected>, and <xref:System.Windows.Forms.DataGridViewElementStates.Visible>.
|
||||
|
||||
### Cells and Bands
|
||||
The <xref:System.Windows.Forms.DataGridView> control comprises two fundamental kinds of objects: cells and bands. All cells derive from the <xref:System.Windows.Forms.DataGridViewCell> base class. The two kinds of bands, <xref:System.Windows.Forms.DataGridViewColumn> and <xref:System.Windows.Forms.DataGridViewRow>, both derive from the <xref:System.Windows.Forms.DataGridViewBand> base class.
|
||||
|
||||
The <xref:System.Windows.Forms.DataGridView> control interoperates with several classes, but the most commonly encountered are <xref:System.Windows.Forms.DataGridViewCell>, <xref:System.Windows.Forms.DataGridViewColumn>, and <xref:System.Windows.Forms.DataGridViewRow>.
|
||||
|
||||
### DataGridViewCell
|
||||
The cell is the fundamental unit of interaction for the <xref:System.Windows.Forms.DataGridView>. Display is centered on cells, and data entry is often performed through cells. You can access cells by using the <xref:System.Windows.Forms.DataGridViewRow.Cells%2A> collection of the <xref:System.Windows.Forms.DataGridViewRow> class, and you can access the selected cells by using the <xref:System.Windows.Forms.DataGridView.SelectedCells%2A> collection of the <xref:System.Windows.Forms.DataGridView> control. The following object model illustrates this usage and shows the <xref:System.Windows.Forms.DataGridViewCell> inheritance hierarchy.
|
||||
|
||||

|
||||
|
||||
The <xref:System.Windows.Forms.DataGridViewCell> type is an abstract base class, from which all cell types derive. <xref:System.Windows.Forms.DataGridViewCell> and its derived types are not Windows Forms controls, but some host Windows Forms controls. Any editing functionality supported by a cell is typically handled by a hosted control.
|
||||
|
||||
<xref:System.Windows.Forms.DataGridViewCell> objects do not control their own appearance and painting features in the same way as Windows Forms controls. Instead, the <xref:System.Windows.Forms.DataGridView> is responsible for the appearance of its <xref:System.Windows.Forms.DataGridViewCell> objects. You can significantly affect the appearance and behavior of cells by interacting with the <xref:System.Windows.Forms.DataGridView> control's properties and events. When you have special requirements for customizations that are beyond the capabilities of the <xref:System.Windows.Forms.DataGridView> control, you can implement your own class that derives from <xref:System.Windows.Forms.DataGridViewCell> or one of its child classes.
|
||||
|
||||
The following list shows the classes derived from <xref:System.Windows.Forms.DataGridViewCell>:
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewTextBoxCell>
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewButtonCell>
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewLinkCell>
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewCheckBoxCell>
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewComboBoxCell>
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewImageCell>
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewHeaderCell>
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewRowHeaderCell>
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewColumnHeaderCell>
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewTopLeftHeaderCell>
|
||||
|
||||
- Your custom cell types
|
||||
|
||||
### DataGridViewColumn
|
||||
The schema of the <xref:System.Windows.Forms.DataGridView> control's attached data store is expressed in the <xref:System.Windows.Forms.DataGridView> control's columns. You can access the <xref:System.Windows.Forms.DataGridView> control's columns by using the <xref:System.Windows.Forms.DataGridView.Columns%2A> collection. You can access the selected columns by using the <xref:System.Windows.Forms.DataGridView.SelectedColumns%2A> collection. The following object model illustrates this usage and shows the <xref:System.Windows.Forms.DataGridViewColumn> inheritance hierarchy.
|
||||
|
||||

|
||||
|
||||
Some of the key cell types have corresponding column types. These are derived from the <xref:System.Windows.Forms.DataGridViewColumn> base class.
|
||||
|
||||
The following list shows the classes derived from <xref:System.Windows.Forms.DataGridViewColumn>:
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewButtonColumn>
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewCheckBoxColumn>
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewComboBoxColumn>
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewImageColumn>
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewTextBoxColumn>
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewLinkColumn>
|
||||
|
||||
- Your custom column types
|
||||
|
||||
### DataGridView Editing Controls
|
||||
Cells that support advanced editing functionality typically use a hosted control that is derived from a Windows Forms control. These controls also implement the <xref:System.Windows.Forms.IDataGridViewEditingControl> interface. The following object model illustrates the usage of these controls.
|
||||
|
||||

|
||||
|
||||
The following editing controls are provided with the <xref:System.Windows.Forms.DataGridView> control:
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewComboBoxEditingControl>
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewTextBoxEditingControl>
|
||||
|
||||
For information about creating your own editing controls, see [How to: Host Controls in Windows Forms DataGridView Cells](how-to-host-controls-in-windows-forms-datagridview-cells.md).
|
||||
|
||||
The following table illustrates the relationship among cell types, column types, and editing controls.
|
||||
|
||||
|Cell type|Hosted control|Column type|
|
||||
|---------------|--------------------|-----------------|
|
||||
|<xref:System.Windows.Forms.DataGridViewButtonCell>|n/a|<xref:System.Windows.Forms.DataGridViewButtonColumn>|
|
||||
|<xref:System.Windows.Forms.DataGridViewCheckBoxCell>|n/a|<xref:System.Windows.Forms.DataGridViewCheckBoxColumn>|
|
||||
|<xref:System.Windows.Forms.DataGridViewComboBoxCell>|<xref:System.Windows.Forms.DataGridViewComboBoxEditingControl>|<xref:System.Windows.Forms.DataGridViewComboBoxColumn>|
|
||||
|<xref:System.Windows.Forms.DataGridViewImageCell>|n/a|<xref:System.Windows.Forms.DataGridViewImageColumn>|
|
||||
|<xref:System.Windows.Forms.DataGridViewLinkCell>|n/a|<xref:System.Windows.Forms.DataGridViewLinkColumn>|
|
||||
|<xref:System.Windows.Forms.DataGridViewTextBoxCell>|<xref:System.Windows.Forms.DataGridViewTextBoxEditingControl>|<xref:System.Windows.Forms.DataGridViewTextBoxColumn>|
|
||||
|
||||
### DataGridViewRow
|
||||
The <xref:System.Windows.Forms.DataGridViewRow> class displays a record's data fields from the data store to which the <xref:System.Windows.Forms.DataGridView> control is attached. You can access the <xref:System.Windows.Forms.DataGridView> control's rows by using the <xref:System.Windows.Forms.DataGridView.Rows%2A> collection. You can access the selected rows by using the <xref:System.Windows.Forms.DataGridView.SelectedRows%2A> collection. The following object model illustrates this usage and shows the <xref:System.Windows.Forms.DataGridViewRow> inheritance hierarchy.
|
||||
|
||||

|
||||
|
||||
You can derive your own types from the <xref:System.Windows.Forms.DataGridViewRow> class, although this will typically not be necessary. The <xref:System.Windows.Forms.DataGridView> control has several row-related events and properties for customizing the behavior of its <xref:System.Windows.Forms.DataGridViewRow> objects.
|
||||
|
||||
If you enable the <xref:System.Windows.Forms.DataGridView> control's <xref:System.Windows.Forms.DataGridView.AllowUserToAddRows%2A> property, a special row for adding new rows appears as the last row. This row is part of the <xref:System.Windows.Forms.DataGridView.Rows%2A> collection, but it has special functionality that may require your attention. For more information, see [Using the Row for New Records in the Windows Forms DataGridView Control](using-the-row-for-new-records-in-the-windows-forms-datagridview-control.md).
|
||||
|
||||
## See also
|
||||
|
||||
- [DataGridView Control Overview](datagridview-control-overview-windows-forms.md)
|
||||
- [Customizing the Windows Forms DataGridView Control](customizing-the-windows-forms-datagridview-control.md)
|
||||
- [Using the Row for New Records in the Windows Forms DataGridView Control](using-the-row-for-new-records-in-the-windows-forms-datagridview-control.md)
|
||||
+152
@@ -0,0 +1,152 @@
|
||||
---
|
||||
title: "DataGridView Control Code Directory"
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "data grids [Windows Forms], examples"
|
||||
- "examples [Windows Forms], DataGridView control"
|
||||
- "DataGridView control [Windows Forms], examples"
|
||||
- "DataGridView control [Windows Forms], code directory"
|
||||
ms.assetid: ea8bd66a-5ed6-491b-b9da-63873e275a0a
|
||||
---
|
||||
# DataGridView Control Code Directory (Windows Forms)
|
||||
This topic provides links to <xref:System.Windows.Forms.DataGridView>-related code examples available in the documentation.
|
||||
|
||||
> [!NOTE]
|
||||
> A link always jumps to the top of the topic in which the code example is found.
|
||||
|
||||
Additional code examples are available in the class library reference documentation. For a list of the principal classes and interfaces associated with the <xref:System.Windows.Forms.DataGridView> control, see the table in [DataGridView Control Technology Summary](datagridview-control-technology-summary-windows-forms.md).
|
||||
|
||||
## CodeList
|
||||
|
||||
## Unbound Data Examples
|
||||
|
||||
- [How to: Add an Unbound Column to a Data-Bound Windows Forms DataGridView Control](unbound-column-to-a-data-bound-datagridview.md)
|
||||
|
||||
- [How to: Create an Unbound Windows Forms DataGridView Control](how-to-create-an-unbound-windows-forms-datagridview-control.md)
|
||||
|
||||
- [Walkthrough: Creating an Unbound Windows Forms DataGridView Control](walkthrough-creating-an-unbound-windows-forms-datagridview-control.md)
|
||||
|
||||
## Data Binding Examples
|
||||
|
||||
- [How to: Bind Data to the Windows Forms DataGridView Control](how-to-bind-data-to-the-windows-forms-datagridview-control.md)
|
||||
|
||||
- [How to: Autogenerate Columns in a Data-Bound Windows Forms DataGridView Control](autogenerate-columns-in-a-data-bound-wf-datagridview-control.md)
|
||||
|
||||
- [How to: Remove Autogenerated Columns from a Windows Forms DataGridView Control](remove-autogenerated-columns-from-a-wf-datagridview-control.md)
|
||||
|
||||
- [How to: Bind Objects to Windows Forms DataGridView Controls](how-to-bind-objects-to-windows-forms-datagridview-controls.md)
|
||||
|
||||
- [How to: Access Objects Bound to Windows Forms DataGridView Rows](how-to-access-objects-bound-to-windows-forms-datagridview-rows.md)
|
||||
|
||||
- [How to: Create a Master/Detail Form Using Two Windows Forms DataGridView Controls](create-a-master-detail-form-using-two-datagridviews.md)
|
||||
|
||||
- [Walkthrough: Creating a Master/Detail Form Using Two Windows Forms DataGridView Controls](creating-a-master-detail-form-using-two-datagridviews.md)
|
||||
|
||||
## Data Formatting Examples
|
||||
|
||||
- [How to: Format Data in the Windows Forms DataGridView Control](how-to-format-data-in-the-windows-forms-datagridview-control.md)
|
||||
|
||||
- [How to: Customize Data Formatting in the Windows Forms DataGridView Control](how-to-customize-data-formatting-in-the-windows-forms-datagridview-control.md)
|
||||
|
||||
## Data Validation Examples
|
||||
|
||||
- [How to: Validate Data in the Windows Forms DataGridView Control](how-to-validate-data-in-the-windows-forms-datagridview-control.md)
|
||||
|
||||
- [Walkthrough: Validating Data in the Windows Forms DataGridView Control](walkthrough-validating-data-in-the-windows-forms-datagridview-control.md)
|
||||
|
||||
- [How to: Handle Errors That Occur During Data Entry in the Windows Forms DataGridView Control](handle-errors-that-occur-during-data-entry-in-the-datagrid.md)
|
||||
|
||||
- [Walkthrough: Handling Errors that Occur During Data Entry in the Windows Forms DataGridView Control](handling-errors-that-occur-during-data-entry-in-the-datagrid.md)
|
||||
|
||||
## Appearance Customization Examples
|
||||
|
||||
- [How to: Change the Border and Gridline Styles in the Windows Forms DataGridView Control](change-the-border-and-gridline-styles-in-the-datagrid.md)
|
||||
|
||||
- [How to: Set Font and Color Styles in the Windows Forms DataGridView Control](how-to-set-font-and-color-styles-in-the-windows-forms-datagridview-control.md)
|
||||
|
||||
- [How to: Set Default Cell Styles for the Windows Forms DataGridView Control](how-to-set-default-cell-styles-for-the-windows-forms-datagridview-control.md)
|
||||
|
||||
- [How to: Use the Row Template to Customize Rows in the Windows Forms DataGridView Control](use-the-row-template-to-customize-rows-in-the-datagrid.md)
|
||||
|
||||
- [How to: Set Alternating Row Styles for the Windows Forms DataGridView Control](how-to-set-alternating-row-styles-for-the-windows-forms-datagridview-control.md)
|
||||
|
||||
## Behavior Customization Examples
|
||||
|
||||
- [How to: Specify the Edit Mode for the Windows Forms DataGridView Control](how-to-specify-the-edit-mode-for-the-windows-forms-datagridview-control.md)
|
||||
|
||||
- [How to: Specify Default Values for New Rows in the Windows Forms DataGridView Control](specify-default-values-for-new-rows-in-the-datagrid.md)
|
||||
|
||||
- [How to: Prevent Row Addition and Deletion in the Windows Forms DataGridView Control](prevent-row-addition-and-deletion-datagridview.md)
|
||||
|
||||
- [How to: Perform a Custom Action Based on Changes in a Cell of a Windows Forms DataGridView Control](perform-a-custom-action-based-on-changes-in-a-cell-of-a-datagrid.md)
|
||||
|
||||
- [How to: Enable Users to Copy Multiple Cells to the Clipboard from the Windows Forms DataGridView Control](enable-users-to-copy-multiple-cells-to-the-clipboard-datagridview.md)
|
||||
|
||||
- [How to: Add ToolTips to Individual Cells in a Windows Forms DataGridView Control](add-tooltips-to-individual-cells-in-a-wf-datagridview-control.md)
|
||||
|
||||
- [How to: Display Images in Cells of the Windows Forms DataGridView Control](how-to-display-images-in-cells-of-the-windows-forms-datagridview-control.md)
|
||||
|
||||
- [How to: Customize Sorting in the Windows Forms DataGridView Control](how-to-customize-sorting-in-the-windows-forms-datagridview-control.md)
|
||||
|
||||
## Column Manipulation Examples
|
||||
|
||||
- [How to: Freeze Columns in the Windows Forms DataGridView Control](how-to-freeze-columns-in-the-windows-forms-datagridview-control.md)
|
||||
|
||||
- [How to: Enable Column Reordering in the Windows Forms DataGridView Control](how-to-enable-column-reordering-in-the-windows-forms-datagridview-control.md)
|
||||
|
||||
- [How to: Change the Order of Columns in the Windows Forms DataGridView Control](how-to-change-the-order-of-columns-in-the-windows-forms-datagridview-control.md)
|
||||
|
||||
- [How to: Hide Columns in the Windows Forms DataGridView Control](how-to-hide-columns-in-the-windows-forms-datagridview-control.md)
|
||||
|
||||
- [How to: Hide Column Headers in the Windows Forms DataGridView Control](how-to-hide-column-headers-in-the-windows-forms-datagridview-control.md)
|
||||
|
||||
- [How to: Make Columns Read-Only in the Windows Forms DataGridView Control](how-to-make-columns-read-only-in-the-windows-forms-datagridview-control.md)
|
||||
|
||||
- [How to: Set the Sort Modes for Columns in the Windows Forms DataGridView Control](set-the-sort-modes-for-columns-wf-datagridview-control.md)
|
||||
|
||||
- [How to: Work with Image Columns in the Windows Forms DataGridView Control](how-to-work-with-image-columns-in-the-windows-forms-datagridview-control.md)
|
||||
|
||||
- [How to: Manipulate Columns in the Windows Forms DataGridView Control](how-to-manipulate-columns-in-the-windows-forms-datagridview-control.md)
|
||||
|
||||
## Row and Column Sizing Examples
|
||||
|
||||
- [Column Fill Mode in the Windows Forms DataGridView Control](column-fill-mode-in-the-windows-forms-datagridview-control.md)
|
||||
|
||||
- [How to: Set the Sizing Modes of the Windows Forms DataGridView Control](how-to-set-the-sizing-modes-of-the-windows-forms-datagridview-control.md)
|
||||
|
||||
- [How to: Programmatically Resize Cells to Fit Content in the Windows Forms DataGridView Control](programmatically-resize-cells-to-fit-content-in-the-datagrid.md)
|
||||
|
||||
- [How to: Automatically Resize Cells When Content Changes in the Windows Forms DataGridView Control](automatically-resize-cells-when-content-changes-in-the-datagrid.md)
|
||||
|
||||
## Selection Examples
|
||||
|
||||
- [How to: Set the Selection Mode of the Windows Forms DataGridView Control](how-to-set-the-selection-mode-of-the-windows-forms-datagridview-control.md)
|
||||
|
||||
- [How to: Get the Selected Cells, Rows, and Columns in the Windows Forms DataGridView Control](selected-cells-rows-and-columns-datagridview.md)
|
||||
|
||||
- [How to: Get and Set the Current Cell in the Windows Forms DataGridView Control](get-and-set-the-current-cell-wf-datagridview-control.md)
|
||||
|
||||
## Advanced Customization Examples
|
||||
|
||||
- [How to: Customize the Appearance of Cells in the Windows Forms DataGridView Control](customize-the-appearance-of-cells-in-the-datagrid.md)
|
||||
|
||||
- [How to: Customize the Appearance of Rows in the Windows Forms DataGridView Control](customize-the-appearance-of-rows-in-the-datagrid.md)
|
||||
|
||||
- [How to: Customize Cells and Columns in the Windows Forms DataGridView Control by Extending Their Behavior and Appearance](customize-cells-and-columns-in-the-datagrid-by-extending-behavior.md)
|
||||
|
||||
- [How to: Disable Buttons in a Button Column in the Windows Forms DataGridView Control](disable-buttons-in-a-button-column-in-the-datagrid.md)
|
||||
|
||||
- [How to: Host Controls in Windows Forms DataGridView Cells](how-to-host-controls-in-windows-forms-datagridview-cells.md)
|
||||
|
||||
## Advanced Data Examples
|
||||
|
||||
- [How to: Implement Virtual Mode in the Windows Forms DataGridView Control](how-to-implement-virtual-mode-in-the-windows-forms-datagridview-control.md)
|
||||
|
||||
- [Walkthrough: Implementing Virtual Mode in the Windows Forms DataGridView Control](implementing-virtual-mode-wf-datagridview-control.md)
|
||||
|
||||
- [Implementing Virtual Mode with Just-In-Time Data Loading in the Windows Forms DataGridView Control](implementing-virtual-mode-jit-data-loading-in-the-datagrid.md)
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridView>
|
||||
- [DataGridView Control Overview](datagridview-control-overview-windows-forms.md)
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
---
|
||||
title: "DataGridView Control Overview"
|
||||
description: Learn how to use the Windows Forms DataGridView control to display and edit tabular data from many different kinds of data sources.
|
||||
ms.date: "03/30/2017"
|
||||
f1_keywords:
|
||||
- "DataGridView"
|
||||
helpviewer_keywords:
|
||||
- "DataGridView control [Windows Forms], about DataGridView control"
|
||||
- "grid controls [Windows Forms]"
|
||||
- "tables [Windows Forms], displaying in DataGridView control"
|
||||
- "tables [Windows Forms], binding to DataGridView control"
|
||||
- "columns [Windows Forms], DataGridView control"
|
||||
- "bound controls [Windows Forms], dataGridView control"
|
||||
- "datasets [Windows Forms], binding to DataGridView control"
|
||||
- "data grids [Windows Forms], about data grids"
|
||||
- "data [Windows Forms], resorting"
|
||||
- "data [Windows Forms], navigating"
|
||||
- "grids [Windows Forms]"
|
||||
- "data binding [Windows Forms], DataGridView control"
|
||||
- "data sources [Windows Forms], binding to DataGridView control"
|
||||
- "DataGridView control [Windows Forms], data binding"
|
||||
ms.assetid: 0a45c661-89dc-4390-9cc6-c47eee501488
|
||||
---
|
||||
# DataGridView Control Overview (Windows Forms)
|
||||
> [!NOTE]
|
||||
> The <xref:System.Windows.Forms.DataGridView> control replaces and adds functionality to the <xref:System.Windows.Forms.DataGrid> control; however, the <xref:System.Windows.Forms.DataGrid> control is retained for both backward compatibility and future use, if you choose. For more information, see [Differences Between the Windows Forms DataGridView and DataGrid Controls](differences-between-the-windows-forms-datagridview-and-datagrid-controls.md).
|
||||
|
||||
With the <xref:System.Windows.Forms.DataGridView> control, you can display and edit tabular data from many different kinds of data sources.
|
||||
|
||||
Binding data to the <xref:System.Windows.Forms.DataGridView> control is straightforward and intuitive, and in many cases it is as simple as setting the <xref:System.Windows.Forms.DataGridView.DataSource%2A> property. When you bind to a data source that contains multiple lists or tables, set the <xref:System.Windows.Forms.DataGridView.DataMember%2A> property to a string that specifies the list or table to bind to.
|
||||
|
||||
The <xref:System.Windows.Forms.DataGridView> control supports the standard Windows Forms data binding model, so it will bind to instances of classes described in the following list:
|
||||
|
||||
- Any class that implements the <xref:System.Collections.IList> interface, including one-dimensional arrays.
|
||||
|
||||
- Any class that implements the <xref:System.ComponentModel.IListSource> interface, such as the <xref:System.Data.DataTable> and <xref:System.Data.DataSet> classes.
|
||||
|
||||
- Any class that implements the <xref:System.ComponentModel.IBindingList> interface, such as the <xref:System.ComponentModel.BindingList%601> class.
|
||||
|
||||
- Any class that implements the <xref:System.ComponentModel.IBindingListView> interface, such as the <xref:System.Windows.Forms.BindingSource> class.
|
||||
|
||||
The <xref:System.Windows.Forms.DataGridView> control supports data binding to the public properties of the objects returned by these interfaces or to the properties collection returned by an <xref:System.ComponentModel.ICustomTypeDescriptor> interface, if implemented on the returned objects.
|
||||
|
||||
Typically, you will bind to a <xref:System.Windows.Forms.BindingSource> component and bind the <xref:System.Windows.Forms.BindingSource> component to another data source or populate it with business objects. The <xref:System.Windows.Forms.BindingSource> component is the preferred data source because it can bind to a wide variety of data sources and can resolve many data binding issues automatically. For more information, see [BindingSource Component](bindingsource-component.md).
|
||||
|
||||
The <xref:System.Windows.Forms.DataGridView> control can also be used in *unbound* mode, with no underlying data store. For a code example that uses an unbound <xref:System.Windows.Forms.DataGridView> control, see [Walkthrough: Creating an Unbound Windows Forms DataGridView Control](walkthrough-creating-an-unbound-windows-forms-datagridview-control.md).
|
||||
|
||||
The <xref:System.Windows.Forms.DataGridView> control is highly configurable and extensible, and it provides many properties, methods, and events to customize its appearance and behavior. When you want your Windows Forms application to display tabular data, consider using the <xref:System.Windows.Forms.DataGridView> control before others (for example, <xref:System.Windows.Forms.DataGrid>). If you are displaying a small grid of read-only values, or if you are enabling a user to edit a table with millions of records, the <xref:System.Windows.Forms.DataGridView> control will provide you with a readily programmable, memory-efficient solution.
|
||||
|
||||
## In This Section
|
||||
[DataGridView Control Technology Summary](datagridview-control-technology-summary-windows-forms.md)
|
||||
Summarizes <xref:System.Windows.Forms.DataGridView> control concepts and the use of related classes.
|
||||
|
||||
[DataGridView Control Architecture](datagridview-control-architecture-windows-forms.md)
|
||||
Describes the architecture of the <xref:System.Windows.Forms.DataGridView> control, explaining its type hierarchy and inheritance structure.
|
||||
|
||||
[DataGridView Control Scenarios](datagridview-control-scenarios-windows-forms.md)
|
||||
Describes the most common scenarios in which <xref:System.Windows.Forms.DataGridView> controls are used.
|
||||
|
||||
[DataGridView Control Code Directory](datagridview-control-code-directory-windows-forms.md)
|
||||
Provides links to code examples in the documentation for various <xref:System.Windows.Forms.DataGridView> tasks. These examples are categorized by task type.
|
||||
|
||||
## Related Sections
|
||||
[Column Types in the Windows Forms DataGridView Control](column-types-in-the-windows-forms-datagridview-control.md)
|
||||
Discusses the column types in the Windows Forms <xref:System.Windows.Forms.DataGridView> control used to display information and allow users to modify or add information.
|
||||
|
||||
[Displaying Data in the Windows Forms DataGridView Control](displaying-data-in-the-windows-forms-datagridview-control.md)
|
||||
Provides topics that describe how to populate the control with data either manually, or from an external data source.
|
||||
|
||||
[Customizing the Windows Forms DataGridView Control](customizing-the-windows-forms-datagridview-control.md)
|
||||
Provides topics that describe custom painting <xref:System.Windows.Forms.DataGridView> cells and rows, and creating derived cell, column, and row types.
|
||||
|
||||
[Performance Tuning in the Windows Forms DataGridView Control](performance-tuning-in-the-windows-forms-datagridview-control.md)
|
||||
Provides topics that describe how to use the control efficiently to avoid performance problems when working with large amounts of data.
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridView>
|
||||
- <xref:System.Windows.Forms.BindingSource>
|
||||
- [DataGridView Control](datagridview-control-windows-forms.md)
|
||||
- [Default Functionality in the Windows Forms DataGridView Control](default-functionality-in-the-windows-forms-datagridview-control.md)
|
||||
- [Default Keyboard and Mouse Handling in the Windows Forms DataGridView Control](default-keyboard-and-mouse-handling-in-the-windows-forms-datagridview-control.md)
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
---
|
||||
title: "DataGridView Control Scenarios"
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "data [Windows Forms], displaying in tabular format"
|
||||
- "data grids [Windows Forms], about data grids"
|
||||
- "DataGridView control [Windows Forms], scenarios"
|
||||
ms.assetid: 09a5fd05-3447-47ec-a4ec-6082a2b7f0dd
|
||||
---
|
||||
# DataGridView Control Scenarios (Windows Forms)
|
||||
With the <xref:System.Windows.Forms.DataGridView> control, you can display tabular data from a variety of data sources. For simple uses, you can manually populate a <xref:System.Windows.Forms.DataGridView> and manipulate the data directly through the control. Typically, however, you will store your data in an external data source and bind the control to it through a <xref:System.Windows.Forms.BindingSource> component.
|
||||
|
||||
This topic describes some of the common scenarios that involve the <xref:System.Windows.Forms.DataGridView> control.
|
||||
|
||||
## Scenario 1: Displaying Small Amounts of Data
|
||||
You do not have to store your data in an external data source to display it in the <xref:System.Windows.Forms.DataGridView> control. If you are working with a small amount of data, you can populate the control yourself and manipulate the data through the control. This is called *unbound mode*. For more information, see [How to: Create an Unbound Windows Forms DataGridView Control](how-to-create-an-unbound-windows-forms-datagridview-control.md).
|
||||
|
||||
### Scenario Key Points
|
||||
|
||||
- In unbound mode, you populate the control manually.
|
||||
|
||||
- Unbound mode is particularly suited for small amounts of read-only data.
|
||||
|
||||
- Unbound mode is also suited for spreadsheet-like or sparsely populated tables.
|
||||
|
||||
## Scenario 2: Viewing and Updating Data Stored in an External Data Source
|
||||
You can use the <xref:System.Windows.Forms.DataGridView> control as a user interface (UI) through which users can access data kept in a data source such as a database table or a collection of business objects. For more information, see [How to: Bind Data to the Windows Forms DataGridView Control](how-to-bind-data-to-the-windows-forms-datagridview-control.md).
|
||||
|
||||
### Scenario Key Points
|
||||
|
||||
- Bound mode lets you connect to a data source, automatically generate columns based on the data source properties or database columns, and automatically populate the control.
|
||||
|
||||
- Bound mode is suited for heavy user interaction with data. Data can be formatted for display, and user-specified data can be parsed into the format expected by the data source. Data entry formatting errors and database constraint errors can be detected so that users can be warned and erroneous cells can be corrected.
|
||||
|
||||
- Additional functionality such as column sorting, freezing, and reordering enable users to view data in the way most convenient for their workflow.
|
||||
|
||||
- Clipboard support enables users to copy data from your application into other applications.
|
||||
|
||||
## Scenario 3: Advanced Data
|
||||
If you have special needs that the standard data binding model does not address, you can manage the interaction between the control and your data by implementing *virtual mode*. Implementing virtual mode means implementing one or more event handlers that let the control request information about cells as the information is needed.
|
||||
|
||||
For example, if you work with large amounts of data, you may want to implement virtual mode to ensure optimal efficiency. Virtual mode is also useful for maintaining the values of unbound columns that you display along with columns retrieved from another data source.
|
||||
|
||||
For more information about virtual mode, see [Walkthrough: Implementing Virtual Mode in the Windows Forms DataGridView Control](implementing-virtual-mode-wf-datagridview-control.md).
|
||||
|
||||
### Scenario Key Points
|
||||
|
||||
- Virtual mode is suited for displaying very large amounts of data when you need to fine-tune performance.
|
||||
|
||||
## Scenario 4: Automatically Resizing Rows and Columns
|
||||
When you display data that is regularly updated, you can automatically resize rows and columns to ensure that all content is visible. The <xref:System.Windows.Forms.DataGridView> control provides several options that let you enable or disable manual resizing, resize programmatically at specific times, or resize automatically whenever content changes. For more information, see [Sizing Options in the Windows Forms DataGridView Control](sizing-options-in-the-windows-forms-datagridview-control.md).
|
||||
|
||||
### Scenario Key Points
|
||||
|
||||
- Manual resizing enables users to adjust cell heights and widths.
|
||||
|
||||
- Automatic resizing enables you to maintain cell sizes so that cell content is never clipped.
|
||||
|
||||
- Programmatic resizing enables you to resize cells at specific times to avoid the performance penalty of continuous automatic resizing.
|
||||
|
||||
## Scenario 5: Simple Customization
|
||||
The <xref:System.Windows.Forms.DataGridView> control provides many ways for you to alter its basic appearance and behavior. For more information, see [Cell Styles in the Windows Forms DataGridView Control](cell-styles-in-the-windows-forms-datagridview-control.md).
|
||||
|
||||
### Scenario Key Points
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridViewCellStyle> objects let you provide color, font, formatting, and positioning information at multiple levels and for individual elements of the control.
|
||||
|
||||
- Cell styles can be layered and shared by multiple elements, letting you reuse code.
|
||||
|
||||
## Scenario 6: Advanced Customization
|
||||
The <xref:System.Windows.Forms.DataGridView> control provides many ways for you to customize its appearance and behavior.
|
||||
|
||||
### Scenario Key Points
|
||||
|
||||
- You can provide your own cell painting code. For more information, see [How to: Customize the Appearance of Cells in the Windows Forms DataGridView Control](customize-the-appearance-of-cells-in-the-datagrid.md).
|
||||
|
||||
- You can provide your own row painting. This is useful, for example, to create rows with content that spans multiple columns. For more information, see [How to: Customize the Appearance of Rows in the Windows Forms DataGridView Control](customize-the-appearance-of-rows-in-the-datagrid.md).
|
||||
|
||||
- You can implement your own cell and column classes to customize cell appearance. For more information, see [How to: Customize Cells and Columns in the Windows Forms DataGridView Control by Extending Their Behavior and Appearance](customize-cells-and-columns-in-the-datagrid-by-extending-behavior.md).
|
||||
|
||||
- You can implement your own cell and column classes to host controls other than the ones provided by the built-in column types. For more information, see [How to: Host Controls in Windows Forms DataGridView Cells](how-to-host-controls-in-windows-forms-datagridview-cells.md).
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridView>
|
||||
- [DataGridView Control Overview](datagridview-control-overview-windows-forms.md)
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
---
|
||||
title: "DataGridView Control Technology Summary"
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "DataGridView control [Windows Forms], about DataGridView control"
|
||||
- "data grids [Windows Forms], about data grids"
|
||||
ms.assetid: 094498c3-a126-4a3f-83fe-f69e96c7717b
|
||||
---
|
||||
# DataGridView Control Technology Summary (Windows Forms)
|
||||
This topic summarizes information about the `DataGridView` control and the classes that support its use.
|
||||
|
||||
Displaying data in a tabular format is a task you are likely to perform frequently. The `DataGridView` control is designed to be a complete solution for presenting data in a grid.
|
||||
|
||||
## Keywords
|
||||
DataGridView, BindingSource, table, cell, data binding, virtual mode
|
||||
|
||||
## Namespaces
|
||||
<xref:System.Windows.Forms?displayProperty=nameWithType>
|
||||
|
||||
<xref:System.Data?displayProperty=nameWithType>
|
||||
|
||||
## Related Technologies
|
||||
`BindingSource`
|
||||
|
||||
## Background
|
||||
User interface (UI) designers frequently find it necessary to display tabular data to users. The .NET Framework provides several ways to show data in a table or grid. The `DataGridView` control represents the latest evolution of this technology for Windows Forms applications.
|
||||
|
||||
The `DataGridView` control can display rows of data from a data store. Many types of data stores are supported. The data store can hold simple, untyped data, such as a one-dimensional array, or it can hold typed data, such as a <xref:System.Data.DataSet>. For more information, see [How to: Bind Data to the Windows Forms DataGridView Control](how-to-bind-data-to-the-windows-forms-datagridview-control.md).
|
||||
|
||||
The `DataGridView` control provides a powerful and flexible way to display data in a tabular format. You can use the control to show read-only or editable views of small to very large sets of data.
|
||||
|
||||
You can extend the `DataGridView` control in several ways to build custom behavior into your applications. For example, you can programmatically specify your own sorting algorithms, and you can create your own types of cells. You can easily customize the appearance of the `DataGridView` control by choosing among several properties. Many types of data stores can be used as a data source, or the `DataGridView` control can operate without a data source bound to it.
|
||||
|
||||
## Implementing DataGridView Classes
|
||||
There are several ways for you to take advantage of the `DataGridView` control's extensibility features. You can customize many aspects of the control through events and properties, but some customizations require you to create new classes derived from existing `DataGridView` classes.
|
||||
|
||||
The most typically used base classes are `DataGridViewCell` and `DataGridViewColumn`. You can derive your own cell class from `DataGridViewCell` or any of its child classes. Although you can add any cell type to any column, you will typically also derive a companion column class from `DataGridViewColumn` that hosts cells of your custom cell type by default.
|
||||
|
||||
You can implement the `IDataGridViewEditingCell` interface in your derived cell class to create a cell type that has editing functionality but does not host a control in editing mode. To create a control that you can host in a cell in editing mode, you can implement the `IDataGridViewEditingControl` interface in a class derived from <xref:System.Windows.Forms.Control>.
|
||||
|
||||
For more information, see [How to: Customize Cells and Columns in the Windows Forms DataGridView Control by Extending Their Behavior and Appearance](customize-cells-and-columns-in-the-datagrid-by-extending-behavior.md) and [How to: Host Controls in Windows Forms DataGridView Cells](how-to-host-controls-in-windows-forms-datagridview-cells.md).
|
||||
|
||||
## DataGridView Classes at a Glance
|
||||
<xref:System.Windows.Forms>
|
||||
|
||||
|Technology Area|Classes/interfaces/configuration elements|
|
||||
|---------------------|-------------------------------------------------|
|
||||
|Data Binding|<xref:System.Windows.Forms.BindingSource>|
|
||||
|Data Presentation|<xref:System.Windows.Forms.DataGridView><br /><br /> <xref:System.Windows.Forms.DataGridViewCell> and derived classes<br /><br /> <xref:System.Windows.Forms.DataGridViewRow> and derived classes<br /><br /> <xref:System.Windows.Forms.DataGridViewColumn> and derived classes<br /><br /> <xref:System.Windows.Forms.DataGridViewCellStyle>|
|
||||
|<xref:System.Windows.Forms.DataGridView> Extensibility|<xref:System.Windows.Forms.DataGridViewCell> and derived classes<br /><br /> <xref:System.Windows.Forms.DataGridViewColumn> and derived classes<br /><br /> <xref:System.Windows.Forms.IDataGridViewEditingCell><br /><br /> <xref:System.Windows.Forms.IDataGridViewEditingControl>|
|
||||
|
||||
## What's New
|
||||
The <xref:System.Windows.Forms.DataGridView> control is designed to be a complete solution for displaying tabular data with Windows Forms. You should consider using the <xref:System.Windows.Forms.DataGridView> control before other solutions, such as <xref:System.Windows.Forms.DataGrid>, when you are authoring a new application. For more information, see [Differences Between the Windows Forms DataGridView and DataGrid Controls](differences-between-the-windows-forms-datagridview-and-datagrid-controls.md).
|
||||
|
||||
The <xref:System.Windows.Forms.DataGridView> control can work in close conjunction with the <xref:System.Windows.Forms.BindingSource> component. This component is designed to be the primary data source of a form. It can manage the interaction between a <xref:System.Windows.Forms.DataGridView> control and its data source, regardless of the data source type.
|
||||
|
||||
## See also
|
||||
|
||||
- [DataGridView Control Overview](datagridview-control-overview-windows-forms.md)
|
||||
- [DataGridView Control Architecture](datagridview-control-architecture-windows-forms.md)
|
||||
- [Protecting Connection Information](https://docs.microsoft.com/dotnet/framework/data/adonet/protecting-connection-information)
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
---
|
||||
title: "DataGridView Control"
|
||||
description: Learn how to use the `DataGridView` control to show read-only views of a small amount of data, or scale it to show editable views of very large sets of data.
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "tables [Windows Forms]"
|
||||
- "data grids [Windows Forms"
|
||||
- "data [Windows Forms], displaying in tabular format"
|
||||
- "grid controls [Windows Forms]"
|
||||
- "datasets [Windows Forms], user interface"
|
||||
- "Windows Forms, displaying data"
|
||||
- "data presentation"
|
||||
- "tabular data [Windows Forms], displaying on Windows Forms"
|
||||
- "datasets [Windows Forms], displaying in DataGridView control"
|
||||
- "DataGridView control [Windows Forms]"
|
||||
ms.assetid: dbee73f2-bba6-4874-9389-cd21d44309be
|
||||
---
|
||||
# DataGridView Control (Windows Forms)
|
||||
The `DataGridView` control provides a powerful and flexible way to display data in a tabular format. You can use the `DataGridView` control to show read-only views of a small amount of data, or you can scale it to show editable views of very large sets of data.
|
||||
|
||||
You can extend the `DataGridView` control in a number of ways to build custom behaviors into your applications. For example, you can programmatically specify your own sorting algorithms, and you can create your own types of cells. You can easily customize the appearance of the `DataGridView` control by choosing among several properties. Many types of data stores can be used as a data source, or the `DataGridView` control can operate with no data source bound to it.
|
||||
|
||||
The topics in this section describe the concepts and techniques that you can use to build `DataGridView` features into your applications.
|
||||
|
||||
## In This Section
|
||||
[DataGridView Control Overview](datagridview-control-overview-windows-forms.md)
|
||||
Provides topics that describe the architecture and core concepts of the Windows Forms `DataGridView` control.
|
||||
|
||||
[Default Functionality in the Windows Forms DataGridView Control](default-functionality-in-the-windows-forms-datagridview-control.md)
|
||||
Describes the default appearance and behavior of the Windows Forms `DataGridView` control when it is bound to a data source.
|
||||
|
||||
[Column Types in the Windows Forms DataGridView Control](column-types-in-the-windows-forms-datagridview-control.md)
|
||||
Describes the column types in the Windows Forms `DataGridView` control used to display data and allow users to modify or add data.
|
||||
|
||||
[Basic Column, Row, and Cell Features in the Windows Forms DataGridView Control](basic-column-row-and-cell-features-wf-datagridview-control.md)
|
||||
Provides topics that describe commonly-used cell, row, and column properties.
|
||||
|
||||
[Basic Formatting and Styling in the Windows Forms DataGridView Control](basic-formatting-and-styling-in-the-windows-forms-datagridview-control.md)
|
||||
Provides topics that describe how to modify the basic appearance of the control and the display formatting of cell data.
|
||||
|
||||
[Displaying Data in the Windows Forms DataGridView Control](displaying-data-in-the-windows-forms-datagridview-control.md)
|
||||
Provides topics that describe how to populate the control with data either manually, or from an external data source.
|
||||
|
||||
[Resizing Columns and Rows in the Windows Forms DataGridView Control](resizing-columns-and-rows-in-the-windows-forms-datagridview-control.md)
|
||||
Provides topics that describe how the size of rows and columns can be adjusted automatically to fit cell content or to fit the available width of the control.
|
||||
|
||||
[Sorting Data in the Windows Forms DataGridView Control](sorting-data-in-the-windows-forms-datagridview-control.md)
|
||||
Provides topics that describe the sorting features in the control.
|
||||
|
||||
[Data Entry in the Windows Forms DataGridView Control](data-entry-in-the-windows-forms-datagridview-control.md)
|
||||
Provides topics that describe how to change the way users add and modify data in the control.
|
||||
|
||||
[Selection and Clipboard Use with the Windows Forms DataGridView Control](selection-and-clipboard-use-with-the-windows-forms-datagridview-control.md)
|
||||
Provides topics that describe the cell, row, and column selection features in the control.
|
||||
|
||||
[Programming with Cells, Rows, and Columns in the Windows Forms DataGridView Control](programming-with-cells-rows-and-columns-in-the-datagrid.md)
|
||||
Provides topics that describe how to program with cell, row, and column objects.
|
||||
|
||||
[Customizing the Windows Forms DataGridView Control](customizing-the-windows-forms-datagridview-control.md)
|
||||
Provides topics that describe custom painting `DataGridView` cells and rows, and creating derived cell, column, and row types.
|
||||
|
||||
[Performance Tuning in the Windows Forms DataGridView Control](performance-tuning-in-the-windows-forms-datagridview-control.md)
|
||||
Provides topics that describe how to use the control efficiently to avoid performance problems when working with large amounts of data.
|
||||
|
||||
[Default Keyboard and Mouse Handling in the Windows Forms DataGridView Control](default-keyboard-and-mouse-handling-in-the-windows-forms-datagridview-control.md)
|
||||
Describes how users can interact with the `DataGridView` control through a keyboard and a mouse.
|
||||
|
||||
[Differences Between the Windows Forms DataGridView and DataGrid Controls](differences-between-the-windows-forms-datagridview-and-datagrid-controls.md)
|
||||
Describes how the `DataGridView` control improves upon and replaces the <xref:System.Windows.Forms.DataGrid> control.
|
||||
|
||||
Also see [Using the Designer with the Windows Forms DataGridView Control](using-the-designer-with-the-windows-forms-datagridview-control.md).
|
||||
|
||||
## Reference
|
||||
<xref:System.Windows.Forms.DataGridView>
|
||||
Provides reference documentation for the <xref:System.Windows.Forms.DataGridView> control.
|
||||
|
||||
<xref:System.Windows.Forms.BindingSource>
|
||||
Provides reference documentation for the <xref:System.Windows.Forms.BindingSource> component. The <xref:System.Windows.Forms.DataGridView> control and the <xref:System.Windows.Forms.BindingSource> component are designed to work closely together.
|
||||
|
||||
## See also
|
||||
|
||||
- [Controls to Use on Windows Forms](controls-to-use-on-windows-forms.md)
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
---
|
||||
title: "DateTimePicker Control Overview"
|
||||
ms.date: "03/30/2017"
|
||||
f1_keywords:
|
||||
- "DateTimePicker"
|
||||
helpviewer_keywords:
|
||||
- "DateTimePicker control [Windows Forms], about"
|
||||
- "date and time picker controls"
|
||||
ms.assetid: 501af106-e9fc-4efc-b9b3-c9d8dcaf8c5c
|
||||
---
|
||||
# DateTimePicker Control Overview (Windows Forms)
|
||||
The Windows Forms <xref:System.Windows.Forms.DateTimePicker> control allows the user to select a single item from a list of dates or times. When used to represent a date, it appears in two parts: a drop-down list with a date represented in text, and a grid that appears when you click on the down-arrow next to the list. The grid looks like the <xref:System.Windows.Forms.MonthCalendar> control, which can be used for selecting multiple dates. For more information on the <xref:System.Windows.Forms.MonthCalendar> control, see [MonthCalendar Control Overview](monthcalendar-control-overview-windows-forms.md).
|
||||
|
||||
## Key Properties
|
||||
If you wish the <xref:System.Windows.Forms.DateTimePicker> to appear as a control for picking or editing times instead of dates, set the <xref:System.Windows.Forms.DateTimePicker.ShowUpDown%2A> property to `true` and the <xref:System.Windows.Forms.DateTimePicker.Format%2A> property to <xref:System.Windows.Forms.DateTimePickerFormat.Time>. For more information see [How to: Display Time with the DateTimePicker Control](how-to-display-time-with-the-datetimepicker-control.md).
|
||||
|
||||
When the <xref:System.Windows.Forms.DateTimePicker.ShowCheckBox%2A> property is set to `true`, a check box is displayed next to the selected date in the control. When the check box is checked, the selected date-time value can be updated. When the check box is empty, the value appears unavailable.
|
||||
|
||||
The control's <xref:System.Windows.Forms.DateTimePicker.MaxDate%2A> and <xref:System.Windows.Forms.DateTimePicker.MinDate%2A> properties determine the range of dates and times. The <xref:System.Windows.Forms.DateTimePicker.Value%2A> property contains the current date and time the control is set to. For details, see [How to: Set and Return Dates with the Windows Forms DateTimePicker Control](how-to-set-and-return-dates-with-the-windows-forms-datetimepicker-control.md). The values can be displayed in four formats, which are set by the <xref:System.Windows.Forms.DateTimePicker.Format%2A> property: <xref:System.Windows.Forms.DateTimePickerFormat.Long>, <xref:System.Windows.Forms.DateTimePickerFormat.Short>, <xref:System.Windows.Forms.DateTimePickerFormat.Time>, or <xref:System.Windows.Forms.DateTimePickerFormat.Custom>. If a custom format is selected, you must set the <xref:System.Windows.Forms.DateTimePicker.CustomFormat%2A> property to an appropriate string. For details, see [How to: Display a Date in a Custom Format with the Windows Forms DateTimePicker Control](display-a-date-in-a-custom-format-with-wf-datetimepicker-control.md).
|
||||
|
||||
## See also
|
||||
|
||||
- [How to: Display a Date in a Custom Format with the Windows Forms DateTimePicker Control](display-a-date-in-a-custom-format-with-wf-datetimepicker-control.md)
|
||||
- [How to: Set and Return Dates with the Windows Forms DateTimePicker Control](how-to-set-and-return-dates-with-the-windows-forms-datetimepicker-control.md)
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
---
|
||||
title: "DateTimePicker Control"
|
||||
description: Learn to use the Windows Forms DateTimePicker control to allow the user to select a single item from a list of dates or times.
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "dates [Windows Forms], choosing from a list"
|
||||
- "dates [Windows Forms], Windows Forms controls"
|
||||
- "DateTimePicker control [Windows Forms]"
|
||||
- "date and time picker controls"
|
||||
ms.assetid: 1dac1fbd-8f67-4910-9ccb-2c5462d0d855
|
||||
---
|
||||
# DateTimePicker Control (Windows Forms)
|
||||
The Windows Forms `DateTimePicker` control allows the user to select a single item from a list of dates or times. When used to represent a date, it appears in two parts: a drop-down list with a date represented in text, and a grid that appears when you click on the down-arrow next to the list.
|
||||
|
||||
## In This Section
|
||||
[DateTimePicker Control Overview](datetimepicker-control-overview-windows-forms.md)
|
||||
Introduces the general concepts of the `DateTimePicker` control, which allows users to select a single item from a list of dates or times.
|
||||
|
||||
[How to: Display a Date in a Custom Format with the Windows Forms DateTimePicker Control](display-a-date-in-a-custom-format-with-wf-datetimepicker-control.md)
|
||||
Explains how to use format strings to display dates in a preferred format.
|
||||
|
||||
[How to: Set and Return Dates with the Windows Forms DateTimePicker Control](how-to-set-and-return-dates-with-the-windows-forms-datetimepicker-control.md)
|
||||
Provides steps to set the date in the control and to access the date the user has selected.
|
||||
|
||||
[How to: Display Time with the DateTimePicker Control](how-to-display-time-with-the-datetimepicker-control.md)
|
||||
Shows steps to for a `DateTimePicker` to display times only.
|
||||
|
||||
## Reference
|
||||
<xref:System.Windows.Forms.DateTimePicker>
|
||||
Describes this class and has links to all its members.
|
||||
|
||||
<xref:System.Windows.Forms.MonthCalendar>
|
||||
Describes this class and has links to all its members.
|
||||
|
||||
## Related Sections
|
||||
[Controls to Use on Windows Forms](controls-to-use-on-windows-forms.md)
|
||||
Provides a complete list of Windows Forms controls, with links to information on their use.
|
||||
|
||||
[MonthCalendar Control](monthcalendar-control-windows-forms.md)
|
||||
Presents an intuitive graphical interface for users to view and set date information.
|
||||
@@ -0,0 +1,65 @@
|
||||
---
|
||||
title: Set Default Cell Styles and Data Formats for DataGridView Control Using the Designer
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "DataGridView control [Windows Forms], cell styles"
|
||||
- "cells [Windows Forms], setting styles"
|
||||
- "data formats"
|
||||
- "data [Windows Forms], setting formats"
|
||||
ms.assetid: fc6da49f-8942-41da-b49f-b2afc38cc656
|
||||
---
|
||||
# How to: Set Default Cell Styles and Data Formats for the Windows Forms DataGridView Control Using the Designer
|
||||
|
||||
The <xref:System.Windows.Forms.DataGridView> control lets you specify default cell styles and cell data formats for the entire control, for specific columns, for row and column headers, and for alternating rows to create a ledger effect. Default styles set for the entire control are overridden by default styles set for columns and alternating rows. Additionally, styles that you set in code for individual rows and cells override the default styles.
|
||||
|
||||
For more information about cell styles, see [Cell Styles in the Windows Forms DataGridView Control](cell-styles-in-the-windows-forms-datagridview-control.md). To set styles for alternating rows, see [How to: Set Alternating Row Styles for the Windows Forms DataGridView Control Using the Designer](set-alternating-row-styles-for-the-datagrid-using-the-designer.md).
|
||||
|
||||
You can also set styles using the <xref:System.Windows.Forms.DataGridView.RowTemplate%2A> property to affect all rows that will be added to the control. For more information about the row template, see [How to: Use the Row Template to Customize Rows in the Windows Forms DataGridView Control](use-the-row-template-to-customize-rows-in-the-datagrid.md).
|
||||
|
||||
The following procedures require a **Windows Application** project with a form containing a <xref:System.Windows.Forms.DataGridView> control. For information about setting up such a project, see [How to: Create a Windows Forms application project](/visualstudio/ide/step-1-create-a-windows-forms-application-project) and [How to: Add Controls to Windows Forms](how-to-add-controls-to-windows-forms.md).
|
||||
|
||||
### To set default styles for all cells in the control
|
||||
|
||||
1. Select the <xref:System.Windows.Forms.DataGridView> control in the designer.
|
||||
|
||||
2. In the **Properties** window, click the ellipsis button () next to the <xref:System.Windows.Forms.DataGridView.DefaultCellStyle%2A>, <xref:System.Windows.Forms.DataGridView.ColumnHeadersDefaultCellStyle%2A>, or <xref:System.Windows.Forms.DataGridView.RowHeadersDefaultCellStyle%2A> property. The **CellStyle Builder** dialog box appears.
|
||||
|
||||
3. Define the style by setting the properties, using the **Preview** pane to confirm your choices.
|
||||
|
||||
> [!NOTE]
|
||||
> If visual styles are enabled, the row and column headers (except for the <xref:System.Windows.Forms.DataGridView.TopLeftHeaderCell%2A>) are automatically styled by the current theme, overriding the <xref:System.Windows.Forms.DataGridView.ColumnHeadersDefaultCellStyle%2A> and <xref:System.Windows.Forms.DataGridView.RowHeadersDefaultCellStyle%2A> property values.
|
||||
>
|
||||
> You can set cell styles for multiple selected <xref:System.Windows.Forms.DataGridView> controls using the designer, but only if they have identical values for the cell style property you want to modify. If any cell styles differ for that property, the **Properties** windows of the **CellStyle Builder** dialog box will be blank.
|
||||
|
||||
### To set default styles for cells in individual columns
|
||||
|
||||
1. Right-click the <xref:System.Windows.Forms.DataGridView> control in the designer and choose **Edit Columns**.
|
||||
|
||||
2. Select a column from the **Selected Columns** list.
|
||||
|
||||
3. In the **Column Properties** grid, click the ellipsis button () next to the <xref:System.Windows.Forms.DataGridViewColumn.DefaultCellStyle%2A> property. The **CellStyle Builder** dialog box appears.
|
||||
|
||||
4. Define the style by setting the properties, using the **Preview** pane to confirm your choices.
|
||||
|
||||
### To format data in cells
|
||||
|
||||
1. Use one of the preceding procedures to display a **CellStyle Builder** dialog box related to a default cell style property.
|
||||
|
||||
2. In the **CellStyle Builder** dialog box, click the ellipsis button () next to the <xref:System.Windows.Forms.DataGridViewCellStyle.Format%2A> property. The **Format String** dialog box appears.
|
||||
|
||||
3. Select a format type, then modify the details of the type (such as the number of decimal places to display), using the **Sample** box to confirm your choices.
|
||||
|
||||
4. If you are binding the <xref:System.Windows.Forms.DataGridView> control to a data source that is likely to contain null values, fill in the **Null Value** text box. This value is displayed when the cell value is equal to a null reference (`Nothing` in Visual Basic) or <xref:System.DBNull.Value?displayProperty=nameWithType>.
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridView>
|
||||
- <xref:System.Windows.Forms.DataGridViewCellStyle>
|
||||
- <xref:System.Windows.Forms.DataGridView.DefaultCellStyle%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridView.RowsDefaultCellStyle%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridViewColumn.DefaultCellStyle%2A?displayProperty=nameWithType>
|
||||
- <xref:System.Windows.Forms.DataGridViewCellStyle.Format%2A?displayProperty=nameWithType>
|
||||
- [Cell Styles in the Windows Forms DataGridView Control](cell-styles-in-the-windows-forms-datagridview-control.md)
|
||||
- [How to: Set Alternating Row Styles for the Windows Forms DataGridView Control Using the Designer](set-alternating-row-styles-for-the-datagrid-using-the-designer.md)
|
||||
- [How to: Create a Windows Forms application project](/visualstudio/ide/step-1-create-a-windows-forms-application-project)
|
||||
- [How to: Add Controls to Windows Forms](how-to-add-controls-to-windows-forms.md)
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
---
|
||||
title: Default Functionality in DataGridView Control
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "data grids [Windows Forms], default functionality in DataGridView control"
|
||||
- "DataGridView control [Windows Forms], default functionality"
|
||||
ms.assetid: 4405f697-cad1-4839-9bcd-8ddb09d9f00e
|
||||
---
|
||||
# Default Functionality in the Windows Forms DataGridView Control
|
||||
The Windows Forms <xref:System.Windows.Forms.DataGridView> control provides users with a significant amount of default functionality.
|
||||
|
||||
## Default Functionality
|
||||
By default, a <xref:System.Windows.Forms.DataGridView> control:
|
||||
|
||||
- Automatically displays column headers and row headers that remain visible as the table scrolls vertically.
|
||||
|
||||
- Has a row header that contains a selection indicator for the current row.
|
||||
|
||||
- Has a selection rectangle in the first cell.
|
||||
|
||||
- Has columns that can be automatically resized when the user double-clicks the column dividers.
|
||||
|
||||
- Automatically supports visual styles on Windows XP and the Windows Server 2003 family when the <xref:System.Windows.Forms.Application.EnableVisualStyles%2A> method is called from the application's `Main` method.
|
||||
|
||||
Additionally, the contents of a <xref:System.Windows.Forms.DataGridView> control can be edited by default:
|
||||
|
||||
- If the user double-clicks or presses F2 in a cell, the control automatically puts the cell into edit mode and updates the contents of the cell as the user types.
|
||||
|
||||
- If the user scrolls to the end of the grid, the user will see that a row for adding new records is present. When the user clicks this row, a new row is added to the <xref:System.Windows.Forms.DataGridView> control, with default values. When the user presses ESC, this new row disappears.
|
||||
|
||||
- If the user clicks a row header, the whole row is selected.
|
||||
|
||||
When you bind a <xref:System.Windows.Forms.DataGridView> control to a data source by setting its <xref:System.Windows.Forms.DataGridView.DataSource%2A> property, the control:
|
||||
|
||||
- Automatically uses the names of the data source's columns as the column header text.
|
||||
|
||||
- Is populated with the contents of the data source. <xref:System.Windows.Forms.DataGridView> columns are automatically created for each column in the data source.
|
||||
|
||||
- Creates a row for each visible row in the table.
|
||||
|
||||
- Automatically sorts the rows based on the underlying data when the user clicks a column header.
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridView>
|
||||
- [DataGridView Control](datagridview-control-windows-forms.md)
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
---
|
||||
title: Default keyboard and mouse handling in DataGridView control
|
||||
ms.date: "02/13/2018"
|
||||
helpviewer_keywords:
|
||||
- "data grids [Windows Forms], mouse handling"
|
||||
- "DataGridView control [Windows Forms], navigation keys"
|
||||
- "keyboards [Windows Forms], default handling in DataGridView control"
|
||||
- "DataGridView control [Windows Forms], keyboard handling"
|
||||
- "mouse [Windows Forms], default handling in DataGridView control"
|
||||
- "DataGridView control [Windows Forms], mouse handling"
|
||||
- "navigation keys [Windows Forms], DataGridView control"
|
||||
ms.assetid: 4519b928-bfc8-4e8b-bb9c-b1e76a0ca552
|
||||
---
|
||||
# Default keyboard and mouse handling in the Windows Forms DataGridView control
|
||||
|
||||
The following tables describe how users can interact with the <xref:System.Windows.Forms.DataGridView> control through a keyboard and a mouse.
|
||||
|
||||
> [!NOTE]
|
||||
> To customize keyboard behavior, you can handle standard keyboard events such as <xref:System.Windows.Forms.Control.KeyDown>. In edit mode, however, the hosted editing control receives the keyboard input and the keyboard events do not occur for the <xref:System.Windows.Forms.DataGridView> control. To handle editing control events, attach your handlers to the editing control in an <xref:System.Windows.Forms.DataGridView.EditingControlShowing> event handler. Alternatively, you can customize keyboard behavior in a <xref:System.Windows.Forms.DataGridView> subclass by overriding the <xref:System.Windows.Forms.DataGridView.ProcessDialogKey%2A> and <xref:System.Windows.Forms.DataGridView.ProcessDataGridViewKey%2A> methods.
|
||||
|
||||
## Default keyboard handling
|
||||
|
||||
### Basic navigation and entry keys
|
||||
|
||||
|Key or key combination|Description|
|
||||
|----------------------------|-----------------|
|
||||
|DOWN ARROW|Moves the focus to the cell directly below the current cell. If the focus is in the last row, does nothing.|
|
||||
|LEFT ARROW|Moves the focus to the previous cell in the row. If the focus is in the first cell in the row, does nothing.|
|
||||
|RIGHT ARROW|Moves the focus to the next cell in the row. If the focus is in the last cell in the row, does nothing.|
|
||||
|UP ARROW|Moves the focus to the cell directly above the current cell. If the focus is in the first row, does nothing.|
|
||||
|HOME|Moves the focus to the first cell in the current row.|
|
||||
|END|Moves the focus to the last cell in the current row.|
|
||||
|PAGE DOWN|Scrolls the control downward by the number of rows that are fully displayed. Moves the focus to the last fully displayed row without changing columns.|
|
||||
|PAGE UP|Scrolls the control upward by the number of rows that are fully displayed. Moves focus to the first displayed row without changing columns.|
|
||||
|TAB|If the <xref:System.Windows.Forms.DataGridView.StandardTab%2A> property value is `false`, moves the focus to the next cell in the current row. If the focus is already in the last cell of the row, moves the focus to the first cell in the next row. If the focus is in the last cell in the control, moves the focus to the next control in the tab order of the parent container.<br /><br /> If the <xref:System.Windows.Forms.DataGridView.StandardTab%2A> property value is `true`, moves the focus to the next control in the tab order of the parent container.|
|
||||
|SHIFT+TAB|If the <xref:System.Windows.Forms.DataGridView.StandardTab%2A> property value is `false`, moves the focus to the previous cell in the current row. If the focus is already in the first cell of the row, moves the focus to the last cell in the previous row. If the focus is in the first cell in the control, moves the focus to the previous control in the tab order of the parent container.<br /><br /> If the <xref:System.Windows.Forms.DataGridView.StandardTab%2A> property value is `true`, moves the focus to the previous control in the tab order of the parent container.|
|
||||
|CTRL+TAB|If the <xref:System.Windows.Forms.DataGridView.StandardTab%2A> property value is `false`, moves the focus to the next control in the tab order of the parent container.<br /><br /> If the <xref:System.Windows.Forms.DataGridView.StandardTab%2A> property value is `true`, moves the focus to the next cell in the current row. If the focus is already in the last cell of the row, moves the focus to the first cell in the next row. If the focus is in the last cell in the control, moves the focus to the next control in the tab order of the parent container.|
|
||||
|CTRL+SHIFT+TAB|If the <xref:System.Windows.Forms.DataGridView.StandardTab%2A> property value is `false`, moves the focus to the previous control in the tab order of the parent container.<br /><br /> If the <xref:System.Windows.Forms.DataGridView.StandardTab%2A> property value is `true`, moves the focus to the previous cell in the current row. If the focus is already in the first cell of the row, moves the focus to the last cell in the previous row. If the focus is in the first cell in the control, moves the focus to the previous control in the tab order of the parent container.|
|
||||
|CTRL+ARROW|Moves the focus to the farthest cell in the direction of the arrow.|
|
||||
|CTRL+HOME|Moves the focus to the first cell in the control.|
|
||||
|CTRL+END|Moves the focus to the last cell in the control.|
|
||||
|CTRL+PAGE DOWN/UP|Same as PAGE DOWN or PAGE UP.|
|
||||
|F2|Puts the current cell into cell edit mode if the <xref:System.Windows.Forms.DataGridView.EditMode%2A> property value is <xref:System.Windows.Forms.DataGridViewEditMode.EditOnF2> or <xref:System.Windows.Forms.DataGridViewEditMode.EditOnKeystrokeOrF2>.|
|
||||
|F3|Sorts the current column if the <xref:System.Windows.Forms.DataGridViewColumn.SortMode%2A?displayProperty=nameWithType> property value is <xref:System.Windows.Forms.DataGridViewColumnSortMode.Automatic>. It's the same as clicking the current column header. Available since .NET Framework 4.7.2. To enable this feature, applications must target .NET Framework 4.7.2 or later versions or explicitly opt into accessibility improvements using AppContext switches.|
|
||||
|F4|If the current cell is a <xref:System.Windows.Forms.DataGridViewComboBoxCell>, puts the cell into edit mode and displays the drop-down list.|
|
||||
|ALT+UP/DOWN ARROW|If the current cell is a <xref:System.Windows.Forms.DataGridViewComboBoxCell>, puts the cell into edit mode and displays the drop-down list.|
|
||||
|SPACE|If the current cell is a <xref:System.Windows.Forms.DataGridViewButtonCell>, <xref:System.Windows.Forms.DataGridViewLinkCell>, or <xref:System.Windows.Forms.DataGridViewCheckBoxCell>, raises the <xref:System.Windows.Forms.DataGridView.CellClick> and <xref:System.Windows.Forms.DataGridView.CellContentClick> events. If the current cell is a <xref:System.Windows.Forms.DataGridViewButtonCell>, also presses the button. If the current cell is a <xref:System.Windows.Forms.DataGridViewCheckBoxCell>, also changes the check state.|
|
||||
|ENTER|Commits any changes to the current cell and row and moves the focus to the cell directly below the current cell. If the focus is in the last row, commits any changes without moving the focus.|
|
||||
|ESC|If the control is in edit mode, cancels the edit. If the control is not in edit mode, reverts any changes that have been made to the current row if the control is bound to a data source that supports editing or virtual mode has been implemented with row-level commit scope.|
|
||||
|BACKSPACE|Deletes the character before the insertion point when editing a cell.|
|
||||
|DELETE|Deletes the character after the insertion point when editing a cell.|
|
||||
|CTRL+ENTER|Commits any changes to the current cell without moving the focus. Also commits any changes to the current row if the control is bound to a data source that supports editing or virtual mode has been implemented with row-level commit scope.|
|
||||
|CTRL+0|Enters a <xref:System.DBNull.Value?displayProperty=nameWithType> value into the current cell if the cell can be edited. By default, the display value for a <xref:System.DBNull> cell value is the value of the <xref:System.Windows.Forms.DataGridViewCellStyle.NullValue%2A> property of the <xref:System.Windows.Forms.DataGridViewCellStyle> in effect for the current cell.|
|
||||
|
||||
### Selection keys
|
||||
|
||||
If the <xref:System.Windows.Forms.DataGridView.MultiSelect%2A> property is set to `false` and the <xref:System.Windows.Forms.DataGridView.SelectionMode%2A> property is set to <xref:System.Windows.Forms.DataGridViewSelectionMode.CellSelect>, changing the current cell by using the navigation keys changes the selection to the new cell. The SHIFT, CTRL, and ALT keys do not affect this behavior.
|
||||
|
||||
If the <xref:System.Windows.Forms.DataGridView.SelectionMode%2A> is set to <xref:System.Windows.Forms.DataGridViewSelectionMode.RowHeaderSelect> or <xref:System.Windows.Forms.DataGridViewSelectionMode.ColumnHeaderSelect>, the same behavior occurs but with the following additions.
|
||||
|
||||
|Key or key combination|Description|
|
||||
|----------------------------|-----------------|
|
||||
|SHIFT+SPACEBAR|Selects the full row or column (the same as clicking the row or column header).|
|
||||
|navigation key (arrow key, PAGE UP/DOWN, HOME, END)|If a full row or column is selected, changing the current cell to a new row or column moves the selection to the full new row or column (depending on the selection mode).|
|
||||
|
||||
If <xref:System.Windows.Forms.DataGridView.MultiSelect%2A> is set to `false` and <xref:System.Windows.Forms.DataGridView.SelectionMode%2A> is set to <xref:System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect> or <xref:System.Windows.Forms.DataGridViewSelectionMode.FullColumnSelect>, changing the current cell to a new row or column by using the keyboard moves the selection to the full new row or column. The SHIFT, CTRL, and ALT keys do not affect this behavior.
|
||||
|
||||
If <xref:System.Windows.Forms.DataGridView.MultiSelect%2A> is set to `true`, the navigation behavior does not change, but navigating with the keyboard while pressing SHIFT (including CTRL+SHIFT) will modify a multi-cell selection. Before navigation begins, the control marks the current cell as an anchor cell. When you navigate while pressing SHIFT, the selection includes all cells between the anchor cell and the current cell. Other cells in the control will remain selected if they were already selected, but they may become unselected if the keyboard navigation temporarily puts them between the anchor cell and the current cell.
|
||||
|
||||
If <xref:System.Windows.Forms.DataGridView.MultiSelect%2A> is set to `true` and <xref:System.Windows.Forms.DataGridView.SelectionMode%2A> is set to <xref:System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect> or <xref:System.Windows.Forms.DataGridViewSelectionMode.FullColumnSelect>, the behavior of the anchor cell and current cell is the same, but only full rows or columns become selected or unselected.
|
||||
|
||||
## Default mouse handling
|
||||
|
||||
### Basic mouse handling
|
||||
|
||||
> [!NOTE]
|
||||
> Clicking a cell with the left mouse button always changes the current cell. Clicking a cell with the right mouse button opens a shortcut menu, when one is available.
|
||||
|
||||
|Mouse action|Description|
|
||||
|------------------|-----------------|
|
||||
|Left mouse button down|Makes the clicked cell the current cell, and raises the <xref:System.Windows.Forms.DataGridView.CellMouseDown?displayProperty=nameWithType> event.|
|
||||
|Left mouse button up|Raises the <xref:System.Windows.Forms.DataGridView.CellMouseUp?displayProperty=nameWithType> event|
|
||||
|Left mouse button click|Raises the <xref:System.Windows.Forms.DataGridView.CellClick?displayProperty=nameWithType> and <xref:System.Windows.Forms.DataGridView.CellMouseClick?displayProperty=nameWithType> events|
|
||||
|Left mouse button down, and drag on a column header cell|If the <xref:System.Windows.Forms.DataGridView.AllowUserToOrderColumns%2A?displayProperty=nameWithType> property is `true`, moves the column so that it can be dropped into a new position.|
|
||||
|
||||
### Mouse selection
|
||||
|
||||
No selection behavior is associated with the middle mouse button or the mouse wheel.
|
||||
|
||||
If the <xref:System.Windows.Forms.DataGridView.MultiSelect%2A> property is set to `false` and the <xref:System.Windows.Forms.DataGridView.SelectionMode%2A> property is set to <xref:System.Windows.Forms.DataGridViewSelectionMode.CellSelect>, the following behavior occurs.
|
||||
|
||||
|Mouse action|Description|
|
||||
|------------------|-----------------|
|
||||
|Click left mouse button|Selects only the current cell if the user clicks a cell. No selection behavior if the user clicks a row or column header.|
|
||||
|Click right mouse button|Displays a shortcut menu if one is available.|
|
||||
|
||||
The same behavior occurs when the <xref:System.Windows.Forms.DataGridView.SelectionMode%2A> is set to <xref:System.Windows.Forms.DataGridViewSelectionMode.RowHeaderSelect> or <xref:System.Windows.Forms.DataGridViewSelectionMode.ColumnHeaderSelect>, except that, depending on the selection mode, clicking a row or column header will select the full row or column and set the current cell to the first cell in the row or column.
|
||||
|
||||
If <xref:System.Windows.Forms.DataGridView.SelectionMode%2A> is set to <xref:System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect> or <xref:System.Windows.Forms.DataGridViewSelectionMode.FullColumnSelect>, clicking any cell in a row or column will select the full row or column.
|
||||
|
||||
If <xref:System.Windows.Forms.DataGridView.MultiSelect%2A> is set to `true`, clicking a cell while pressing CTRL or SHIFT will modify a multi-cell selection.
|
||||
|
||||
When you click a cell while pressing CTRL, the cell will change its selection state while all other cells retain their current selection state.
|
||||
|
||||
When you click a cell or a series of cells while pressing SHIFT, the selection includes all cells between the current cell and an anchor cell located at the position of the current cell before the first click. When you click and drag the pointer across multiple cells, the anchor cell is the cell clicked at the beginning of the drag operation. Subsequent clicks while pressing SHIFT change the current cell, but not the anchor cell. Other cells in the control will remain selected if they were already selected, but they may become unselected if mouse navigation temporarily puts them between the anchor cell and the current cell.
|
||||
|
||||
If <xref:System.Windows.Forms.DataGridView.MultiSelect%2A> is set to `true` and <xref:System.Windows.Forms.DataGridView.SelectionMode%2A> is set to <xref:System.Windows.Forms.DataGridViewSelectionMode.RowHeaderSelect> or <xref:System.Windows.Forms.DataGridViewSelectionMode.ColumnHeaderSelect>, clicking a row or column header (depending on the selection mode) while pressing SHIFT will modify an existing selection of full rows or columns if such a selection exists. Otherwise, it will clear the selection and start a new selection of full rows or columns. Clicking a row or column header while pressing CTRL, however, will add or remove the clicked row or column from the current selection without otherwise modifying the current selection.
|
||||
|
||||
If <xref:System.Windows.Forms.DataGridView.MultiSelect%2A> is set to `true` and <xref:System.Windows.Forms.DataGridView.SelectionMode%2A> is set to <xref:System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect> or <xref:System.Windows.Forms.DataGridViewSelectionMode.FullColumnSelect>, clicking a cell while pressing SHIFT or CTRL behaves the same way except that only full rows and columns are affected.
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.DataGridView>
|
||||
- [DataGridView Control](datagridview-control-windows-forms.md)
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
---
|
||||
title: Define control properties
|
||||
ms.date: "03/30/2017"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
helpviewer_keywords:
|
||||
- "properties [Windows Forms], defining in code"
|
||||
- "custom controls [Windows Forms], defining properties in code"
|
||||
ms.assetid: c2eb8277-a842-4d99-89a9-647b901a0434
|
||||
---
|
||||
# Defining a Property in Windows Forms Controls
|
||||
For an overview of properties, see [Properties Overview](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2013/65zdfbdt(v=vs.120)). There are a few important considerations when defining a property:
|
||||
|
||||
- You must apply attributes to the properties you define. Attributes specify how the designer should display a property. For details, see [Design-Time Attributes for Components](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2013/tk67c2t8(v=vs.120)).
|
||||
|
||||
- If changing the property affects the visual display of the control, call the <xref:System.Windows.Forms.Control.Invalidate%2A> method (that your control inherits from <xref:System.Windows.Forms.Control>) from the `set` accessor. <xref:System.Windows.Forms.Control.Invalidate%2A> in turn calls the <xref:System.Windows.Forms.Control.OnPaint%2A> method, which redraws the control. Multiple calls to <xref:System.Windows.Forms.Control.Invalidate%2A> result in a single call to <xref:System.Windows.Forms.Control.OnPaint%2A> for efficiency.
|
||||
|
||||
- The .NET Framework class library provides type converters for common data types such as integers, decimal numbers, Boolean values, and others. The purpose of a type converter is generally to provide string-to-value conversion (from string data to other data types). Common data types are associated with default type converters that convert values into strings and strings into the appropriate data types. If you define a property that is a custom (that is, nonstandard) data type, you will have to apply an attribute that specifies the type converter to associate with that property. You can also use an attribute to associate a custom UI type editor with a property. A UI type editor provides a user interface for editing a property or data type. A color picker is an example of a UI type editor. Examples of attributes are given at the end of this topic.
|
||||
|
||||
> [!NOTE]
|
||||
> If a type converter or a UI type editor is not available for your custom property, you can implement one as described in [Extending Design-Time Support](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2013/37899azc(v=vs.120)).
|
||||
|
||||
The following code fragment defines a custom property named `EndColor` for the custom control `FlashTrackBar`.
|
||||
|
||||
```vb
|
||||
Public Class FlashTrackBar
|
||||
Inherits Control
|
||||
...
|
||||
' Private data member that backs the EndColor property.
|
||||
Private _endColor As Color = Color.LimeGreen
|
||||
|
||||
' The Category attribute tells the designer to display
|
||||
' it in the Flash grouping.
|
||||
' The Description attribute provides a description of
|
||||
' the property.
|
||||
<Category("Flash"), _
|
||||
Description("The ending color of the bar.")> _
|
||||
Public Property EndColor() As Color
|
||||
' The public property EndColor accesses _endColor.
|
||||
Get
|
||||
Return _endColor
|
||||
End Get
|
||||
Set
|
||||
_endColor = value
|
||||
If Not (baseBackground Is Nothing) And showGradient Then
|
||||
baseBackground.Dispose()
|
||||
baseBackground = Nothing
|
||||
End If
|
||||
' The Invalidate method calls the OnPaint method, which redraws
|
||||
' the control.
|
||||
Invalidate()
|
||||
End Set
|
||||
End Property
|
||||
...
|
||||
End Class
|
||||
```
|
||||
|
||||
```csharp
|
||||
public class FlashTrackBar : Control {
|
||||
...
|
||||
// Private data member that backs the EndColor property.
|
||||
private Color endColor = Color.LimeGreen;
|
||||
// The Category attribute tells the designer to display
|
||||
// it in the Flash grouping.
|
||||
// The Description attribute provides a description of
|
||||
// the property.
|
||||
[
|
||||
Category("Flash"),
|
||||
Description("The ending color of the bar.")
|
||||
]
|
||||
// The public property EndColor accesses endColor.
|
||||
public Color EndColor {
|
||||
get {
|
||||
return endColor;
|
||||
}
|
||||
set {
|
||||
endColor = value;
|
||||
if (baseBackground != null && showGradient) {
|
||||
baseBackground.Dispose();
|
||||
baseBackground = null;
|
||||
}
|
||||
// The Invalidate method calls the OnPaint method, which redraws
|
||||
// the control.
|
||||
Invalidate();
|
||||
}
|
||||
}
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
The following code fragment associates a type converter and a UI type editor with the property `Value`. In this case `Value` is an integer and has a default type converter, but the <xref:System.ComponentModel.TypeConverterAttribute> attribute applies a custom type converter (`FlashTrackBarValueConverter`) that enables the designer to display it as a percentage. The UI type editor, `FlashTrackBarValueEditor`, allows the percentage to be displayed visually. This example also shows that the type converter or editor specified by the <xref:System.ComponentModel.TypeConverterAttribute> or <xref:System.ComponentModel.EditorAttribute> attribute overrides the default converter.
|
||||
|
||||
```vb
|
||||
<Category("Flash"), _
|
||||
TypeConverter(GetType(FlashTrackBarValueConverter)), _
|
||||
Editor(GetType(FlashTrackBarValueEditor), _
|
||||
GetType(UITypeEditor)), _
|
||||
Description("The current value of the track bar. You can enter an actual value or a percentage.")> _
|
||||
Public ReadOnly Property Value() As Integer
|
||||
...
|
||||
End Property
|
||||
```
|
||||
|
||||
```csharp
|
||||
[
|
||||
Category("Flash"),
|
||||
TypeConverter(typeof(FlashTrackBarValueConverter)),
|
||||
Editor(typeof(FlashTrackBarValueEditor), typeof(UITypeEditor)),
|
||||
Description("The current value of the track bar. You can enter an actual value or a percentage.")
|
||||
]
|
||||
public int Value {
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [Properties in Windows Forms Controls](properties-in-windows-forms-controls.md)
|
||||
- [Defining Default Values with the ShouldSerialize and Reset Methods](defining-default-values-with-the-shouldserialize-and-reset-methods.md)
|
||||
- [Property-Changed Events](property-changed-events.md)
|
||||
- [Attributes in Windows Forms Controls](attributes-in-windows-forms-controls.md)
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
---
|
||||
title: Define an event in controls
|
||||
ms.date: "03/30/2017"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
helpviewer_keywords:
|
||||
- "events [Windows Forms], defining within Windows Forms custom controls"
|
||||
- "custom controls [Windows Forms], events using code"
|
||||
ms.assetid: d89f1096-8061-42e2-a855-a1f053f1940a
|
||||
---
|
||||
# Defining an Event in Windows Forms Controls
|
||||
For details about defining custom events, see [Events](https://docs.microsoft.com/dotnet/standard/events/index). If you define an event that does not have any associated data, use the base type for event data, <xref:System.EventArgs>, and use <xref:System.EventHandler> as the event delegate. All that remains to do is to define an event member and a protected `On`*EventName* method that raises the event.
|
||||
|
||||
The following code fragment shows how the `FlashTrackBar` custom control defines a custom event, `ValueChanged`. For the complete code for the `FlashTrackBar` sample, see the [How to: Create a Windows Forms Control That Shows Progress](how-to-create-a-windows-forms-control-that-shows-progress.md).
|
||||
|
||||
```vb
|
||||
Option Explicit
|
||||
Option Strict
|
||||
|
||||
Imports System
|
||||
Imports System.Windows.Forms
|
||||
Imports System.Drawing
|
||||
|
||||
Public Class FlashTrackBar
|
||||
Inherits Control
|
||||
|
||||
' The event does not have any data, so EventHandler is adequate
|
||||
' as the event delegate.
|
||||
' Define the event member using the event keyword.
|
||||
' In this case, for efficiency, the event is defined
|
||||
' using the event property construct.
|
||||
Public Event ValueChanged As EventHandler
|
||||
' The protected method that raises the ValueChanged
|
||||
' event when the value has actually
|
||||
' changed. Derived controls can override this method.
|
||||
Protected Overridable Sub OnValueChanged(e As EventArgs)
|
||||
RaiseEvent ValueChanged(Me, e)
|
||||
End Sub
|
||||
End Class
|
||||
```
|
||||
|
||||
```csharp
|
||||
using System;
|
||||
using System.Windows.Forms;
|
||||
using System.Drawing;
|
||||
|
||||
public class FlashTrackBar : Control {
|
||||
// The event does not have any data, so EventHandler is adequate
|
||||
// as the event delegate.
|
||||
private EventHandler onValueChanged;
|
||||
// Define the event member using the event keyword.
|
||||
// In this case, for efficiency, the event is defined
|
||||
// using the event property construct.
|
||||
public event EventHandler ValueChanged {
|
||||
add {
|
||||
onValueChanged += value;
|
||||
}
|
||||
remove {
|
||||
onValueChanged -= value;
|
||||
}
|
||||
}
|
||||
// The protected method that raises the ValueChanged
|
||||
// event when the value has actually
|
||||
// changed. Derived controls can override this method.
|
||||
protected virtual void OnValueChanged(EventArgs e)
|
||||
{
|
||||
onValueChanged?.Invoke(this, e);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- [Events in Windows Forms Controls](events-in-windows-forms-controls.md)
|
||||
- [Events](https://docs.microsoft.com/dotnet/standard/events/index)
|
||||
+143
@@ -0,0 +1,143 @@
|
||||
---
|
||||
title: "Defining Default Values with the ShouldSerialize and Reset Methods"
|
||||
ms.date: "03/30/2017"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
helpviewer_keywords:
|
||||
- "custom controls [Windows Forms], property methods"
|
||||
- "ShouldPersist method"
|
||||
ms.assetid: 7b6c5e00-3771-46b4-9142-5a80d5864a5e
|
||||
---
|
||||
# Defining Default Values with the ShouldSerialize and Reset Methods
|
||||
`ShouldSerialize` and `Reset` are optional methods that you can provide for a property, if the property does not a have simple default value. If the property has a simple default value, you should apply the <xref:System.ComponentModel.DefaultValueAttribute> and supply the default value to the attribute class constructor instead. Either of these mechanisms enables the following features in the designer:
|
||||
|
||||
- The property provides visual indication in the property browser if it has been modified from its default value.
|
||||
|
||||
- The user can right-click on the property and choose **Reset** to restore the property to its default value.
|
||||
|
||||
- The designer generates more efficient code.
|
||||
|
||||
> [!NOTE]
|
||||
> Either apply the <xref:System.ComponentModel.DefaultValueAttribute> or provide `Reset`*PropertyName* and `ShouldSerialize`*PropertyName* methods. Do not use both.
|
||||
|
||||
The `Reset`*PropertyName* method sets a property to its default value, as shown in the following code fragment.
|
||||
|
||||
```vb
|
||||
Public Sub ResetMyFont()
|
||||
MyFont = Nothing
|
||||
End Sub
|
||||
```
|
||||
|
||||
```csharp
|
||||
public void ResetMyFont() {
|
||||
MyFont = null;
|
||||
}
|
||||
```
|
||||
|
||||
> [!NOTE]
|
||||
> If a property does not have a `Reset` method, is not marked with a <xref:System.ComponentModel.DefaultValueAttribute>, and does not have a default value supplied in its declaration, the `Reset` option for that property is disabled in the shortcut menu of the **Properties** window of the Windows Forms Designer in Visual Studio.
|
||||
|
||||
Designers such as Visual Studio use the `ShouldSerialize`*PropertyName* method to check whether a property has changed from its default value and write code into the form only if a property is changed, thus allowing for more efficient code generation. For example:
|
||||
|
||||
```vb
|
||||
'Returns true if the font has changed; otherwise, returns false.
|
||||
' The designer writes code to the form only if true is returned.
|
||||
Public Function ShouldSerializeMyFont() As Boolean
|
||||
Return Not (thefont Is Nothing)
|
||||
End Function
|
||||
```
|
||||
|
||||
```csharp
|
||||
// Returns true if the font has changed; otherwise, returns false.
|
||||
// The designer writes code to the form only if true is returned.
|
||||
public bool ShouldSerializeMyFont() {
|
||||
return thefont != null;
|
||||
}
|
||||
```
|
||||
|
||||
A complete code example follows.
|
||||
|
||||
```vb
|
||||
Option Explicit
|
||||
Option Strict
|
||||
|
||||
Imports System.Drawing
|
||||
Imports System.Windows.Forms
|
||||
|
||||
Public Class MyControl
|
||||
Inherits Control
|
||||
|
||||
' Declare an instance of the Font class
|
||||
' and set its default value to Nothing.
|
||||
Private thefont As Font = Nothing
|
||||
|
||||
' The MyFont property.
|
||||
Public Property MyFont() As Font
|
||||
' Note that the Font property never
|
||||
' returns null.
|
||||
Get
|
||||
If Not (thefont Is Nothing) Then
|
||||
Return thefont
|
||||
End If
|
||||
If Not (Parent Is Nothing) Then
|
||||
Return Parent.Font
|
||||
End If
|
||||
Return Control.DefaultFont
|
||||
End Get
|
||||
Set
|
||||
thefont = value
|
||||
End Set
|
||||
End Property
|
||||
|
||||
Public Function ShouldSerializeMyFont() As Boolean
|
||||
Return Not (thefont Is Nothing)
|
||||
End Function
|
||||
|
||||
Public Sub ResetMyFont()
|
||||
MyFont = Nothing
|
||||
End Sub
|
||||
End Class
|
||||
```
|
||||
|
||||
```csharp
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Windows.Forms;
|
||||
|
||||
public class MyControl : Control {
|
||||
// Declare an instance of the Font class
|
||||
// and set its default value to null.
|
||||
private Font thefont = null;
|
||||
|
||||
// The MyFont property.
|
||||
public Font MyFont {
|
||||
// Note that the MyFont property never
|
||||
// returns null.
|
||||
get {
|
||||
if (thefont != null) return thefont;
|
||||
if (Parent != null) return Parent.Font;
|
||||
return Control.DefaultFont;
|
||||
}
|
||||
set {
|
||||
thefont = value;
|
||||
}
|
||||
}
|
||||
|
||||
public bool ShouldSerializeMyFont() {
|
||||
return thefont != null;
|
||||
}
|
||||
|
||||
public void ResetMyFont() {
|
||||
MyFont = null;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
In this case, even when the value of the private variable accessed by the `MyFont` property is `null`, the property browser does not display `null`; instead, it displays the <xref:System.Windows.Forms.Control.Font%2A> property of the parent, if it is not `null`, or the default <xref:System.Windows.Forms.Control.Font%2A> value defined in <xref:System.Windows.Forms.Control>. Thus the default value for `MyFont` cannot be simply set, and a <xref:System.ComponentModel.DefaultValueAttribute> cannot be applied to this property. Instead, the `ShouldSerialize` and `Reset` methods must be implemented for the `MyFont` property.
|
||||
|
||||
## See also
|
||||
|
||||
- [Properties in Windows Forms Controls](properties-in-windows-forms-controls.md)
|
||||
- [Defining a Property](defining-a-property-in-windows-forms-controls.md)
|
||||
- [Property-Changed Events](property-changed-events.md)
|
||||
+407
@@ -0,0 +1,407 @@
|
||||
---
|
||||
title: Design-time errors in the Windows Forms Designer
|
||||
titleSuffix: ""
|
||||
description: Learn about the errors that occur when the Windows Forms Designer fails to load due to an error in your code, in a third-party component, or elsewhere.
|
||||
ms.date: 09/09/2019
|
||||
f1_keywords:
|
||||
- "DTELErrorList"
|
||||
- "WhyDTELPage"
|
||||
helpviewer_keywords:
|
||||
- "errors [Windows Forms Designer]"
|
||||
- "design-time errors [Windows Forms Designer]"
|
||||
ms.assetid: ad408380-825a-46d8-9a4a-531b130b88ce
|
||||
author: jillre
|
||||
ms.author: jillfra
|
||||
manager: jillfra
|
||||
---
|
||||
# Windows Forms Designer error page
|
||||
|
||||
If the Windows Forms Designer fails to load due to an error in your code, in a third-party component, or elsewhere, you'll see an error page instead of the designer. This error page does not necessarily signify a bug in the designer. The bug may be somewhere in the code-behind page that's named \<your-form-name>.Designer.cs. Errors appear in collapsible, yellow bars with a link to jump to the location of the error on the code page.
|
||||
|
||||

|
||||
|
||||
You can choose to ignore the errors and continue loading the designer by clicking **Ignore and Continue**. This action may result in unexpected behavior, for example, controls may not appear on the design surface.
|
||||
|
||||
## Instances of this error
|
||||
|
||||
When the yellow error bar is expanded, each instance of the error is listed. Many error types include an exact location in the following format: *[Project Name]* *[Form Name]* Line:*[Line Number]* Column:*[Column Number]*. If a call stack is associated with the error, you can click the **Show Call Stack** link to see it. Examining the call stack may further help you resolve the error.
|
||||
|
||||

|
||||
|
||||
> [!NOTE]
|
||||
>
|
||||
> - For Visual Basic apps, the design-time error page does not display more than one error, but it may display multiple instances of the same error.
|
||||
> - For C++ apps, errors don't have code location links.
|
||||
|
||||
## Help with this error
|
||||
|
||||
If a help topic for the error is available, click the **MSDN Help** link to navigate directly to the help page on docs.microsoft.com.
|
||||
|
||||
## Forum posts about this error
|
||||
|
||||
Click the **Search the MSDN Forums for posts related to this error** link to navigate to the Microsoft Developer Network forums. You may want to specifically search the [Windows Forms Designer](https://social.msdn.microsoft.com/Forums/windows/home?forum=winformsdesigner) or [Windows Forms](https://social.msdn.microsoft.com/Forums/windows/home?category=windowsforms) forums.
|
||||
|
||||
## Design-time errors
|
||||
|
||||
This section lists some of the errors you may encounter.
|
||||
|
||||
### '\<identifier name>' is not a valid identifier
|
||||
|
||||
This error indicates that a field, method, event, or object is improperly named.
|
||||
|
||||
### '\<name>' already exists in '\<project name>'
|
||||
|
||||
Error message: "'\<name>' already exists in '\<project name>'. Please enter a unique name."
|
||||
|
||||
You've specified a name for an inherited form that already exists in the project. To correct this error, give the inherited form a unique name.
|
||||
|
||||
### '\<Toolbox tab name>' is not a toolbox category
|
||||
|
||||
A third-party designer has tried to access a tab on the Toolbox that does not exist. Contact the component vendor.
|
||||
|
||||
### A requested language parser is not installed
|
||||
|
||||
Error message: "A requested language parser is not installed. The language parser name is '{0}'."
|
||||
|
||||
Visual Studio attempted to a load a designer that's registered for the file type but could not. This is most likely because of an error that occurred during setup. Contact the vendor of the language you're using for a fix.
|
||||
|
||||
### A service required for generating and parsing source code is missing
|
||||
|
||||
This is a problem with a third-party component. Contact the component vendor.
|
||||
|
||||
### An exception occurred while trying to create an instance of '\<object name>'
|
||||
|
||||
Error message: "An exception occurred while trying to create an instance of '\<object name>'. The exception was "\<exception string\>".
|
||||
|
||||
A third-party designer requested that Visual Studio create an object, but the object raised an error. Contact the component vendor.
|
||||
|
||||
### Another editor has '\<document name>' open in an incompatible mode
|
||||
|
||||
Error message: "Another editor has '\<document name>' open in an incompatible mode. Please close the editor and try this operation again."
|
||||
|
||||
This error arises if you try to open a file that is already opened in another editor. The editor that already has the file open is shown. To correct this error, close the editor that has the file open, and try again.
|
||||
|
||||
### Another editor has made changes to '\<document name>'
|
||||
|
||||
Close and reopen the designer for the changes to take effect. Normally, Visual Studio automatically reloads a designer after changes are made. However, other designers, such as third-party component designers, may not support reload behavior. In this case, Visual Studio prompts you to close and reopen the designer manually.
|
||||
|
||||
### Another editor has the file open in an incompatible mode
|
||||
|
||||
Error message: "Another editor has the file open in an incompatible mode. Please close the editor and try this operation again."
|
||||
|
||||
This message is similar to "Another editor has '\<document name>' open in an incompatible mode", but Visual Studio is unable to determine the file name. To correct this error, close the editor that has the file open, and try again.
|
||||
|
||||
### Array rank '\<rank in array>' is too high
|
||||
|
||||
Visual Studio only supports single-dimension arrays in the code block that's parsed by the designer. Multidimensional arrays are valid outside this area.
|
||||
|
||||
### Assembly '\<assembly name>' could not be opened
|
||||
|
||||
Error message: "Assembly '\<assembly name>' could not be opened. Verify that the file still exists."
|
||||
|
||||
This error message arises when you try to open a file that could not be opened. Verify that the file exists and is a valid assembly.
|
||||
|
||||
### Bad element type. This serializer expects an element of type '\<type name>'
|
||||
|
||||
This is a problem with a third-party component. Contact the component vendor.
|
||||
|
||||
### Cannot access the Visual Studio Toolbox at this time
|
||||
|
||||
Visual Studio made a call to the Toolbox, which was not available. If you see this error, If you see this error, please log an issue by using [Report a Problem](/visualstudio/ide/how-to-report-a-problem-with-visual-studio).
|
||||
|
||||
### Cannot bind an event handler to the '\<event name>' event because it is read-only
|
||||
|
||||
This error most often arises when you've tried to connect an event to a control that's inherited from a base class. If the control's member variable is private, Visual Studio cannot connect the event to the method. Privately inherited controls cannot have additional events bound to them.
|
||||
|
||||
### Cannot create a method name for the requested component because it is not a member of the design container
|
||||
|
||||
Visual Studio has tried to add an event handler to a component that does not have a member variable in the designer. Contact the component vendor.
|
||||
|
||||
### Cannot name the object '\<name>' because it is already named '\<name>'
|
||||
|
||||
This is an internal error in the Visual Studio serializer. It indicates that the serializer has tried to name an object twice, which is not supported. If you see this error, please log an issue by using [Report a Problem](/visualstudio/ide/how-to-report-a-problem-with-visual-studio).
|
||||
|
||||
### Cannot remove or destroy inherited component '\<component name>'
|
||||
|
||||
Inherited controls are under the ownership of their inheriting class. Changes to the inherited control must be made in the class from which the control originates. Thus, you cannot rename or destroy it.
|
||||
|
||||
### Category '\<Toolbox tab name>' does not have a tool for class '\<class name>'
|
||||
|
||||
The designer tried to reference a class on a particular Toolbox tab, but the class does not exist. Contact the component vendor.
|
||||
|
||||
### Class '\<class name>' has no matching constructor
|
||||
|
||||
A third-party designer has asked Visual Studio to create an object with particular parameters in the constructor that does not exist. Contact the component vendor.
|
||||
|
||||
### Code generation for property '\<property name>' failed
|
||||
|
||||
This is a generic wrapper for an error. The error string that accompanies this message will give more details about the error message and have a link to a more specific help topic. To correct this error, address the error specified in the error message appended to this error.
|
||||
|
||||
### Component '\<component name>' did not call Container.Add() in its constructor
|
||||
|
||||
This is an error in the component you just loaded or placed on the form. It indicates that the component did not add itself to its container control (whether that is another control or a form). The designer will continue to work, but there may be problems with the component at run time.
|
||||
|
||||
To correct the error, contact the component vendor. Or, if it is a component you created, call the `IContainer.Add` method in the component's constructor.
|
||||
|
||||
### Component name cannot be empty
|
||||
|
||||
This error arises when you try to rename a component to an empty value.
|
||||
|
||||
### Could not access the variable '\<variable name>' because it has not been initialized yet
|
||||
|
||||
This error can arise because of two scenarios. Either a third-party component vendor has a problem with a control or component they have distributed, or the code you have written has recursive dependencies between components.
|
||||
|
||||
To correct this error, ensure that your code does not have a recursive dependency. If it is free of such problems, note the exact text of the error message and contact the component vendor.
|
||||
|
||||
### Could not find type '\<type name>'
|
||||
|
||||
Error message: "Could not find type '\<type name>'. Please make sure that the assembly that contains this type is referenced. If this type is a part of your development project, make sure that the project has been successfully built."
|
||||
|
||||
This error occurred because a reference was not found. Make sure the type indicated in the error message is referenced, and that any assemblies that the type requires are also referenced. Often, the problem is that a control in the solution has not been built. To build, select **Build Solution** from the **Build** menu. Otherwise, if the control has already been built, add a reference manually from the right-click menu of the **References** or **Dependencies** folder in Solution Explorer.
|
||||
|
||||
### Could not load type '\<type name>'
|
||||
|
||||
Error message: "Could not load type '\<type name>'. Please make sure that the assembly containing this type is added to the project references."
|
||||
|
||||
Visual Studio attempted to wire up an event-handling method and could not find one or more parameter types for the method. This is usually caused by a missing reference. To correct this error, add the reference containing the type to the project and try again.
|
||||
|
||||
### Could not locate the project item templates for inherited components
|
||||
|
||||
The templates for inherited forms in Visual Studio are not available. If you see this error, please log an issue by using [Report a Problem](/visualstudio/ide/how-to-report-a-problem-with-visual-studio).
|
||||
|
||||
### Delegate class '\<class name>' has no invoke method. Is this class a delegate?
|
||||
|
||||
Visual Studio has tried to create an event handler, but there is something wrong with the event type. This can happen if the event was created by a non-CLS-compliant language. Contact the component vendor.
|
||||
|
||||
### Duplicate declaration of member '\<member name>'
|
||||
|
||||
This error arises because a member variable has been declared twice (for example, two controls named `Button1` are declared in the code). Names must be unique across inherited forms. Additionally, names cannot differ only by case.
|
||||
|
||||
### Error reading resources from the resource file for the culture '\<culture name>'
|
||||
|
||||
This error can occur if there is a bad .resx file in the project.
|
||||
|
||||
To correct this error:
|
||||
|
||||
1. Click the **Show All Files** button in Solution Explorer to view the .resx files associated with the solution.
|
||||
2. Load the .resx file in the XML Editor by right-clicking the .resx file and choosing **Open**.
|
||||
3. Edit the .resx file manually to address the errors.
|
||||
|
||||
### Error reading resources from the resource file for the default culture '\<culture name>'
|
||||
|
||||
This error can occur if there is a bad .resx file in the project for the default culture.
|
||||
|
||||
To correct this error:
|
||||
|
||||
1. Click the **Show All Files** button in Solution Explorer to view the .resx files associated with the solution.
|
||||
2. Load the .resx file in the XML Editor by right-clicking the .resx file and choosing **Open**.
|
||||
3. Edit the .resx file manually to address the errors.
|
||||
|
||||
### Failed to parse method '\<method name>'
|
||||
|
||||
Error message: "Failed to parse method '\<method name>'. The parser reported the following error: '\<error string>'. Please look in the Task List for potential errors."
|
||||
|
||||
This is a general error message for problems that arise during parsing. These errors are often due to syntax errors. See the Task List for specific messages related to the error.
|
||||
|
||||
### Invalid component name: '\<component name>'
|
||||
|
||||
You've tried to rename a component to an invalid value for that language. To correct this error, name the component such that it complies with the naming rules for that language.
|
||||
|
||||
### The type '\<class name>' is made of several partial classes in the same file
|
||||
|
||||
When you define a class in multiple files by using the [partial](https://docs.microsoft.com/dotnet/csharp/language-reference/keywords/partial-type) keyword, you can only have one partial definition in each file.
|
||||
|
||||
To correct this error, remove all but one of the partial definitions of your class from the file.
|
||||
|
||||
### The assembly '\<assembly name>' could not be found
|
||||
|
||||
Error message: "The assembly '\<assembly name>' could not be found. Ensure that the assembly is referenced. If the assembly is part of the current development project, ensure that the project has been built."
|
||||
|
||||
This error is similar to "The type '\<type name>' could not be found", but this error usually happens because of a metadata attribute. To correct this error, check that all assemblies used by attributes are referenced.
|
||||
|
||||
### The assembly name '\<assembly name>' is invalid
|
||||
|
||||
A component has requested a particular assembly, but the name provided by the component is not a valid assembly name. Contact the component vendor.
|
||||
|
||||
### The base class '\<class name>' cannot be designed
|
||||
|
||||
Visual Studio loaded the class, but the class cannot be designed because the implementer of the class did not provide a designer. If the class supports a designer, make sure there are no problems that would cause issues with displaying it in a designer, such as compiler errors. Also, make sure that all references to the class are correct and all class names are correctly spelled. Otherwise, if the class is not designable, edit it in Code view.
|
||||
|
||||
### The base class '\<class name>' could not be loaded
|
||||
|
||||
The class is not referenced in the project, so Visual Studio can't load it. To correct this error, add a reference to the class in the project, and close and reopen the Windows Forms Designer window.
|
||||
|
||||
### The class '\<class name>' cannot be designed in this version of Visual Studio
|
||||
|
||||
The designer for this control or component does not support the same types that Visual Studio does. Contact the component vendor.
|
||||
|
||||
### The class name is not a valid identifier for this language
|
||||
|
||||
The source code being created by the user has a class name that is not valid for the language being used. To correct this error, name the class such that it conforms to the language requirements.
|
||||
|
||||
### The component cannot be added because it contains a circular reference to '\<reference name>'
|
||||
|
||||
You cannot add a control or component to itself. Another situation where this might occur is if there is code in the InitializeComponent method of a form (for example, Form1) that creates another instance of Form1.
|
||||
|
||||
### The designer cannot be modified at this time
|
||||
|
||||
This error occurs when the file in the editor is marked as read-only. Ensure that the file is not marked read-only and the application is not running.
|
||||
|
||||
### The designer could not be shown for this file because none of the classes within it can be designed
|
||||
|
||||
This error occurs when Visual Studio cannot find a base class that satisfies designer requirements. Forms and controls must derive from a base class that supports designers. If you're deriving from an inherited form or control, make sure the project has been built.
|
||||
|
||||
### The designer for base class '\<class name>' is not installed
|
||||
|
||||
Visual Studio could not load the designer for the class. If you see this error, please log an issue by using [Report a Problem](/visualstudio/ide/how-to-report-a-problem-with-visual-studio).
|
||||
|
||||
### The designer must create an instance of type '\<type name>', but it can't because the type is declared as abstract
|
||||
|
||||
This error occurred because the base class of the object being passed to the designer is [abstract](https://docs.microsoft.com/dotnet/csharp/language-reference/keywords/abstract), which is not allowed.
|
||||
|
||||
### The file could not be loaded in the designer
|
||||
|
||||
The base class of this file does not support any designers. As a workaround, use Code view to work on the file. Right-click the file in Solution Explorer and choose **View Code**.
|
||||
|
||||
### The language for this file does not support the necessary code parsing and generation services
|
||||
|
||||
Error message: "The language for this file does not support the necessary code parsing and generation services. Please ensure the file you are opening is a member of a project and then try to open the file again."
|
||||
|
||||
This error most likely resulted from opening a file that's in a project that does not support designers.
|
||||
|
||||
### The language parser class '\<class name>' is not implemented properly
|
||||
|
||||
Error message: "The language parser class '\<class name>' is not implemented properly. Contact the vendor for an updated parser module."
|
||||
|
||||
The language in use has registered a designer class that doesn't derive from the correct base class. Contact the vendor of the language you're using.
|
||||
|
||||
### The name '\<name>' is already used by another object
|
||||
|
||||
This is an internal error in the Visual Studio serializer. If you see this error, please log an issue by using [Report a Problem](/visualstudio/ide/how-to-report-a-problem-with-visual-studio).
|
||||
|
||||
### The object '\<object name>' does not implement the IComponent interface
|
||||
|
||||
Visual Studio tried to create a component, but the object created does not implement the <xref:System.ComponentModel.IComponent> interface. Contact the component vendor for a fix.
|
||||
|
||||
### The object '\<object name>' returned null for the property '\<property name>' but this is not allowed
|
||||
|
||||
There are some .NET properties that should always return an object. For example, the **Controls** collection of a form should always return an object, even when there are no controls in it.
|
||||
|
||||
To correct this error, ensure that the property specified in the error is not null.
|
||||
|
||||
### The serialization data object is not of the proper type
|
||||
|
||||
A data object offered by the serializer is not an instance of a type that matches the current serializer being used. Contact the component vendor.
|
||||
|
||||
### The service '\<service name>' is required, but could not be located
|
||||
|
||||
Error message: "The service '\<service name>' is required, but could not be located. There may be a problem with your Visual Studio installation."
|
||||
|
||||
A service required by Visual Studio is unavailable. If you were trying to load a project that does not support that designer, use the Code Editor to make the changes you require. Otherwise, If you see this error, please log an issue by using [Report a Problem](/visualstudio/ide/how-to-report-a-problem-with-visual-studio).
|
||||
|
||||
### The service instance must derive from or implement '\<interface name>'
|
||||
|
||||
This error indicates that a component or component designer has called the **AddService** method, which requires an interface and object, but the object specified does not implement the interface specified. Contact the component vendor.
|
||||
|
||||
### The text in the code window could not be modified
|
||||
|
||||
Error message: "The text in the code window could not be modified. Check that the file is not read-only and there is sufficient disk space."
|
||||
|
||||
This error occurs when Visual Studio is unable to edit a file due to disk space or memory problems, or the file is marked read-only.
|
||||
|
||||
### The Toolbox enumerator object only supports retrieving one item at a time
|
||||
|
||||
If you see this error, If you see this error, please log an issue by using [Report a Problem](/visualstudio/ide/how-to-report-a-problem-with-visual-studio).
|
||||
|
||||
### The Toolbox item for '\<component name>' could not be retrieved from the Toolbox
|
||||
|
||||
Error message: "The Toolbox item for '\<component name>' could not be retrieved from the Toolbox. Make sure the assembly that contains the Toolbox item is correctly installed. The Toolbox item raised the following error: \<error string>."
|
||||
|
||||
The component in question threw an exception when Visual Studio accessed it. Contact the component vendor.
|
||||
|
||||
### The Toolbox item for '\<Toolbox item name>' could not be retrieved from the Toolbox
|
||||
|
||||
Error message: "The Toolbox item for '\<Toolbox item name>' could not be retrieved from the Toolbox. Try removing the item from the Toolbox and adding it back."
|
||||
|
||||
This error occurs if the data within the Toolbox item becomes corrupted or the version of the component has changed. Try removing the item from the Toolbox and adding it back again.
|
||||
|
||||
### The type '\<type name>' could not be found
|
||||
|
||||
Error message: "The type '\<type name>' could not be found. Ensure that the assembly containing the type is referenced. If the assembly is part of the current development project, ensure that the project has been built."
|
||||
|
||||
While loading the designer, Visual Studio failed to find a type. Ensure that the assembly containing the type is referenced. If the assembly is part of the current development project, ensure that the project has been built.
|
||||
|
||||
### The type resolution service may only be called from the main application thread
|
||||
|
||||
Visual Studio attempted to access required resources from the wrong thread. This error is displayed when the code used to create the designer has called the type resolution service from a thread other than the main application thread. To correct this error, call the service from the correct thread or contact the component vendor.
|
||||
|
||||
### The variable '\<variable name>' is either undeclared or was never assigned
|
||||
|
||||
The source code has a reference to a variable, such as **Button1**, that isn't declared or assigned. If the variable has not been assigned, this message appears as a warning, not an error.
|
||||
|
||||
### There is already a command handler for the menu command '\<menu command name>'
|
||||
|
||||
This error arises if a third-party designer adds a command that already has a handler to the command table. Contact the component vendor.
|
||||
|
||||
### There is already a component named '\<component name>'
|
||||
|
||||
Error message: "There is already a component named '\<component name>'. Components must have unique names, and names must not be case-sensitive. A name also cannot conflict with the name of any component in an inherited class."
|
||||
|
||||
This error message arises when there has been a change to the name of a component in the Properties window. To correct this error, ensure that all component names are unique, are not case-sensitive, and do not conflict with the names of any components in the inherited classes.
|
||||
|
||||
### There is already a Toolbox item creator registered for the format '\<format name>'
|
||||
|
||||
A third-party component made a callback to an item on a Toolbox tab, but the item already contained a callback. Contact the component vendor.
|
||||
|
||||
### This language engine does not support a CodeModel with which to load a designer
|
||||
|
||||
This message is similar to "The language for this file does not support the necessary code parsing and generation services", but this message involves an internal registration problem. If you see this error, If you see this error, please log an issue by using [Report a Problem](/visualstudio/ide/how-to-report-a-problem-with-visual-studio).
|
||||
|
||||
### Type '\<type name\>' does not have a constructor with parameters of types '\<parameter type names>'
|
||||
|
||||
Visual Studio could not find a [constructor](https://docs.microsoft.com/dotnet/csharp/programming-guide/classes-and-structs/constructors) that had matching parameters. This may be the result of supplying a constructor with types other than those that are required. For example, a **Point** constructor might take two integers. If you provided floats, this error is raised.
|
||||
|
||||
To correct this error, use a different constructor or explicitly cast the parameter types such that they match those provided by the constructor.
|
||||
|
||||
### Unable to add reference '\<reference name>' to the current application
|
||||
|
||||
Error message: "Unable to add reference '\<reference name>' to the current application. Check that a different version of '\<reference name>' is not already referenced."
|
||||
|
||||
Visual Studio is unable to add a reference. To correct this error, check that a different version of the reference is not already referenced.
|
||||
|
||||
### Unable to check out the current file
|
||||
|
||||
Error message: "Unable to check out the current file. The file may be locked, or you may need to check out the file manually."
|
||||
|
||||
This error arises when you change a file that's currently checked in to source-code control. Usually, Visual Studio presents the file checkout dialog box so that the user can check out the file. This time, the file was not checked out, perhaps because of a merge conflict during checkout. To correct this error, ensure that the file is not locked, and then try to check out the file manually.
|
||||
|
||||
### Unable to find page named '\<Options dialog box tab name>'
|
||||
|
||||
This error arises when a component designer requests access to a page from the Options dialog box by using a name that does not exist. Contact the component vendor.
|
||||
|
||||
### Unable to find property '\<property name>' on page '\<Options dialog box tab name>'
|
||||
|
||||
This error arises when a component designer requests access to a particular value on a page from the Options dialog box, but that value does not exist. Contact the component vendor.
|
||||
|
||||
### Visual Studio cannot open a designer for the file because the class within it does not inherit from a class that can be visually designed
|
||||
|
||||
Visual Studio loaded the class, but the designer for that class could not be loaded. Visual Studio requires that designers use the first class in a file. To correct this error, move the class code so that it is the first class in the file, and then load the designer again.
|
||||
|
||||
### Visual Studio cannot save or load instances of the type '\<type name>'
|
||||
|
||||
This is a problem with a third-party component. Contact the component vendor.
|
||||
|
||||
### Visual Studio is unable to open '\<document name>' in Design view
|
||||
|
||||
Error message: "Visual Studio is unable to open '\<document name>' in Design view. No parser is installed for the file type."
|
||||
|
||||
This error indicates that the language of the project does not support a designer and arises when you attempt to open a file in the Open File dialog box or from Solution Explorer. Instead, edit the file in Code view.
|
||||
|
||||
### Visual Studio was unable to find a designer for classes of type '\<type name>'
|
||||
|
||||
Visual Studio loaded the class, but the class cannot be designed. Instead, edit the class in Code view by right-clicking the class and choosing **View Code**.
|
||||
|
||||
## See also
|
||||
|
||||
- [Develop Windows Forms controls using the designer](developing-windows-forms-controls-at-design-time.md)
|
||||
- [Windows Forms Designer forum](https://social.msdn.microsoft.com/Forums/windows/home?forum=winformsdesigner)
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
---
|
||||
title: Designate a Button as the Accept Button Using the Designer
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "buttons [Windows Forms], default on Windows Forms"
|
||||
- "Accept button on Windows Forms"
|
||||
- "Button control [Windows Forms], designating as default"
|
||||
- "Windows Forms controls, default button on form"
|
||||
ms.assetid: a1da0590-755f-49f2-aca7-609fac6351bf
|
||||
---
|
||||
# How to: Designate a Windows Forms Button as the Accept Button Using the Designer
|
||||
On any Windows Form, you can designate a <xref:System.Windows.Forms.Button> control to be the accept button, also known as the default button. Whenever the user presses the ENTER key, the default button is clicked regardless of which other control on the form has the focus. The exceptions to this are when the control with focus is another button — in that case, the button with the focus will be clicked — or a multiline text box, or a custom control that traps the ENTER key.
|
||||
|
||||
## To designate the accept button
|
||||
|
||||
1. Select the form on which the button resides.
|
||||
|
||||
2. In the **Properties** window, set the form's <xref:System.Windows.Forms.Form.AcceptButton%2A> property to the <xref:System.Windows.Forms.Button> control's name.
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.Form.AcceptButton%2A>
|
||||
- [Button Control Overview](button-control-overview-windows-forms.md)
|
||||
- [Ways to Select a Windows Forms Button Control](ways-to-select-a-windows-forms-button-control.md)
|
||||
- [How to: Respond to Windows Forms Button Clicks](how-to-respond-to-windows-forms-button-clicks.md)
|
||||
- [How to: Designate a Windows Forms Button as the Cancel Button Using the Designer](designate-a-wf-button-as-the-cancel-button-using-the-designer.md)
|
||||
- [Button Control](button-control-windows-forms.md)
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
---
|
||||
title: Designate a Button as the Cancel Button Using the Designer
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "buttons [Windows Forms], cancel buttons"
|
||||
- "Button control [Windows Forms], designating as cancel button"
|
||||
ms.assetid: 30e77d9c-d565-4ab5-a84a-62c043af8822
|
||||
---
|
||||
# How to: Designate a Windows Forms Button as the Cancel Button Using the Designer
|
||||
On any Windows Form, you can designate a <xref:System.Windows.Forms.Button> control to be the cancel button. A cancel button is clicked whenever the user presses the ESC key, regardless of which other control on the form has the focus. Such a button is usually programmed to enable the user to quickly exit an operation without committing to any action.
|
||||
|
||||
## To designate the cancel button
|
||||
|
||||
1. Select the form on which the button resides.
|
||||
|
||||
2. In the **Properties** window, set the form's <xref:System.Windows.Forms.Form.CancelButton%2A> property to the <xref:System.Windows.Forms.Button> control's name.
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.Form.CancelButton%2A>
|
||||
- [Button Control Overview](button-control-overview-windows-forms.md)
|
||||
- [Ways to Select a Windows Forms Button Control](ways-to-select-a-windows-forms-button-control.md)
|
||||
- [How to: Respond to Windows Forms Button Clicks](how-to-respond-to-windows-forms-button-clicks.md)
|
||||
- [How to: Designate a Windows Forms Button as the Accept Button Using the Designer](designate-a-wf-button-as-the-accept-button-using-the-designer.md)
|
||||
- [Button Control](button-control-windows-forms.md)
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
---
|
||||
title: Determine When Formatting Attributes Change in RichTextBox Control
|
||||
ms.date: "03/30/2017"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
- "cpp"
|
||||
helpviewer_keywords:
|
||||
- "examples [Windows Forms], text boxes"
|
||||
- "RichTextBox control [Windows Forms], determining font changes"
|
||||
- "text boxes [Windows Forms], determining font changes"
|
||||
- "SelChange event"
|
||||
ms.assetid: bdfed015-f77a-41e5-b38f-f8629b2fa166
|
||||
---
|
||||
# How to: Determine When Formatting Attributes Change in the Windows Forms RichTextBox Control
|
||||
A common use of the Windows Forms <xref:System.Windows.Forms.RichTextBox> control is formatting text with attributes such as font options or paragraph styles. Your application may need to keep track of any changes in text formatting for the purpose of displaying a toolbar, as in many word-processing applications.
|
||||
|
||||
### To respond to changes in formatting attributes
|
||||
|
||||
1. Write code in the <xref:System.Windows.Forms.RichTextBox.SelectionChanged> event handler to perform an appropriate action depending on the value of the attribute. The following example changes the appearance of a toolbar button depending on the value of the <xref:System.Windows.Forms.RichTextBox.SelectionBullet%2A> property. The toolbar button will only be updated when the insertion point is moved in the control.
|
||||
|
||||
The example below assumes a form with a <xref:System.Windows.Forms.RichTextBox> control and a <xref:System.Windows.Forms.ToolBar> control that contains a toolbar button. For more information about toolbars and toolbar buttons, see [How to: Add Buttons to a ToolBar Control](how-to-add-buttons-to-a-toolbar-control.md).
|
||||
|
||||
```vb
|
||||
' The following code assumes the existence of a toolbar control
|
||||
' with at least one toolbar button.
|
||||
Private Sub RichTextBox1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.SelectionChanged
|
||||
If RichTextBox1.SelectionBullet = True Then
|
||||
' Bullet button on toolbar should appear pressed
|
||||
ToolBarButton1.Pushed = True
|
||||
Else
|
||||
' Bullet button on toolbar should appear unpressed
|
||||
ToolBarButton1.Pushed = False
|
||||
End If
|
||||
End Sub
|
||||
```
|
||||
|
||||
```csharp
|
||||
// The following code assumes the existence of a toolbar control
|
||||
// with at least one toolbar button.
|
||||
private void richTextBox1_SelectionChanged(object sender,
|
||||
System.EventArgs e)
|
||||
{
|
||||
if (richTextBox1.SelectionBullet == true)
|
||||
{
|
||||
// Bullet button on toolbar should appear pressed
|
||||
toolBarButton1.Pushed = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Bullet button on toolbar should appear unpressed
|
||||
toolBarButton1.Pushed = false;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```cpp
|
||||
// The following code assumes the existence of a toolbar control
|
||||
// with at least one toolbar button.
|
||||
private:
|
||||
System::Void richTextBox1_SelectionChanged(
|
||||
System::Object ^ sender, System::EventArgs ^ e)
|
||||
{
|
||||
if (richTextBox1->SelectionBullet == true)
|
||||
{
|
||||
// Bullet button on toolbar should appear pressed
|
||||
toolBarButton1->Pushed = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Bullet button on toolbar should appear unpressed
|
||||
toolBarButton1->Pushed = false;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.RichTextBox.SelectionChanged>
|
||||
- <xref:System.Windows.Forms.RichTextBox>
|
||||
- [RichTextBox Control](richtextbox-control-windows-forms.md)
|
||||
- [Controls to Use on Windows Forms](controls-to-use-on-windows-forms.md)
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
---
|
||||
title: Determine Which Panel in StatusBar Control Was Clicked
|
||||
ms.date: "03/30/2017"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
- "cpp"
|
||||
helpviewer_keywords:
|
||||
- "status bars [Windows Forms], determining panel clicked"
|
||||
- "panels [Windows Forms], determining clicked"
|
||||
- "StatusBar control [Windows Forms], coding panel click events"
|
||||
- "StatusBar control [Windows Forms], determining panel clicked"
|
||||
- "PanelClick event [Windows Forms], determining panel clicked"
|
||||
- "Panel control [Windows Forms], determining click"
|
||||
ms.assetid: d14c6092-04b2-4a07-8ddf-0dd11277ff5f
|
||||
---
|
||||
# How to: Determine Which Panel in the Windows Forms StatusBar Control Was Clicked
|
||||
> [!IMPORTANT]
|
||||
> The <xref:System.Windows.Forms.StatusStrip> and <xref:System.Windows.Forms.ToolStripStatusLabel> controls replace and add functionality to the <xref:System.Windows.Forms.StatusBar> and <xref:System.Windows.Forms.StatusBarPanel> controls; however, the <xref:System.Windows.Forms.StatusBar> and <xref:System.Windows.Forms.StatusBarPanel> controls are retained for both backward compatibility and future use, if you choose.
|
||||
|
||||
To program the [StatusBar Control](statusbar-control-windows-forms.md) control to respond to user clicks, use a case statement within the <xref:System.Windows.Forms.StatusBar.PanelClick> event. The event contains an argument (the panel argument), which contains a reference to the clicked <xref:System.Windows.Forms.StatusBarPanel>. Using this reference, you can determine the index of the clicked panel, and program accordingly.
|
||||
|
||||
> [!NOTE]
|
||||
> Ensure that the <xref:System.Windows.Forms.StatusBar> control's <xref:System.Windows.Forms.StatusBar.ShowPanels%2A> property is set to `true`.
|
||||
|
||||
### To determine which panel was clicked
|
||||
|
||||
1. In the <xref:System.Windows.Forms.StatusBar.PanelClick> event handler, use a `Select Case` (in Visual Basic) or `switch case` (Visual C# or Visual C++) statement to determine which panel was clicked by examining the index of the clicked panel in the event arguments.
|
||||
|
||||
The following code example requires the presence, on the form, of a <xref:System.Windows.Forms.StatusBar> control, `StatusBar1`, and two <xref:System.Windows.Forms.StatusBarPanel> objects, `StatusBarPanel1` and `StatusBarPanel2`.
|
||||
|
||||
```vb
|
||||
Private Sub StatusBar1_PanelClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.StatusBarPanelClickEventArgs) Handles StatusBar1.PanelClick
|
||||
Select Case StatusBar1.Panels.IndexOf(e.StatusBarPanel)
|
||||
Case 0
|
||||
MessageBox.Show("You have clicked Panel One.")
|
||||
Case 1
|
||||
MessageBox.Show("You have clicked Panel Two.")
|
||||
End Select
|
||||
End Sub
|
||||
```
|
||||
|
||||
```csharp
|
||||
private void statusBar1_PanelClick(object sender,
|
||||
System.Windows.Forms.StatusBarPanelClickEventArgs e)
|
||||
{
|
||||
switch (statusBar1.Panels.IndexOf(e.StatusBarPanel))
|
||||
{
|
||||
case 0 :
|
||||
MessageBox.Show("You have clicked Panel One.");
|
||||
break;
|
||||
case 1 :
|
||||
MessageBox.Show("You have clicked Panel Two.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```cpp
|
||||
private:
|
||||
void statusBar1_PanelClick(System::Object ^ sender,
|
||||
System::Windows::Forms::StatusBarPanelClickEventArgs ^ e)
|
||||
{
|
||||
switch (statusBar1->Panels->IndexOf(e->StatusBarPanel))
|
||||
{
|
||||
case 0 :
|
||||
MessageBox::Show("You have clicked Panel One.");
|
||||
break;
|
||||
case 1 :
|
||||
MessageBox::Show("You have clicked Panel Two.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
(Visual C#, Visual C++) Place the following code in the form's constructor to register the event handler.
|
||||
|
||||
```csharp
|
||||
this.statusBar1.PanelClick += new
|
||||
System.Windows.Forms.StatusBarPanelClickEventHandler
|
||||
(this.statusBar1_PanelClick);
|
||||
```
|
||||
|
||||
```cpp
|
||||
this->statusBar1->PanelClick += gcnew
|
||||
System::Windows::Forms::StatusBarPanelClickEventHandler
|
||||
(this, &Form1::statusBar1_PanelClick);
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
- <xref:System.Windows.Forms.StatusBar>
|
||||
- <xref:System.Windows.Forms.ToolStripStatusLabel>
|
||||
- [How to: Set the Size of Status-Bar Panels](how-to-set-the-size-of-status-bar-panels.md)
|
||||
- [Walkthrough: Updating Status Bar Information at Run Time](walkthrough-updating-status-bar-information-at-run-time.md)
|
||||
- [StatusBar Control Overview](statusbar-control-overview-windows-forms.md)
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
---
|
||||
title: Develop a Composite Control
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "custom controls [Windows Forms], composite controls"
|
||||
- "composite controls [Windows Forms]"
|
||||
- "composite controls [Windows Forms], Windows Forms"
|
||||
- "controls [Windows Forms], composite"
|
||||
ms.assetid: d086f2a3-baa3-4e09-b40c-a5bb3cfc51a6
|
||||
---
|
||||
# Develop a composite Windows Forms control
|
||||
|
||||
You can develop a composite Windows Forms control by combining other Windows Forms controls. Composite controls that derive from <xref:System.Web.UI.UserControl> are called user controls. The base class, <xref:System.Windows.Forms.UserControl>, provides keyboard routing for the child controls, thus ensuring that child controls can receive focus. For an example of a user control, see the <xref:System.Windows.Forms.UserControl> sample in [How to: Apply Attributes in Windows Forms Controls](how-to-apply-attributes-in-windows-forms-controls.md).
|
||||
|
||||
The Windows Forms designer in Visual Studio provides rich design-time support for authoring user controls.
|
||||
|
||||
- [How to: Display a Control in the Choose Toolbox Items Dialog Box](how-to-display-a-control-in-the-choose-toolbox-items-dialog-box.md)
|
||||
|
||||
- [Walkthrough: Serializing Collections of Standard Types with the DesignerSerializationVisibilityAttribute](serializing-collections-designerserializationvisibilityattribute.md)
|
||||
|
||||
- [Walkthrough: Inheriting from a Windows Forms Control with Visual C#](walkthrough-inheriting-from-a-windows-forms-control-with-visual-csharp.md)
|
||||
|
||||
- [How to: Provide a Toolbox Bitmap for a Control](how-to-provide-a-toolbox-bitmap-for-a-control.md)
|
||||
|
||||
- [How to: Inherit from Existing Windows Forms Controls](how-to-inherit-from-existing-windows-forms-controls.md)
|
||||
|
||||
- [Walkthrough: Debugging Custom Windows Forms Controls at Design Time](walkthrough-debugging-custom-windows-forms-controls-at-design-time.md)
|
||||
|
||||
- [How to: Inherit from the Control Class](how-to-inherit-from-the-control-class.md)
|
||||
|
||||
- [How to: Test the Run-Time Behavior of a UserControl](how-to-test-the-run-time-behavior-of-a-usercontrol.md)
|
||||
|
||||
- [How to: Align a Control to the Edges of Forms at Design Time](how-to-align-a-control-to-the-edges-of-forms-at-design-time.md)
|
||||
|
||||
- [How to: Inherit from the UserControl Class](how-to-inherit-from-the-usercontrol-class.md)
|
||||
|
||||
- [How to: Author Controls for Windows Forms](how-to-author-controls-for-windows-forms.md)
|
||||
|
||||
- [How to: Author Composite Controls](how-to-author-composite-controls.md)
|
||||
|
||||
- [Walkthrough: Authoring a Composite Control with Visual C#](walkthrough-authoring-a-composite-control-with-visual-csharp.md)
|
||||
|
||||
- [Walkthrough: Creating a Windows Forms Control That Takes Advantage of Visual Studio Design-Time Features](creating-a-wf-control-design-time-features.md)
|
||||
|
||||
- [How to: Create a Windows Forms Control That Takes Advantage of Design-Time Features](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2013/307hck25(v=vs.120))
|
||||
|
||||
## See also
|
||||
|
||||
- [How to: Apply Attributes in Windows Forms Controls](how-to-apply-attributes-in-windows-forms-controls.md)
|
||||
- [Developing Custom Windows Forms Controls with the .NET Framework](developing-custom-windows-forms-controls.md)
|
||||
- [Varieties of Custom Controls](varieties-of-custom-controls.md)
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
---
|
||||
title: Develop Custom Controls
|
||||
description: Learn about Windows Form controls. Specifically, you'll learn to combine existing controls, extend existing controls, and author your own custom controls.
|
||||
ms.date: "03/30/2017"
|
||||
helpviewer_keywords:
|
||||
- "custom controls [Windows Forms], developing using code"
|
||||
- "Control class [Windows Forms], Windows Forms"
|
||||
ms.assetid: 236cebc0-bd71-4f18-9fd6-5d0e592375df
|
||||
---
|
||||
# Developing Custom Windows Forms Controls with the .NET Framework
|
||||
Windows Forms controls are reusable components that encapsulate user interface functionality and are used in client-side Windows-based applications. Not only does Windows Forms provide many ready-to-use controls, it also provides the infrastructure for developing your own controls. You can combine existing controls, extend existing controls, or author your own custom controls. This section provides background information and samples to help you develop Windows Forms controls.
|
||||
|
||||
## In This Section
|
||||
[Overview of Using Controls in Windows Forms](overview-of-using-controls-in-windows-forms.md)
|
||||
Highlights the essential elements of using controls in Windows Forms applications.
|
||||
|
||||
[Varieties of Custom Controls](varieties-of-custom-controls.md)
|
||||
Describes the different kinds of custom controls you can author with the <xref:System.Windows.Forms?displayProperty=nameWithType> namespace.
|
||||
|
||||
[Windows Forms Control Development Basics](windows-forms-control-development-basics.md)
|
||||
Discusses the first steps in developing a Windows Forms control.
|
||||
|
||||
[Properties in Windows Forms Controls](properties-in-windows-forms-controls.md)
|
||||
Shows how to add to properties to Windows Forms controls.
|
||||
|
||||
[Events in Windows Forms Controls](events-in-windows-forms-controls.md)
|
||||
Shows how to handle and define events in Windows Forms controls.
|
||||
|
||||
[Attributes in Windows Forms Controls](attributes-in-windows-forms-controls.md)
|
||||
Describes the attributes you can apply to properties or other members of your custom controls and components.
|
||||
|
||||
[Custom Control Painting and Rendering](custom-control-painting-and-rendering.md)
|
||||
Shows how to customize the appearance of your controls.
|
||||
|
||||
[Layout in Windows Forms Controls](layout-in-windows-forms-controls.md)
|
||||
Shows how to create sophisticated layouts for your controls and forms.
|
||||
|
||||
[Multithreading in Windows Forms Controls](multithreading-in-windows-forms-controls.md)
|
||||
Shows how to implement multithreaded controls.
|
||||
|
||||
## Reference
|
||||
<xref:System.Windows.Forms.Control?displayProperty=nameWithType>
|
||||
Describes this class and has links to all of its members.
|
||||
|
||||
<xref:System.Windows.Forms.UserControl?displayProperty=nameWithType>
|
||||
Describes this class and has links to all of its members.
|
||||
|
||||
## Related Sections
|
||||
[Design-Time Attributes for Components](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2013/tk67c2t8(v=vs.120))
|
||||
Lists metadata attributes to apply to components and controls so that they are displayed correctly at design time in visual designers.
|
||||
|
||||
[Extending Design-Time Support](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2013/37899azc(v=vs.120))
|
||||
Describes how to implement classes such as editors and designers that provide design-time support.
|
||||
|
||||
[How to: License Components and Controls](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2013/fe8b1eh9(v=vs.120))
|
||||
Describes how to implement licensing in your control or component.
|
||||
|
||||
Also see [Developing Windows Forms Controls at Design Time](developing-windows-forms-controls-at-design-time.md).
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user