新手求助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? 展开
{
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? 展开
展开全部
//添加两个变量
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;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询