ToolStripMenuItem.Checked属性未设置C# WinForms

xt0899hw  于 2023-04-07  发布在  C#
关注(0)|答案(1)|浏览(153)
  • 我用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);
        }```
cu6pst1q

cu6pst1q1#

据我所知,每次右键单击music项目并显示上下文菜单时,您都会重新填充上下文菜单。由于无论如何都要创建新的ToolStripMenuItem示例,因此请考虑在创建时为music项目提供添加/删除操作所需的所有信息。一种有效的方法是创建自定义TSMI类:

class PlaylistToolStripItem : ToolStripMenuItem
{
    public PlaylistToolStripItem(Playlist playlist, Music music)
    {
        Playlist = playlist;
        Music = music;
        Text = playlist.Name;
        Checked = playlist.Contains(music);
        base.Click += Click;            
    }
    public Playlist Playlist { get; }
    public Music Music { get; }
    public static new event EventHandler Click;
}

使click事件为static意味着主窗体只订阅它一次。

PlaylistToolStripItem.Click += (sender, e) =>
{
    if (sender is PlaylistToolStripItem tsmiPlus)
    {
        Debug.WriteLine($"Clicked Playlist: {tsmiPlus.Playlist.Name} Music: {tsmiPlus.Music}");
        if(tsmiPlus.Checked) tsmiPlus.Playlist.Remove(tsmiPlus.Music);
        else tsmiPlus.Playlist.Add(tsmiPlus.Music);
    }
};

上下文菜单示例代码

上下文菜单准确地描述了包含右键单击项目的播放列表,并且添加/删除行为按预期工作。

dataGridViewMusic.CellMouseDown += (sender, e) =>
{
    if (e.Button.Equals(MouseButtons.Right) && !e.RowIndex.Equals(-1))
    {
        var menu = new ContextMenuStrip();
        var music = _allMusic[e.RowIndex];
        dataGridViewMusic.ClearSelection();
        dataGridViewMusic.Rows[e.RowIndex].Selected = true;
        foreach (Playlist playlist in _allPlaylists)
        {
            menu.Items.Add(new PlaylistToolStripItem(playlist, music));
        }
        menu.Show(new Point(MousePosition.X + 30, MousePosition.Y - 30));
    }
};

相关问题