//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;
}
所以这里发生的事情是我在movietitles列表中填充movieList(列表框)中的电影标题。然而,每当我再次按下按钮电影时,它会再次填充它而不清除前一个,即使我清除了它。我为movietitles打印出了.count,每次都会增加一倍,所以它的定义就是问题。
我已经.Clear()它,所以我不知道我还能做些什么。我重置了所有变量,但没有任何作用。谢谢!
答案 0 :(得分:-1)
我认为如果将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;