- 我用C#做了一个音乐播放器,唯一的问题是当你右键点击曲目时,播放列表的上下文菜单条。当它不在播放列表中时,它不会被选中。**注意:
1.当用户右键单击音乐时,项目将自动添加。**
- 我的解决方案:
1.尝试更改渲染模式,但没有更改任何内容
1.试图强制检查复选框使用的方法,但仍然不起作用,所以它去下一个方法,我尝试
1.尝试仅使用正常代码行强制检查
1.尝试更改检查项目的方法
代码如下:
public static void PlaylistsOpening(object sender, CancelEventArgs e, object instance, AudioFileInfo music)
{
if (instance is Form1 form1)
{
if (form1.cmsPlaylists.InvokeRequired)
{
form1.cmsPlaylists.Invoke(new MethodInvoker(delegate { form1.cmsPlaylists.Items.Clear(); }));
}
else
{
form1.cmsPlaylists.Items.Clear();
}
Debug.WriteLine(sender.GetType().Name);
IEnumerable<Playlist> playlists = Playlist.GetAllPlaylists();
Debug.WriteLine(playlists.Count());
foreach (Playlist playlist in playlists.ToList())
{
ToolStripMenuItem item = new ToolStripMenuItem
{
Tag = playlist,
Text = playlist.Name
};
if (Playlist.GetPlaylistsWithAudioFile(music).Contains(playlist))
{
Debug.WriteLine("Contains");
item.Checked = true;
}
item.Click += delegate
{
Debug.WriteLine("Clicked");
var nItem = form1.cmsPlaylists.GetItemsFromTag(playlist)[0];
if (nItem.CheckState == CheckState.Checked)
{
Debug.WriteLine("Checked");
music.RemoveFromPlaylist((Playlist)nItem.Tag);
nItem.CheckState = CheckState.Unchecked;
}
else
{
Debug.WriteLine("Unchecked");
music.AddToPlaylist((Playlist)nItem.Tag);
nItem.CheckState = CheckState.Checked;
}
};
music.Volume = form1.SoundVolume;
form1.cmsPlaylists.Items.Add(item);
}
if (form1.cmsPlaylists.InvokeRequired)
{
ToolStripMenuItem item = new ToolStripMenuItem()
{
Text = "Create a new playlist..."
};
item.Click += delegate
{
using (DialogPlaylist dialogPlaylist = DialogPlaylist.CreatePlaylist())
{
if (dialogPlaylist.ShowDialog() == DialogResult.OK)
{
form1.cmsPlaylists.Invoke(new MethodInvoker(delegate
{
ToolStripMenuItem nItem = new ToolStripMenuItem
{
Text = dialogPlaylist.Tag.Name,
Tag = dialogPlaylist.Tag,
CheckOnClick = true,
Checked = true
};
nItem.Click += delegate
{
if (nItem.Checked)
{
music.RemoveFromPlaylist((Playlist)nItem.Tag);
}
else
{
music.AddToPlaylist((Playlist)nItem.Tag);
}
};
form1.cmsPlaylists.Items.Add(nItem);
}));
}
}
};
}
else
{
void AddPlaylist(object sender1, EventArgs e1)
{
using (DialogPlaylist dialogPlaylist = DialogPlaylist.CreatePlaylist())
{
if (dialogPlaylist.ShowDialog() == DialogResult.OK)
{
ToolStripMenuItem nItem = new ToolStripMenuItem()
{
Text = dialogPlaylist.Tag.Name,
Tag = dialogPlaylist.Tag,
CheckOnClick = true,
Checked = true
};
nItem.Click += delegate
{
if (nItem.Checked)
{
music.RemoveFromPlaylist((Playlist)nItem.Tag);
}
else
{
music.AddToPlaylist((Playlist)nItem.Tag);
}
};
form1.cmsPlaylists.Items.Add(nItem);
}
}
}
ToolStripMenuItem item = new ToolStripMenuItem()
{
Text = "Create a new playlist..."
};
item.Click += (s, e2) => AddPlaylist(s, e2);
form1.cmsPlaylists.Items.Add(item);
}
}
else
{
return;
}
}```
And here's how to use that method: ```
private void PlaylistsContextMenuStrip_Opening(object sender, System.ComponentModel.CancelEventArgs e)
{
Debug.WriteLine("Opening...");
MusicPlayer.Events.PlaylistsOpening(sender, e, this, currentFile);
}```
1条答案
按热度按时间cu6pst1q1#
据我所知,每次右键单击
music
项目并显示上下文菜单时,您都会重新填充上下文菜单。由于无论如何都要创建新的ToolStripMenuItem
示例,因此请考虑在创建时为music
项目提供添加/删除操作所需的所有信息。一种有效的方法是创建自定义TSMI类:使click事件为
static
意味着主窗体只订阅它一次。上下文菜单示例代码
上下文菜单准确地描述了包含右键单击项目的播放列表,并且添加/删除行为按预期工作。