none
Passing Combobox Selected Values Of Form2 to Form1 (Called Form)

    Question

  • hi to all.

    i have problem with Passing Values in WinForm Application.

    Case Study:

    i have 2 win forms  Form -Sales , Form-Customers.

    In Form-Sales:

    i have a Radio Button  NewCustomers--> when the status Changed -> im calling Form-Customers.

    i declared a string varible     : public string combovalue;

    Code :  rbNewCustomer_CheckedChanged(obj s,EventArgs e)

                 {Customers fcust=new Customers(this);

                   fcust.showDialog();

                   txtCustId.Text=combovalue;

                 }

    after calling Form-Customers->  i will Add new Customers -> Form-Customers Adds List of Customers -> i will select the Newly added Customer from the ComboBox (cmbCustId).

    After that i will Click SendData Button -> it should Reach the Form-Sales -> should Catch on to a Text Box on Form-Sales.

    In Form-Customers:

    i have Constructors on this Form:

    public Sales callingForm;

    public Customers(){ IntializeComponent();} and

    public Customers(Sales sal)
            {
                InitializeComponent();
                this.callingForm = sal;
                con.ConnectionString = Connections.Connect;  // Database Connection string
            }

    i have one Button(above Said button) on this Form:

    when Button SendData Clicked :

    public void SendData_Cliked(obj sender, EventArgs e)

    {

     callingForm.combovalue=cmbCustid. SelectedItem.Text; // cmCustid is the Combobox on Form-Customers which will have all Customers

    }

    Problem:

    Everything Working Fine ...But  The Form-Customer Form Calls Twice. I don't know how?

    i tried with only One - Constructor() [dfault]  and    Second - Constructor(parm) [either one]

    even it calls Twice.

    help me.

    Thank u.

    • Changed type vmdhartec Wednesday, October 10, 2012 6:12 AM To Know ALL
    Friday, October 05, 2012 4:18 PM

Answers

  • Hi vmdhartec,

    If you want to pass combobox selected value from Form2 to Form1, you can try the sample below.

     public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            public string lbText
            {
                set { this.label1.Text = value; }
                get { return this.label1.Text; }
            }
            private void button1_Click(object sender, EventArgs e)
            {
                Form2 fm = new Form2();
                fm.Show(this);
            }
        }
     public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
                comboBox1.Items.Add("test");           
            }
            private void button1_Click(object sender, EventArgs e)
            {
                Form1 fm2 = (Form1)this.Owner;
                fm2.lbText = this.comboBox1.Text;
                this.Close();
            }
        }

    Good day.

    Bob Shen [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, October 11, 2012 10:03 AM

All replies

  • This is because radioButton`s event CheckedChanged is rised twice:

    1.st time the code goes of the previous checked radioButton

    2nd time the code goes for the newly selected radioButton.

    ---

    So you have to do some limitations the code will only fire for the 2nd time (for the new selected radioButton).

    You can use some boolean flag to sort this out, like:

        public partial class Form1 : Form
        {
            bool bFlag;
            public Form1()
            {
                InitializeComponent();
                foreach (var c in this.Controls.OfType<RadioButton>())
                    c.CheckedChanged += (s, a) => c_CheckedChanged(s, a); //subscribe to all radioButtons
            }
    
            private void c_CheckedChanged(object sender, EventArgs e)
            {
                if (!bFlag)
                {
                    bFlag = true;
                    string name = ((RadioButton)sender).Name;
                    //execute your code in here
                }
                else
                    bFlag = false;
            }
        }


    Mitja

    Friday, October 05, 2012 4:30 PM
  • BTW, please chnage this thread to a Question, now its set as Discussion!

    thx in advance.


    Mitja

    Friday, October 05, 2012 4:31 PM
  • Thanks Mitja,

    but it still calling twice. i dont know how. i debugged with stepin mode.

    i have

    FormA has RadioButton --> OnChecked event i called Form B --> From Form B return the ComboBox Value to Form A- Property/String value. then Closes the FormB ,  Form A still Opend.

    here im not Calling Form A from FormB.  Then  where is Twice Calling Of RB status changed  (if i call Form A from  Form B , it loads).

    i just Disabled the rbcontrol of Form A  as Disable.

    in RbCheckedChanged()  Code

    Customers cust=new Customers(this);//  calling Parametrized Constructor Calling    . 

    cust.showDialog(); //  here it calls once again

    i think this position will be the problem.

    is there any way to call the Parametrized Constructor without calling Default Constructor , when we call the form dynamically with new instance of the Form.

    i still working on  what u said..Even Though trying the other way also.

    Thanks.

    Wednesday, October 10, 2012 7:31 AM
  • Hi vmdhartec,

    If you want to pass combobox selected value from Form2 to Form1, you can try the sample below.

     public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            public string lbText
            {
                set { this.label1.Text = value; }
                get { return this.label1.Text; }
            }
            private void button1_Click(object sender, EventArgs e)
            {
                Form2 fm = new Form2();
                fm.Show(this);
            }
        }
     public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
                comboBox1.Items.Add("test");           
            }
            private void button1_Click(object sender, EventArgs e)
            {
                Form1 fm2 = (Form1)this.Owner;
                fm2.lbText = this.comboBox1.Text;
                this.Close();
            }
        }

    Good day.

    Bob Shen [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, October 11, 2012 10:03 AM
  • BTW why you are using 2 places Initializecomponent?

    Also Debug using F11 to know from where the second call to constructor is going.


    Mark it as helpful if so! Thanks

    Thursday, October 11, 2012 10:57 AM