- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
Lesson 1: Creating and Configuring List-Display Controls in .NET
Lesson 1: Creating and Configuring List-Display Controls QR Code JIS X 0510 Generator In .NET Framework Using Barcode generator for ASP.NET Control to generate, create QR Code ISO/IEC18004 image in ASP.NET applications. www.OnBarcode.comPaint Barcode In VS .NET Using Barcode generation for ASP.NET Control to generate, create barcode image in ASP.NET applications. www.OnBarcode.com' VB ListBox1.Items.Insert(2, "This item will be third") // C# listBox1.Items.Insert(2, "This item will be third"); QR Code Printer In C# Using Barcode printer for .NET Control to generate, create Quick Response Code image in .NET applications. www.OnBarcode.comQR Code Creator In .NET Framework Using Barcode creator for VS .NET Control to generate, create QR Code image in VS .NET applications. www.OnBarcode.comYou can use the Items.Remove method to remove an item from the list. This method requires a reference to the object that you want to remove from the items collection. Note that if your control contains a collection of objects that are not strings, you will need to pass a reference to the object itself to remove it, not just to the string represen tation that appears in the control. The following example demonstrates the Items.Remove method. QR Code 2d Barcode Generator In Visual Basic .NET Using Barcode creation for VS .NET Control to generate, create QR Code image in Visual Studio .NET applications. www.OnBarcode.comEncoding Code-39 In Visual Studio .NET Using Barcode generator for ASP.NET Control to generate, create Code 39 Full ASCII image in ASP.NET applications. www.OnBarcode.com' VB ListBox1.Items.Remove("This string will be removed") // C# listbox1.Items.Remove("This string will be removed"); Printing UCC-128 In .NET Using Barcode maker for ASP.NET Control to generate, create EAN 128 image in ASP.NET applications. www.OnBarcode.comPaint UPC-A Supplement 2 In .NET Using Barcode generator for ASP.NET Control to generate, create Universal Product Code version A image in ASP.NET applications. www.OnBarcode.comIf you do not know the actual item that you want to remove at run time but have the index of the item you want to remove, you can use the Items.RemoveAt method. This method removes the item at a given index and adjusts the indexes of the other items accordingly. The Items.RemoveAt method is demonstrated in the following code example: Bar Code Drawer In .NET Using Barcode creator for ASP.NET Control to generate, create barcode image in ASP.NET applications. www.OnBarcode.comEncode Bar Code In Visual Studio .NET Using Barcode drawer for ASP.NET Control to generate, create bar code image in ASP.NET applications. www.OnBarcode.com' VB ' Removes the third item in the list ListBox1.Items.RemoveAt(2) // C# // Removes the third item in the list listBox1.Items.RemoveAt(2); EAN-13 Supplement 5 Creation In .NET Using Barcode generation for ASP.NET Control to generate, create GS1 - 13 image in ASP.NET applications. www.OnBarcode.comMake MSI Plessey In Visual Studio .NET Using Barcode maker for ASP.NET Control to generate, create MSI Plessey image in ASP.NET applications. www.OnBarcode.comTo remove all items from a list-based control, you can use the Items.Clear method, as shown here: Making QR In Visual Studio .NET Using Barcode maker for Reporting Service Control to generate, create QR Code image in Reporting Service applications. www.OnBarcode.comCode 39 Extended Encoder In Visual Studio .NET Using Barcode creation for VS .NET Control to generate, create Code39 image in .NET framework applications. www.OnBarcode.com' VB ListBox1.Items.Clear() // C# listBox1.Items.Clear(); Create GS1 128 In Java Using Barcode creator for Java Control to generate, create EAN128 image in Java applications. www.OnBarcode.comPDF417 Printer In None Using Barcode generation for Online Control to generate, create PDF 417 image in Online applications. www.OnBarcode.comDetermining Where an Item Appears in a List
Printing PDF417 In None Using Barcode maker for Font Control to generate, create PDF-417 2d barcode image in Font applications. www.OnBarcode.comBar Code Scanner In VS .NET Using Barcode scanner for .NET framework Control to read, scan read, scan image in VS .NET applications. www.OnBarcode.comIf you want to determine where an item appears in a list programmatically, you can do so by using the Items.IndexOf method. This method takes the item you want to find as an argument and returns an integer that represents the index of that item. If the item QR Code Recognizer In Visual Studio .NET Using Barcode scanner for VS .NET Control to read, scan read, scan image in .NET applications. www.OnBarcode.comDraw Bar Code In Objective-C Using Barcode generator for iPhone Control to generate, create barcode image in iPhone applications. www.OnBarcode.com 3
Advanced Windows Forms Controls
is not found in the Items collection, the IndexOf method returns -1. An example of the IndexOf method is shown here: ' VB Dim anIndex As Integer
anIndex = ListBox1.IndexOf("A String") // C# int anIndex; anIndex = listBox1.IndexOf("A String"); You can also programmatically determine the index of an item that has been selected by the user by using the SelectedIndex property. The SelectedIndex property returns the item that has been selected in the user interface at run time. If more than one item has been selected, the SelectedIndex property can return any of the selected items. The SelectedIndex property is demonstrated here: ' VB Dim anIndex As Integer
anIndex = ListBox1.SelectedIndex
// C# int anIndex; anIndex = listBox1.SelectedIndex; In controls where the SelectionMode property is set to MultiSimple or MultiExtended, you can return all of the selected indexes by using the SelectedIndices property, as shown in the following example: ' VB For Each i As Integer In ListBox1.SelectedIndices
Console.WriteLine(ListBox1.Items(i).ToString) Next
// C# foreach (int i in listBox1.SelectedIndices) Console.WriteLine(listBox1.Items[i].ToString()); Binding List-Based Controls to Data Sources
You will frequently want to expose data to the user in list-based controls. You can bind ListBox controls and ComboBox controls (but not CheckedListBox controls) to a data source by using the DataSource, DisplayMember, and ValueMember properties to bind a list-based control to a column of data in a data table. Lesson 1: Creating and Configuring List-Display Controls
Add a data source to your project. Adding data sources to your project is covered in detail in 5, Configuring Connections and Connecting to Data. To bind a list-based control to a data source
1. In the Designer, select the list-based control that you want to bind to a data source. 2. In the Properties window, click the DataSource property to open the data source configuration interface, as shown in Figure 3-2. Set the DataSource property to a table contained in one of the data sources in your project. Figure 3-2 Setting the DataSource property
3. In the Properties window, click the DisplayMember property. Visual Studio dis plays the columns in the selected table. This is the column whose rows will be displayed in the control. 4. In the Properties window, click the ValueMember property. Choose a column name in the interface to bind the control to. This is the column whose members will provide the value that is returned from the selected index in the control. The DataSource property indicates the data source (usually a data table) that the data in the control is drawn from. The DisplayMember property represents the column of data in the data source that is displayed to the user in the control. The ValueMember property allows you to designate an additional column of values to be represented in the control. For example, you might set the DisplayMember property to the Products column to indicate a list of products to the user but set the ValueMember to a ProductsCode column that returned a numeric code for each product. In this instance, when ever an item was selected, the SelectedItem property would return the item displayed
|
|