C# 具有传递给其他ListsBox的对象的Listbox
///团队挑选应用程序帮助Dota 2草稿/// 表格上的对象:C# 具有传递给其他ListsBox的对象的Listbox,c#,class,validation,parameter-passing,listboxitems,C#,Class,Validation,Parameter Passing,Listboxitems,///团队挑选应用程序帮助Dota 2草稿/// 表格上的对象: heroListBox 团队1列表框 team2ListBox AddTeam1按钮 AddTeam2按钮 放置在每个团队列表框下方的标签 我有一门课叫英雄。英雄的每个实例都有布尔值和一个字符串作为其名称。布尔值跟踪该英雄能力的属性,例如,如果他有远程攻击 在我的主窗体上,有一个列表框,其中包含所有可用的英雄。选择一个英雄,然后按下两个按钮中的一个:将对象发送到团队1的列表框或团队2的列表框的按钮。每个团队的列表框下方都有标签,用
public class Hero {
public string Name;
public bool IsInitiator;
public bool IsTank;
public bool IsNuker;
public bool IsCarry;
public bool IsPusher;
public bool IsRanged;
public bool IsGreedy;
public bool IsAOE;
public bool IsDisabler;
public bool IsRat;
public Hero(string tempName){
Name = tempName;
IsInitiator = false;
IsTank = false;
IsNuker = false;
IsCarry = false;
IsPusher = false;
IsRanged = false;
IsGreedy = false;
IsAOE = false;
IsDisabler = false;
IsRat = false;
}
public override string ToString()
{
return Name;
}
}
public partial class MainForm : Form
{
public int rcounter;
public int dcounter;
List<Hero> rHeroes = new List<Hero>();
List<Hero> dHeroes = new List<Hero>();
public MainForm()
{
InitializeComponent();
//添加到团队1。。。又名辐射。更新那些标签文本
void addTeam1ButtonClick(object sender, EventArgs e)
{
rcounter++;
rHeroes.Add(heroList.SelectedItem as Hero);
team1List.Items.Add(heroList.SelectedItem);
heroList.Items.Remove(heroList.SelectedItem);
for (int i = 0; i == rcounter; i++)
{
int rangedCounts = 0;
if (rHeroes[i].IsTank == true){
rangedCounts++;
}
radiantRangedLabel.Text = rangedCounts.ToString();
}
}
}
您正在为每个英雄的团队计数设置标签,因此它将始终显示1或0,具体取决于您循环中最后选中的英雄 将变量rangedCounts移到循环外部
void addTeam1ButtonClick(object sender, EventArgs e)
{
rcounter++;
rHeroes.Add(heroList.SelectedItem as Hero);
team1List.Items.Add(heroList.SelectedItem);
heroList.Items.Remove(heroList.SelectedItem);
for (int i = 0; i == rcounter; i++)
{
int rangedCounts = 0;
if (rHeroes[i].IsTank == true){
rangedCounts++;
}
radiantRangedLabel.Text = rangedCounts.ToString();
}
}
}