c# - Change a buttons action by two other buttons. C#
问题描述
I got three buttons. And have two List strings with random lines. I want to be able to push the two first buttons to change the "action" on the third one. If I click on the first button the third one only uses "HEB" and if I click the second one, the third one uses "HEC"
When pressing button1, I want button3 to do this:
HEB f1 = new HEB();
textBox1.Text = f1.RandomString();
When pressing button2, I want button3 to do this:
HEC f2 = new HEC();
textBox1.Text = f2.RandomString();
What is the best way to solve this?
EDIT: HEB and HEC are random text that is displayed in textbox1 when clicked on button3.
FULL CODE:
namespace Questions
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
HEB f1 = new HEB();
textBox1.Text = f1.RandomString();
}
private void button2_Click(object sender, EventArgs e)
{
HEC f2 = new HEC();
textBox1.Text = f2.RandomString();
}
private void button3_Click(object sender, EventArgs e)
{
}
}
}
public class HEB
{
List<string> list = new List<string>
{
"This is test number 1",
"This is test number 2",
"This is test number 3"
};
public string RandomString()
{
Random r = new Random();
int index = r.Next(list.Count);
string randomString = list[index];
return randomString;
}
}
public class HEC
{
List<string> list = new List<string>
{
"This will show later number 4",
"This will show later number 5",
"This will show later number 6",
};
public string RandomString()
{
Random r = new Random();
int index = r.Next(list.Count);
string randomString = list[index];
return randomString;
}
}
解决方案
您可以拥有一个包含当前“消息”的列表,您可以使用前两个按钮将其更改为预定义值,然后在按下第三个按钮时随机选择一条当前消息。
就像是:
public partial class Form1 : Form
{
private List<string> messages;
private string RandomString()
{
Random random = new Random();
return messages[random.Next(messages.Count)];
}
//To use it just do:
//(if it is a button it should have a method that it triggers on use)
private void HEB()
{
//same for HEC, but other strings (test 3 and 4)
messages = new List<string>(){"test 1", "test 2"};
}
private void TheButton()
{
textBox1.text = RandomString();
}
}
这应该可行,但不要害怕要求更多澄清:)
编辑:更新的有效代码(只需添加变量并将当前按钮中的代码替换为我的代码)。
namespace Test_btns
{
public partial class Form1 : Form
{
private List<string> messages;
Random random = new Random();
private string RandomString()
{
return messages[random.Next(messages.Count)];
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
messages = new List<string>() { "test 1", "test 2" };
}
private void button2_Click(object sender, EventArgs e)
{
messages = new List<string>() { "test 3", "test 4" };
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = RandomString();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
//not really needed for this, but the method exists.
}
}
}
推荐阅读
- sql - PostgreSQL函数返回表
- c++ - C++ How to delete a specific row or column in a dynamically allocated 2d array?
- javascript - How to export an object from a separate file to both Node and Javascript?
- angular - 如何在
零件? - typescript - 如何使用 Firebase 按子键值排序?
- jquery - 根据选择的付款方式显示一些 html
- python - 如何从python中的行列表中选择特定元素
- asp.net-mvc - ASP.NET Core FromForm 和 FromBody 相同的动作
- tensorflow - Tensorflow 中的 LSTM:到 Keras 还是不到 Keras
- python - 如何从表`tr`和`td`中提取数据?