我正在制作一个自动柜员机模拟器,目前我正在尝试在一个表单上使用一个按钮来创建另一个表单(自动柜员机)。我已经能够做到这一点,但只有一个表单,因为当新的表单出现时,我无法点击另一个表单,其中有“添加自动柜员机”按钮。我尝试过使用C#的Form.Show、Form.ShowDialog和Applications.Run()。
代码如下:
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();
}
}发布于 2013-03-19 04:04:07
如果不希望窗体成为模式对话框,请使用窗体的Show方法而不是ShowDialog方法。
您还需要确保从UI线程而不是后台线程创建和显示窗体。根据您的代码,您根本不需要创建后台线程,只需直接从构造函数创建/显示窗体即可。
https://stackoverflow.com/questions/15485849
复制相似问题