The ListBox component programming in WinForm

Source: Internet
Author: User
Tags count

The listbox component is a commonly used component in the design of a program, in Visual C # and Visual Basic. NET program, you must import the namespace System.Windows.Forms in the. NET FrameWork SDK in your program because this component is defined in the System.Windows.Forms namespace. In the ASP.net Web page, the ListBox component appears as a server-side component, and the server-side component is where the components exist on the server side. This article is about how to use and manipulate the ListBox component in the ASP.net Web page.

A How to define a ListBox component in the ASP.net page:

The syntax for creating a ListBox component in the ASP.net page is as follows:

<asp:ListBox Id = "MyListBox" runat = "server" >
<asp:ListItem Value = "1" >第一个条目</asp:ListItem >
<asp:ListItem Value = "2" >第二个条目</asp:ListItem >
注释:这里还可以加入类似上面的若干条目
.....
</asp:ListBox >

Executing the above statement in a Web page can produce a name called "MyListBox" that contains several purposes listbox components.

Two Commonly used properties in the ListBox component:

We use the following table to illustrate some of the commonly used properties of the ListBox component:

selectionmode selection of entries in the component Choice of type is: multiple selection, radio. Single,multiple
This component shows the total number of rows
selected Detection entries are very selected
The returned type is ListItem, gets the selected entry in the component
Total number of entries in the component
index value of the selected entry in the component

Three Use an example to grasp the specific use of the ListBox component in the ASP.net page:

When you describe the use of the ListBox component in ASP.net, the programming language used in the program is Visual C #.

(1). How to add a new entry in the ListBox component:

The following statement adds an entry named "Sample" to the ListBox component named Lstitem:

Lstitem. Items. ADD (New ListItem ("Sample"))

(2). How to delete the specified entry in the ListBox component:

The following statement is the deletion of the selected entry in the ListBox component named Lstitem:

Lstitem. Items. Remove (Lstitem. SelectedItem)

(3). How to move a pointer to an entry in a component:

There are four main ways to move an entry: to the first entry, to the end entry, to the next, and to the previous one. In the program design, mainly through the operation of the components of the count and SelectedIndex properties to achieve the above four ways. The following are the program code that implements these four ways:

//按钮"至首条"事件处理程序
if ( sender == First )
{
if ( lstItem . Items . Count > 0 )
{
lstItem . SelectedIndex = 0 ;
}
}
//按钮"至尾条"事件处理程序
if ( sender == Last )
{
if ( lstItem . Items . Count > 0 )
{
lstItem . SelectedIndex = lstItem . Items . Count - 1 ;
}
}
//按钮"上一条"事件处理程序
if ( sender == Prev )
{
if ( lstItem . SelectedIndex > 0 )
{
lstItem . SelectedIndex = lstItem . SelectedIndex - 1 ;
}
}
//按钮"下一条"事件处理程序
if ( sender == Next )
{
if ( lstItem . SelectedIndex < lstItem . Items . Count - 1 )
{
lstItem . SelectedIndex = lstItem . SelectedIndex + 1 ;
}
}

Contact Us

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

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

A Free Trial That Lets You Build Big!

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

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

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