listbox的索引问题

1、UI

 

2、代码

 

 

[csharp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10.   
  11. namespace WindowsFormsApplication8  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.         public Form1()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.   
  20.          
  21.         /// <summary>  
  22.         /// 显示当前的listbox的索引值  
  23.         /// </summary>  
  24.         /// <param name="sender"></param>  
  25.         /// <param name="e"></param>  
  26.         private void button3_Click(object sender, EventArgs e)  
  27.         {  
  28.             MessageBox.Show(listBox1.SelectedIndex.ToString());  
  29.         }  
  30.   
  31.         /// <summary>  
  32.         /// 让蓝条向上,循环移动  
  33.         /// </summary>  
  34.         /// <param name="sender"></param>  
  35.         /// <param name="e"></param>  
  36.         private void buttonShang_Click(object sender, EventArgs e)  
  37.         {  
  38.             if(listBox1.SelectedIndex==0)  
  39.             {  
  40.                 listBox1.SelectedIndex = listBox1.Items.Count-1;  
  41.             }  
  42.             else  
  43.             {  
  44.                 listBox1.SelectedIndex--;  
  45.             }  
  46.         }  
  47.   
  48.         /// <summary>  
  49.         /// 有了这个默认值,就好像有了参考系一样。简化了很多代码  
  50.         /// </summary>  
  51.         /// <param name="sender"></param>  
  52.         /// <param name="e"></param>  
  53.         private void Form1_Load(object sender, EventArgs e)  
  54.         {  
  55.             //在窗体加载的时候就给Listbox一个默认值吧,这样可以简化代码  
  56.             listBox1.SelectedIndex = 0;  
  57.         }  
  58.   
  59.         /// <summary>  
  60.         /// 让蓝条向下循环移动  
  61.         /// </summary>  
  62.         /// <param name="sender"></param>  
  63.         /// <param name="e"></param>  
  64.         private void buttonXia_Click(object sender, EventArgs e)  
  65.         {  
  66.             if (listBox1.SelectedIndex == listBox1.Items.Count - 1)  
  67.             {  
  68.                 listBox1.SelectedIndex = 0;  
  69.             }  
  70.             else  
  71.             {  
  72.                 listBox1.SelectedIndex++;  
  73.             }  
  74.         }  
  75.     }  
  76. }  



 

 

3、效果图





posted on 2017-01-11 14:18  带刀侍卫++i  阅读(492)  评论(0编辑  收藏  举报

导航