我正在制作一个 ATM 模拟器,我目前正在尝试在一个表单上添加一个按钮来创建另一个表单(ATM)。我已经能够做到这一点,但仅限于一个表单,因为当新表单出现时,我无法单击另一个具有“添加 atm”按钮的表单。我已经尝试过Form.Show
,Form.ShowDialog
并Applications.Run()
使用 C#。
这是代码:
public class Program
{
private Account[] ac = new Account[3];
private ATM atm;
public Form1 form1;
/* This function initilises the 3 accounts
* and instanciates the ATM class passing a referance to the account information
*/
public Program()
{
ac[0] = new Account(300, 1111, 111111);
ac[1] = new Account(750, 2222, 222222);
ac[2] = new Account(3000, 3333, 333333);
Thread form1thread = new Thread(new ThreadStart(startform)); //Creates ATM Form
//Thread atm2 = new Thread(new ThreadStart(start));
form1thread.Start();
//atm2.Start();
}
static void Main(string[] args)
{
new Program();
}
public void startform()
{
form1 = new Form1(this);
form1.ShowDialog();
}
public void newatm()
{
atm = new ATM(ac);
atm.ShowDialog();
}
public void makethread()
{
Thread newatm = new Thread(new ThreadStart(startform));
newatm.Start();
}
}
在form1中:
public partial class Form1 : Form
{
Program program;
public Form1(Program program)
{
InitializeComponent();
this.program = program;
}
private void Form1_Load(object sender, EventArgs e)
{
this.ShowDialog();
}
private void button1_Click(object sender, EventArgs e)
{
program.newatm();
}
}