C#brojevi这个名字在当前上下文中不存在

C#brojevi这个名字在当前上下文中不存在,c#,C#,我想制作一个简单的程序(彩票号码生成器),它在特定范围内获取号码,并将其洗牌“n”次,每次洗牌后,它选择一个随机号码,并将其从给定范围的列表移动到一个新列表,并执行“n”次(直到它选择特定数量的号码,准确地说是7). 我已经找到了一种算法,可以做到这一点(扩展方法或洗牌通用列表)。但我不太喜欢编程,而且我在将结果(带有绘制数字的列表)显示到文本框或标签时遇到了问题,不过我已经让它与MessageBox一起工作了。但是使用TextBox/Label,我会得到错误“当前上下文中不存在名称*”。我在谷

我想制作一个简单的程序(彩票号码生成器),它在特定范围内获取号码,并将其洗牌“n”次,每次洗牌后,它选择一个随机号码,并将其从给定范围的列表移动到一个新列表,并执行“n”次(直到它选择特定数量的号码,准确地说是7). 我已经找到了一种算法,可以做到这一点(扩展方法或洗牌通用列表)。但我不太喜欢编程,而且我在将结果(带有绘制数字的列表)显示到文本框或标签时遇到了问题,不过我已经让它与MessageBox一起工作了。但是使用TextBox/Label,我会得到错误“当前上下文中不存在名称*”。我在谷歌上搜索了一个解决方案,但没有任何帮助

代码如下:

      private void button1_Click(object sender, EventArgs e)
      {
         List<int> numbers;
         numbers = Enumerable.Range(1, 39).ToList();
         numbers.Shuffle();
      }

      private void brojevi_TextChanged(object sender, EventArgs e)
      {          
      }
  }
}

/// <summary>
/// Class for shuffling lists
/// </summary>
/// <typeparam name="T">The type of list to shuffle</typeparam>
public static class ListShufflerExtensionMethods
{
    //for getting random values
    private static Random _rnd = new Random();

    /// <summary>
    /// Shuffles the contents of a list
    /// </summary>
    /// <typeparam name="T">The type of the list to sort</typeparam>
    /// <param name="listToShuffle">The list to shuffle</param>
    /// <param name="numberOfTimesToShuffle">How many times to shuffle the list,    by default this is 5 times</param>
    public static void Shuffle<T>(this List<T> listToShuffle, int numberOfTimesToShuffle = 7)
    {          
        //make a new list of the wanted type
        List<T> newList = new List<T>();

        //for each time we want to shuffle
        for (int i = 0; i < numberOfTimesToShuffle; i++)
        {
            //while there are still items in our list
            while (listToShuffle.Count >= 33)
            {
                //get a random number within the list
                int index = _rnd.Next(listToShuffle.Count);

                //add the item at that position to the new list
                newList.Add(listToShuffle[index]);

                //and remove it from the old list
                listToShuffle.RemoveAt(index);
            }

            //then copy all the items back in the old list again
            listToShuffle.AddRange(newList);

            //display contents of a list
            string line = string.Join(",", newList.ToArray());
            brojevi.Text = line;

            //and clear the new list
            //to make ready for next shuffling
            newList.Clear();
            break;
        }
    }
}
private void按钮1\u单击(对象发送者,事件参数e)
{
清单编号;
numbers=可枚举的.Range(1,39).ToList();
numbers.Shuffle();
}
私有void brojevi_TextChanged(对象发送方,事件参数e)
{          
}
}
}
/// 
///用于洗牌列表的类
/// 
///要洗牌的列表类型
公共静态类ListShuffleExtensionMethods
{
//用于获取随机值
私有静态随机_rnd=新随机();
/// 
///洗牌列表的内容
/// 
///要排序的列表的类型
///要洗牌的列表
///洗牌列表的次数,默认为5次
公共静态void Shuffle(此列表列出toshuffle,int numberOfTimesToShuffle=7)
{          
//制作所需类型的新列表
List newList=新列表();
//每次我们都想洗牌
for(int i=0;i=33)
{
//在列表中获取一个随机数
int index=\u rnd.Next(listtosuffle.Count);
//将该位置的项目添加到新列表中
Add(listtosuffle[index]);
//并将其从旧列表中删除
ListToSuffle.RemoveAt(索引);
}
//然后再次复制旧列表中的所有项目
ListToSuffle.AddRange(新列表);
//显示列表的内容
string line=string.Join(“,”,newList.ToArray());
brojevi.Text=行;
//并清除新列表
//为下一次洗牌做准备
newList.Clear();
打破
}
}
}

}ListShuffleExtensionMethods不知道您的文本框(brojevi),因为它超出了范围。您可以重新构造并使Shuffle返回一个值,然后在调用者范围内设置textbox文本的值。

问题是
brojevi
(或者
textbox
或者
Label
)没有在扩展方法的范围内定义,它是
控件,因此应该在
表单中定义它。因此,当您洗牌数字时,在执行
按钮1\u单击事件处理程序时,将其放入
文本框中

删除以下行:

    string line = string.Join(",", newList.ToArray());
    brojevi.Text = line;
编辑:

您可以像这样更改扩展方法,以返回绘制项目的字符串或绘制项目的列表。我们来看看这个列表,因为你可能想把这些数字用于其他事情。另外,我不认为洗牌7次有什么意义,因为你只能看到最后一次洗牌。所以我觉得一个就够了。检查代码:

public static List<T> Shuffle<T>(this List<T> listToShuffle)
        {
            //make a new list of the wanted type
            List<T> newList = new List<T>();


            //while there are still items in our list
            while (listToShuffle.Count >= 33)
            {
                //get a random number within the list
                int index = _rnd.Next(listToShuffle.Count);

                //add the item at that position to the new list
                newList.Add(listToShuffle[index]);

                //and remove it from the old list
                listToShuffle.RemoveAt(index);
            }

            //then copy all the items back in the old list again
            listToShuffle.AddRange(newList);

            return newList;
        }
publicstaticlist-Shuffle(此列表listToShuffle)
{
//制作所需类型的新列表
List newList=新列表();
//虽然我们的清单上还有一些项目
while(listtosuffle.Count>=33)
{
//在列表中获取一个随机数
int index=\u rnd.Next(listtosuffle.Count);
//将该位置的项目添加到新列表中
Add(listtosuffle[index]);
//并将其从旧列表中删除
ListToSuffle.RemoveAt(索引);
}
//然后再次复制旧列表中的所有项目
ListToSuffle.AddRange(新列表);
返回newList;
}
在button1\u Click1事件处理程序中,我们可以有:

List<int> numbers;
numbers = Enumerable.Range(1, 39).ToList();
List<int> drawnNumbers = numbers.Shuffle();
string line = string.Join(",", drawnNumbers.ToArray());
brojevi.Text = line;
列表编号;
numbers=可枚举的.Range(1,39).ToList();
List drawnNumbers=numbers.Shuffle();
string line=string.Join(“,”,drawnNumbers.ToArray());
brojevi.Text=行;

这可能是一个范围问题:。但是,很难说,因为您没有说错误在哪里。显示MessageBox的代码在哪里?是字面上的
名称*不存在
,还是
*
表示特定符号?比演示问题所需的代码更多。请将其编辑为再现问题所需的最小代码。此代码显示“数字.随机”的内容(随机排列后的39个数字),但我需要显示“新列表”的内容,因为它只包含7个抽取的数字。TextBox“brojevi”就像你写的,超出了洗牌列表的类的范围,所以我需要一种方法,要么将TextBox“brojevi”放入类的范围,要么将值从“newList”导出到TextBox“brojevi”。@JamesDawkins现在检查答案,我认为现在更清楚了。