C#出现新表格时无法点击其他表格

时间:2013-03-18 20:03:07

标签: c# winforms forms

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

1 个答案:

答案 0 :(得分:3)

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

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