在做某些需要点一下鼠标或者自动跳到列表下一个、上一个时使用,这样就可以不使用 for,foreach 来遍历寻找了
比如:
首先定义一个列表循环类,继承自 List ,这样就具有 List 所有的特性
代码:
public class ListLoop<T> : List<T>
{
public int CurrentID { get; private set; }
public int LastID
{
get
{
CurrentID--;
if (CurrentID < 0) CurrentID = this.Count - 1;
CurrentID %= this.Count;
return CurrentID;
}
}
public int NextID
{
get
{
CurrentID++;
if (CurrentID == this.Count) CurrentID = 0;
CurrentID %= this.Count;
return CurrentID;
}
}
public T CurrentValue
{
get { return this[CurrentID]; }
}
public T LastValue
{
get { return this[LastID]; }
}
public T NextValue
{
get { return this[NextID]; }
}
}
初始化这个类,并添加内容
//初始化
private ListLoop<int> mListLoop = new ListLoop<int>()
{
5,
6,
7,
8,
9,
10,
11,
12,
13,
14
};
调用:
//调用
//当前值
string currentValue = mListLoop.CurrentValue.ToString();
//上一个值
string lastValue = mListLoop.LastValue.ToString();
//下一个值
string nextValue = mListLoop.NextValue.ToString();
//当前ID
string mListCurrentID = "当前ID:" + mListLoop.CurrentID;
示例:下面做一个游戏选择器,类都用 string 代替,重要的是方法
首先新建一个类 GameChoose
public class GameChoose
{
//ID
public int ID { get; set; }
//名称
public string Name { get; set; }
//显示图片
public string ShowPic { get; set; }
//背景图
public string BackgroundImage { get; set; }
//简介
public string Introduction { get; set; }
//公告
public string Notice { get; set; }
//其它
public string Other { get; set; }
//特效
public string SpecialEffects { get; set; }
public GameChoose(int id, string name)
{
this.ID = id;
this.Name = name;
}
public GameChoose(int id, string name, string showPic, string backgroundImage)
{
this.ID = id;
this.Name = name;
this.ShowPic = showPic;
this.BackgroundImage = backgroundImage;
}
public GameChoose(int id, string name, string showPic, string backgroundImage, string introduction)
{
this.ID = id;
this.Name = name;
this.ShowPic = showPic;
this.BackgroundImage = backgroundImage;
this.Introduction = introduction;
}
public GameChoose(int id, string name, string showPic, string backgroundImage, string introduction, string notice)
{
this.ID = id;
this.Name = name;
this.ShowPic = showPic;
this.BackgroundImage = backgroundImage;
this.Introduction = introduction;
this.Notice = notice;
}
public GameChoose(int id, string name, string showPic, string backgroundImage, string introduction, string notice, string specialEffects)
{
this.ID = id;
this.Name = name;
this.ShowPic = showPic;
this.BackgroundImage = backgroundImage;
this.Introduction = introduction;
this.Notice = notice;
this.SpecialEffects = specialEffects;
}
}
初始化选择器的内容:
ListLoop<GameChoose> gameChooses = new ListLoop<GameChoose>()
{
new GameChoose(1,"游戏一"),
new GameChoose(2,"游戏二","显示二","背景二"),
new GameChoose(3,"游戏三", "显示三","背景三","简介三"),
new GameChoose(4,"游戏四", "显示四","背景四","简介四","公告四"),
new GameChoose(5,"游戏五", "显示五","背景五","简介五","公告五","特效五"),
};
调用:不使用 for,foreach 来遍历
int temp = gameChooses.NextID;
int id = gameChooses.CurrentValue.ID;
string name = gameChooses.CurrentValue.Name;
string showPic = gameChooses.CurrentValue.ShowPic;
string backgroundImage = gameChooses.CurrentValue.BackgroundImage;
string introduction = gameChooses.CurrentValue.Introduction;
string notice = gameChooses.CurrentValue.Notice;
string specialEffects = gameChooses.CurrentValue.SpecialEffects;