簡體   English   中英

c# - 在新線程中打開表單並傳遞參數

[英]c# - open form in new thread and pass parameter

想知道是否有人可以提供幫助。 我正在嘗試在新線程中打開一個新表單並傳遞一個參數,然后我可以使用它來填充該新表單中的文本框。 任何建議。 我已經搜索過,找不到任何東西。

謝謝

沒什么特別的,但這里有一個例子:

Form1 代碼 -

       public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            OpenNewForm();
        }

        private void OpenNewForm()
        {
            int _paramter1 = 5;
            string _parameter2 = "hello";
            Thread th = new Thread(() =>
            {
                Form2 form2 = new Form2(_paramter1, _parameter2);
                form2.ShowDialog();

            });
            th.Start();
        }

Form2 代碼 -

        int Parameter1;
        string Parameter2;

        public Form2(int parameter1, string parameter2)
        {
            InitializeComponent();
            Parameter1 = parameter1;
            Parameter2 = parameter2;
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            textBox1.Text = Parameter1.ToString();
            textBox2.Text = Parameter2;
        }

祝你好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM