Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何更改在代码中创建的所有按钮(而不是手动创建的按钮)的button.text?C#_C#_Forms_Visual Studio_Button - Fatal编程技术网

如何更改在代码中创建的所有按钮(而不是手动创建的按钮)的button.text?C#

如何更改在代码中创建的所有按钮(而不是手动创建的按钮)的button.text?C#,c#,forms,visual-studio,button,C#,Forms,Visual Studio,Button,我的目标是我想把一个单词的每个字母都命名成单独的按钮,我还想把按钮上的字母洗牌或重新排列。。所以基本上看起来是这样的 H p E L (每个字母代表一个按钮) 单击按钮时,这些按钮应重新排列(类似于洗牌按钮) 因为我想从零开始制作我自己版本的文本扭曲游戏。。所以我用一个循环来创建我的按钮以节省工作量 private void addButtonLetters() { string testabc = "abc"; shuffled = new stri

我的目标是我想把一个单词的每个字母都命名成单独的按钮,我还想把按钮上的字母洗牌或重新排列。。所以基本上看起来是这样的

H p E L (每个字母代表一个按钮)

单击按钮时,这些按钮应重新排列(类似于洗牌按钮)

因为我想从零开始制作我自己版本的文本扭曲游戏。。所以我用一个循环来创建我的按钮以节省工作量

private void addButtonLetters()
    {
         string testabc = "abc";
         shuffled = new string(testabc.OrderBy(r => RNDM.Next(i, 4)).ToArray());

        for (int i = 0; i < 3; i++)
        {

            newButton = new Button();
            newButton.Size = new Size(75, 41);
            newButton.Location = new Point(11 + 80 * i, 66);
            newButton.Text = shuffled[i].ToString();
            newButton.Click += buttonClicked; //click handler
            letters[i] = newButton;
            this.Controls.Add(letters[i]);


        }
    }
private void addButtonLetters()
{
字符串testabc=“abc”;
shuffled=newstring(testabc.OrderBy(r=>RNDM.Next(i,4)).ToArray();
对于(int i=0;i<3;i++)
{
newButton=新按钮();
纽扣尺寸=新尺寸(75,41);
新按钮位置=新点(11+80*i,66);
newButton.Text=shuffled[i].ToString();
newButton.Click+=buttonClicked;//单击处理程序
字母[i]=纽布顿;
此.Controls.Add(字母[i]);
}
}
从这一点上,我想洗牌的字母,每个按钮,当我试图点击一个按钮,这是假定要重新安排字母。。。。 我试着做的是我只调用'addButtonLetter();'事件,因为我假设它只会重新创建一组新的重新排列的按钮,但它没有,也没有。。所以我就试着去做

string testabc = "abc";
shuffled = new string(testabc.OrderBy(r => RNDM.Next(i, 4)).ToArray());

    for (int i = 0; i < 3; i++)
    {
       newButton.Text = shuffled[i].ToString();
    }
string testabc=“abc”;
shuffled=newstring(testabc.OrderBy(r=>RNDM.Next(i,4)).ToArray();
对于(int i=0;i<3;i++)
{
newButton.Text=shuffled[i].ToString();
}
它只会改变最后一个按钮的字母,但我想改变所有三个!我甚至发现,循环是无用的,因为当我删除它,它仍然只是改变了最后一个按钮

我需要关于如何改变所有按钮文本的建议,因为我无法手动逐个重命名

我是否需要运行我创建的所有按钮,然后从那里开始操作?如果是,怎么做?任何建议都会有帮助。我尽量把我的问题弄清楚,并尽可能多地提供信息。我希望我提供的所有信息都是清楚的,对那些想要帮助的人有帮助。。。如果不是问我任何我能澄清的问题!提前谢谢你

注意:这是我的测试代码,所以我不会弄乱我的原始代码。。。我原来的一个将包含更多按钮,我正在尝试婴儿步骤第一…

正如你所说的“我正在尝试从头开始制作我自己版本的文本扭曲游戏”,然后你可以随意移动字母,希望这能帮助你:-

List<string> source =new List<string>();
    public Form1()
    {
        InitializeComponent();
        source.Add("a");
        source.Add("b");
        source.Add("c");
        source.Add("d");
        source.Add("e");
        SetButtonText(source);
    }

    private void SetButtonText(List<string> source)
    {
        button1.Text = source[0];
        button2.Text = source[1];
        button3.Text = source[2];
        button4.Text = source[3];
        button5.Text = source[4];
    }

    private void button1_Click(object sender, EventArgs e)
    {
        var rnd = new Random();
        var newsource = source.OrderBy(item => rnd.Next());
        SetButtonText(newsource.ToList());

    }
List source=new List();
公共表格1()
{
初始化组件();
来源。添加(“a”);
来源。添加(“b”);
来源。添加(“c”);
来源。添加(“d”);
来源。添加(“e”);
SetButtonText(源代码);
}
私有void SetButtonText(列表源)
{
button1.Text=源[0];
button2.Text=源[1];
button3.Text=源[2];
按钮4.Text=源[3];
button5.Text=源[4];
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
var rnd=新随机数();
var newsource=source.OrderBy(item=>rnd.Next());
SetButtonText(newsource.ToList());
}
如果要更改按钮位置,请尝试以下操作:-

    private void GetAllControl(Control c, List<Control> list)
    {
        foreach (Control control in c.Controls)
        {
            list.Add(control);

            if (control.GetType() == typeof(Panel))
                GetAllControl(control, list);
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        List<Rectangle> btnLocation = new List<Rectangle>();
        List<Control> btnList = new List<Control>();
        GetAllControl(this, btnList);
        foreach (Control control in btnList)
        {
            if (control.Text != "Shuffle")
            {
                btnLocation.Add(control.Bounds);
            }
        }
        var rnd = new Random();
        var newsource = btnLocation.OrderBy(item => rnd.Next()).ToList();
        int i = 0;
        foreach (Control control in btnList)
        {
            if (control.Text != "Shuffle")
            {
                control.Bounds = newsource[i];
            }
            i++;
        }
    }
private void GetAllControl(控件c,列表)
{
foreach(c.Controls中的控件)
{
列表。添加(控制);
if(control.GetType()==typeof(Panel))
GetAllControl(控件,列表);
}
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
List btnLocation=新列表();
List btnList=新列表();
GetAllControl(此,btnList);
foreach(btnList中的控件)
{
if(control.Text!=“洗牌”)
{
btnLocation.Add(control.Bounds);
}
}
var rnd=新随机数();
var newsource=btnLocation.OrderBy(item=>rnd.Next()).ToList();
int i=0;
foreach(btnList中的控件)
{
if(control.Text!=“洗牌”)
{
control.Bounds=newsource[i];
}
i++;
}
}

我已经测试了这两个功能,它正在工作;)

将创建的按钮放入列表中,然后可以根据需要对其进行修改。您需要使用
字母
数组访问按钮并设置其文本。类似于
字母[i].Text=suffled[i].ToString()的内容。我假设
字母
是一个按钮数组。