簡體   English   中英

以其他形式獲取組合框的價值C#

[英]Getting value of combobox in other form c#

我嘗試以另一種形式獲取ComboBox的值,但是我什么也沒有。

當我在Form1上使用MessageBox ,得到的值確實是以前的值。

當我在Form2上嘗試它時,我得到一個空白的MessageBox ,因此以這種方式,我將無法使用“ IF”語句來獲取所需的數據。

表格1:

namespace ScoreDuizenden
{
    public partial class AantalSpelers : Form
    {
        public AantalSpelers()
        {
            InitializeComponent();
        }

        public string si;
        public void AantalSpelers_Load(object sender, EventArgs e)
        {
            NaamSpelers ns = new NaamSpelers();
            cbAantalSpelers.Items.Add("2");
            cbAantalSpelers.Items.Add("3");
            cbAantalSpelers.Items.Add("4");

        }
        public void btnDoorgaan_Click(object sender, EventArgs e)
        {
            si = cbAantalSpelers.Text;
            NaamSpelers ns = new NaamSpelers();
            ns.Show();
            this.Hide();
        }
    }
}

表格2:

namespace ScoreDuizenden
{
    public partial class NaamSpelers : Form
    {
        public NaamSpelers()
        {
            InitializeComponent();
        }
        AantalSpelers asp = new AantalSpelers();

        private void NaamSpelers_Load(object sender, EventArgs e)
        {
            if (asp.cbAantalSpelers.Text == "2")
            {
                label3.Hide();
                label4.Hide();
                txtNaam3.Hide();
                txtNaam4.Hide();
                btnAsDoorgaan.Location = new Point(16, 62);
                this.Size = new Size(198, 130);
            }
            if (asp.cbAantalSpelers.Text == "3")
            {
                label4.Hide();
                txtNaam4.Hide();
                btnAsDoorgaan.Location = new Point(16, 88);
                this.Size = new Size(198, 157);
            }
        }

        private void NaamSpelers_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        private void btnAsDoorgaan_Click(object sender, EventArgs e)
        {
            MessageBox.Show(asp.si);
        }
    }
}

希望你能幫助我解決我做錯的事情。

請看下面的代碼

表格1:

namespace ScoreDuizenden
{
    public partial class AantalSpelers : Form
    {
        public AantalSpelers()
        {
            InitializeComponent();
        }

        public string si;
        public void AantalSpelers_Load(object sender, EventArgs e)
        {
            NaamSpelers ns = new NaamSpelers();
            cbAantalSpelers.Items.Add("2");
            cbAantalSpelers.Items.Add("3");
            cbAantalSpelers.Items.Add("4");

        }
        public void btnDoorgaan_Click(object sender, EventArgs e)
        {
            si = cbAantalSpelers.Text;
            NaamSpelers ns = new NaamSpelers(si); // passing value to form through constructor
            ns.Show();
            this.Hide();
        }
    }
}

表格2:

    namespace ScoreDuizenden
    {
        public partial class NaamSpelers : Form
        {
            private string cbAntalSpelers = string.Empty; // class level field to store combo value
            public NaamSpelers(string cboValue)  // combo value need to be passed to this form whenever a new instance is created
            {
                InitializeComponent();
                this.cbAntalSpelers = cboValue;
            }

            ....
        }
    }

暫無
暫無

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

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