//selects the movie option, this will populate the movie lists (titles only)
private void btnMovies_Click(object sender, EventArgs e)
{
int size = 0;
string titles = null;
byte[] dataSend = new byte[1024];
byte[] dataReceive = new byte[1024];
string[] split = null;
movieList.Items.Clear();
movietitles.Clear();
try
{
Array.Clear(dataSend, 0, dataSend.Length);
dataSend = Encoding.ASCII.GetBytes("movies");
socket.Send(dataSend);
size = socket.Receive(dataReceive);//receive movie titles
titles = Encoding.ASCII.GetString(dataReceive, 0, size);
char[] separators = { ';' };
split = titles.Split(separators);
for (int i =0;i<split.Count();i++)
{
movietitles.Add(split[i]);
}
foreach (string smovie in movietitles)
{
movieList.Items.Add(smovie);
}
}
catch (SocketException ex)
{
lblError.Text = "Unable to connect to server";
lblError.Text += ex;
}
pnlMovies.Visible = true;
}
所以,这里发生的事情是,我填充movieList(listbox)与movietitles列表中的电影标题。然而,每次我再次按下movies按钮,它都会再次填充,而不清除之前的一个,即使我清除了它。我打印出movietitles的.count,每次都增加一倍,所以肯定是问题。
我已经用.Clear()清除了它,所以不知道还能做什么。我也重置了所有的变量,但是没有任何效果。谢谢
我认为,如果你把ListBox绑定到一个数据源上可能会更好。 就像这样。
//populate your List with your movie titles
private List<String> movieTitles = new List<string>() { "movie1", "movie2", "movie3" };
//Then bind the datasource
movieList.ItemSource = movieTitles;