我正在开发一个游戏库应用程序,它将用户的游戏集合添加到列表视图中,其中包含多个子项。我有一个子项,它是一个复选框,可让您检查游戏适用于哪个平台或系统。我更改了列表视图的选定索引的代码,当您在列表视图中选择项目时,它会显示游戏标题的保存信息。但是,我无法弄清楚如何从选中的列表框中保存信息,并让它显示用户在选择指定游戏时之前选中的平台。我将所有内容都安排在班级中,我的代码如下。我试过f.platformCheckBox.CheckedItems.ToString() = f.Game[f.gameList.SelectedItems[0].Index].platform;
在我的IndexChanged.cs
类,但它会抛出一个错误,提示“赋值的左侧必须是变量、属性或索引器”。所以我无法做到这一点。下面是我的代码,如果有人对如何保存选中的列表框的内容有任何建议,我将不胜感激。注意:IndexChanged 类位于代码的底部,我想我可能会显示我的所有代码,以防万一我在代码的其他地方遗漏了一些重要的东西。
namespace GameLibrary
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public List<Library> Game = new List<Library>();
public Library lib = new Library();
public void saveBttn_Click(object sender, EventArgs e)
{
Save save = new Save();
save.saveChanges(this);
}
private void button1_Click(object sender, EventArgs e)
{
Add add = new Add();
add.addGame(this);
}
private void deleteBtnClick_Click(object sender, EventArgs e)
{
Delete delete = new Delete();
delete.deleteGame(this);
}
private void gameList_SelectedIndexChanged(object sender, EventArgs e)
{
IndexChanged Index = new IndexChanged();
Index.selectedIndexChanged(this);
}
private void searchBttn_Click_1(object sender, EventArgs e)
{
Search search = new Search();
search.searchLibrary(this);
}
}
public class Library
{
public string gametitle
{
get;
set;
}
public string developer
{
get;
set;
}
public string publisher
{
get;
set;
}
public string platform
{
get;
set;
}
}
class Save
{
public void saveChanges(Form1 f)
{
if (f.gameList.SelectedItems.Count == 1)
{
f.Game[f.gameList.SelectedItems[0].Index].gametitle = f.titleText.Text;
f.Game[f.gameList.SelectedItems[0].Index].developer = f.developerText.Text;
f.Game[f.gameList.SelectedItems[0].Index].publisher = f.publisherText.Text;
f.Game[f.gameList.SelectedItems[0].Index].platform = f.platformCheckBox.CheckedItems.ToString();
}
}
}
class Add
{
public void addGame(Form1 f)
{
f.lib.gametitle = f.titleText.Text;
f.lib.developer = f.developerText.Text;
f.lib.publisher = f.publisherText.Text;
f.lib.platform = f.platformCheckBox.CheckedItems.ToString();
f.Game.Add(f.lib);
f.gameList.Items.Add(f.lib.gametitle);
f.titleText.Clear();
f.developerText.Clear();
f.publisherText.Clear();
foreach (int i in f.platformCheckBox.CheckedIndices)
{
f.platformCheckBox.SetItemCheckState(i, CheckState.Unchecked);
}
}
}
class Search
{
public void searchLibrary(Form1 f)
{
foreach(ListViewItem item in f.gameList.Items)
{
if(item.Text.StartsWith(f.lib.gametitle))
{
item.Selected = true;
item.BackColor = SystemColors.Highlight;
item.ForeColor = SystemColors.HighlightText;
}
else
{
item.Selected = false;
item.BackColor = SystemColors.Window;
item.ForeColor = SystemColors.WindowText;
MessageBox.Show("Game title not found in library");
}
}
if(f.gameList.SelectedItems.Count == 1)
{
f.gameList.Focus();
f.titleText.Text = f.Game[f.gameList.SelectedItems[0].Index].gametitle;
f.developerText.Text = f.Game[f.gameList.SelectedItems[0].Index].developer;
f.publisherText.Text = f.Game[f.gameList.SelectedItems[0].Index].publisher;
f.gameList.SelectedItems[0].Text = f.titleText.Text;
}
}
}
class IndexChanged
{
public void selectedIndexChanged(Form1 f)
{
if(f.gameList.SelectedItems.Count == 1)
{
f.titleText.Text = f.Game[f.gameList.SelectedItems[0].Index].gametitle;
f.developerText.Text = f.Game[f.gameList.SelectedItems[0].Index].developer;
f.publisherText.Text = f.Game[f.gameList.SelectedItems[0].Index].publisher;
f.platformCheckBox.CheckedItems.ToString() = f.Game[f.gameList.SelectedItems[0].Index].platform; //This is where it throws the error
}
}
}
}