문제

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