0

我正在制作一个 ATM 模拟器,我目前正在尝试在一个表单上添加一个按钮来创建另一个表单(ATM)。我已经能够做到这一点,但仅限于一个表单,因为当新表单出现时,我无法单击另一个具有“添加 atm”按钮的表单。我已经尝试过Form.ShowForm.ShowDialogApplications.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();
    }
}
4

1 回答 1

3

如果您不希望表单成为模式对话框,请使用表单的Show方法而不是方法。ShowDialog

您还需要确保从 UI 线程而不是后台线程创建和显示表单。根据您的代码,您根本不需要创建后台线程,只需直接从构造函数创建/显示表单。

于 2013-03-18T20:04:06.837 回答