法甲

WinForm中的ListBox组件编程 2

2019-09-13 19:29:02来源:励志吧0次阅读

四. 本文中源程序代码(listbox.aspx)和执行的界面:

下图是执行了下列源程序代码(listbox.aspx)后,生成的界面:

screen.width-600)this.style.width=screen.width-600;" border=0>

listbox.aspx源程序代码,具体如下:

<% @ Page Language = "C#" %>

<html >

<head >

<script runat = "server" >

protected void Button_Click ( object sender , EventArgs e )

{

//按钮"增加条目"事件处理程序

if ( sender == Add )

{

if ( txtItem.Text != "" )

{

lstItem . Items . Add ( new ListItem ( txtItem . Text ) ) ;

}

}

//按钮"删除"事件处理程序

if ( sender == Del )

{

if ( lstItem . SelectedIndex > -1 )

{

lstItem . Items . Remove ( lstItem . SelectedItem ) ;

}

}

//按钮"向上移位"和"向下移位"事件处理程序

if ( ( sender == Up && lstItem . SelectedIndex > 0 ) || ( sender == Down && lstItem . SelectedIndex < lstItem . Items . Count - 1 ) )

{

int offset ;

if ( sender == Up )

{

offset = -1 ;

}

else

{

offset = 1 ;

}

ListItem lstTemp = new ListItem ( lstItem . SelectedItem . Text , lstItem . SelectedItem . Value ) ;

lstItem . Items [ lstItem.SelectedIndex ] .Text = lstItem . Items [ lstItem . SelectedIndex + offset ] . Text ;

lstItem . Items [ lstItem . SelectedIndex ] . Value = lstItem . Items [ lstItem . SelectedIndex + offset ] . Value ;

lstItem . Items [ lstItem . SelectedIndex + offset ] . Text = lstTemp . Text ;

lstItem . Items [ lstItem . SelectedIndex + offset ] . Value = lstTemp . Value ;

lstItem . SelectedIndex = lstItem . SelectedIndex + offset ;

}

//按钮"至首条"事件处理程序

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 ;

}

}

}

</script >

</head >

<body >

<form runat = "server" >

<table >

<tr > <td Colspan = 2 > <h1 > <font color = "red" > WinForm组件ListBox演示程序 </font > </h1 > </td> </tr >

<tr >

<td > 请输入要增加的条目名称:</td >

<td >

<asp:TextBox id = "txtItem" TextMode = "SingleLine" runat = "server"/>

<asp:Button id = Add Text = "增加条目" runat = "server" onclick = "Button_Click"/>

</td >

</tr >

<tr >

<td >ListBox: <br >

<asp:ListBox id = "lstItem" Width = 200 Height = 250 runat = "server" >

<asp:ListItem > 第一个条目 </asp:ListItem >

</asp:ListBox >

</td >

<td >

<asp:Button id = "Del" Text = "删除" runat = "server" onclick = "Button_Click" />

<asp:Button id = "Up" Text = "向上移位" runat = "server" onclick = "Button_Click" />

<asp:Button id = "Down" Text = "向下移位" runat = "server" onclick = "Button_Click" />

<asp:Button id = "First" Text = "至首条" runat = "server" onclick = "Button_Click" />

<asp:Button id="Prev" Text = "上一条" runat = "server" onclick = "Button_Click" />

<asp:Button id = "Next" Text = "下一条" runat = "server" onclick = "Button_Click" />

<asp:Button id = "Last" Text = "至尾条" runat = "server" onclick = "Button_Click" />

</td >

</tr >

</table >

</form >

<body >

</html > 查看本文来源

孩子积食吃什么药好
小孩晚上睡觉出汗是怎么回事
小孩经常便秘怎么办
宝宝发热39度怎么办
分享到: