新手求助C#button1中随机抽取的数字怎么在点击button2后继续使用?

privatevoidbutton1_Click(objectsender,EventArgse){stringallChar="1,2,3,4,5,6";string[... private void button1_Click(object sender, EventArgs e)
{
string allChar = "1,2,3,4,5,6";
string[] allname = allChar.Split(',');
Random rand = new Random();
int t = rand.Next(0, 6);
int i = rand.Next(0, 6);
string name = allname[t];
string name1 = allname[i];
textBox1.Text = name;
while (textBox1.Text == tx1.Text)
{
string[] allname2 = allChar.Split(',');
Random rand2 = new Random();
int a = rand.Next(0, 6);
int b = rand.Next(0, 6);
string name2 = allname[a];
string name3 = allname[b];
textBox1.Text = name2;
tx1.Text = name3;

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

tx1 = new TextBox();
tx1.Name = "tx1";
tx1.Location = new Point(92, 60);
this.Controls.Add(tx1);
tx1.ReadOnly = true;
string fontName = tx1.Font.Name;
tx1.Font = new Font(fontName, 18);
tx1.Text = name1;
while (textBox1.Text == tx1.Text)
{
tx1.Text = name3;
}
}
为什么被提示上下文中没有name1和name3?怎么在点击button后出现的新textbox显示button1抽取的name1?
展开
 我来答
freeeeeewind
推荐于2016-03-21 · TA获得超过1万个赞
知道大有可为答主
回答量:3227
采纳率:94%
帮助的人:1365万
展开全部
//添加两个变量
string name, name1, name2, name3;
private void button1_Click(object sender, EventArgs e)
        {
            string allChar = "1,2,3,4,5,6";         
            string[] allname = allChar.Split(',');
            Random rand = new Random();
            int t = rand.Next(0, 6);
            int i = rand.Next(0, 6);
            //修改这里
            //string name = allname[t];
            //string name1 = allname[i];
            name = allname[t];
            name1 = allname[i];
            textBox1.Text = name;
            while (textBox1.Text == tx1.Text)
            {
                string[] allname2 = allChar.Split(',');
                Random rand2 = new Random();
                int a = rand.Next(0, 6);
                int b = rand.Next(0, 6);
                //修改这里
                //string name2 = allname[a];
                //string name3 = allname[b];
                name2 = allname[a];
                name3 = allname[b];
              
                textBox1.Text = name2;
                tx1.Text = name3;

            }
    }
        private void button2_Click_1(object sender, EventArgs e)
        {
           
            tx1 = new TextBox();
            tx1.Name = "tx1";
            tx1.Location = new Point(92, 60);
            this.Controls.Add(tx1);
            tx1.ReadOnly = true;
            string fontName = tx1.Font.Name;
            tx1.Font = new Font(fontName, 18);
            tx1.Text = name1;
            while (textBox1.Text == tx1.Text)
            {
                tx1.Text = name3;
            }
      }
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式