Вопрос

Right I have got a list box which contains a list of tracks and when the track is pressed it moves to a second list box, what I now need to happen is have the first item that's in the second list box move automatic to a text box.

This is my current code for the first move

private void genreListBox_DoubleClick(object sender, EventArgs e)
{
   playlistListBox.Items.Add(genreListBox.SelectedItem);
}

I am thinking it should be something like this

presentlyPlayingTextBox.AppendText(playlistListBox.);

But I'm not sure how to add the first line without clicking it.

I have tried this but I get an error for the value.

presentlyPlayingTextBox.Text = playlistListBox.SelectedItem.Value;
Это было полезно?

Решение

Normally you would use something like the 'SelectedIndexChanged' or 'SelectedValueChanged' action of a listbox, otherwise events like DoubleClick will fail if the keyboard is used to change the selection.

Also that event should be triggered on the second listbox when it is changed by the first.

EDIT:

On a standard Listbox, there is no .SelectedItem.Value, only .SelectedItem. However as a listbox holds objects, not just text, you need to say you want the text value.

presentlyPlayingTextBox.Text = playlistListBox.SelectedItem.ToString();

Другие советы

I don't know if I fully understand your question, but if you're just attempting to move the first item from ListBox_2 to a textbox, would the following work?

presentlyPlayingTextBox.Text = playlistListBox.Items[0]?.Value; // C# >= 6
presentlyPLayingTextBox.Text = ((playlistListBox.Items == null) ? playlistListBox.Items[0].Value : "" ); // C# < 6

You could also create your own delegate/event that would fire when the user did some action.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top