【问题标题】:How can I access control created within if-statement from outside if-statement in C# WinForms?如何从 C# WinForms 中的外部 if 语句访问在 if 语句中创建的控件?
【发布时间】:2014-06-23 18:01:25
【问题描述】:

我正在将音乐播放列表从磁盘加载到 C# ListView 控件。我使用ListViewGroups来分隔专辑,播放列表中可以有多个专辑。

播放列表以以下文本格式保存:(我知道这不是最好的方法,但适用于本示例)

|album|name of album
track 1 fsdfsfasf.mp3
track 2 fdsgfgfdhhh.mp3
track 3 gfdgsdgsdfgs.mp3

当我将播放列表加载到 ListView 时,我会测试字符串是否为“|album|”从行的开头找到,并将该行用于组标题文本。下面的代码示例:

using (StreamReader reader = File.OpenText("playlist.txt"))
{
    while (reader.Peek() >= 0)
    {
        result = reader.ReadLine();

        if (result.Substring(0, 7) == "|album|")
        {
            ListViewGroup group = new ListViewGroup();
            group.Header = result.Substring(7);
            lstPlaylist.Groups.Add(group); // lstPlaylist is existing ListView control for playlist
        }

        else
        {
            ListViewItem item = new ListViewItem(result, 0, group);
            lstPlaylist.Items.Add(item);
        }
    }
}

如果“|专辑|”找到字符串,然后我创建新的 ListViewGroup。但是该组在 else 语句中是不可访问的(我无法将项目分配给组),因为它超出了范围。如何在 if 语句中创建新的 ListViewGroup 并在该 if 语句之外使用它?

【问题讨论】:

  • 在外面声明变量。
  • 或者使用一个字段,如果你打算在其他方法中使用它。

标签: c# winforms scope listviewgroup


【解决方案1】:

您需要在 if 语句之外声明变量,以便它在 else 子句中可用。您还需要处理在专辑之前找到曲目的情况,除非您已经验证了源文件。

using (StreamReader reader = File.OpenText("playlist.txt"))
        {
            ListViewGroup group = null;
            while (reader.Peek() >= 0)
            {
                result = reader.ReadLine();
                if (result.Substring(0, 7) == "|album|")
                {
                    group = new ListViewGroup();
                    group.Header = result.Substring(7);
                    lstPlaylist.Groups.Add(group); // lstPlaylist is existing ListView control for playlist
                }

                else
                {
                    if (group != null)
                    {
                        ListViewItem item = new ListViewItem(result, 0, group);
                        lstPlaylist.Items.Add(item);
                    } 
                    else
                    {
                        // you are trying to add a track before any group has been created.
                        // handle this error condition
                    }
                }
            }
        }

【讨论】:

  • 我认为这里有一个错误,因为我得到的只是一组,名称为“default”。但修复很简单,我声明“ListViewGroup group = null;”在 while 语句之前(不在其中,因此没有多次声明),并且有效。谢谢你让我走上正轨!
【解决方案2】:

你必须首先在 if 语句之外声明变量,然后在 if 语句中给它任何值。如果你想在 if 和 else 中使用相同的值,或者在外面。

基本上发生的情况是,如果您转到代码的 else 部分,则永远不会生成该变量,因为它是在 if 部分中创建和初始化的。

祝你好运!

【讨论】:

    【解决方案3】:

    看看你的逻辑,无论哪种情况,你都需要初始化ListViewGroup。如果您找到“|专辑|”这个词然后您还分配一个属性值。因此,一个简单的解决方法是将变量向上移动以增加其范围:

    ListViewGroup group = new ListViewGroup();//move to here
    if (result.Substring(0, 7) == "|album|")
            {
    
                group.Header = result.Substring(7);
                lstPlaylist.Groups.Add(group); // lstPlaylist is existing ListView control for playlist
            }
    
            else
            {
                ListViewItem item = new ListViewItem(result, 0, group);//now group is initialized here as well
                lstPlaylist.Items.Add(item);
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-27
      • 2019-06-02
      • 1970-01-01
      • 2012-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多