1.11M Members

How to pass value from one form to another

 
0
 

Hey Guys,


Currently, I am doing a project. I don't know how to pass a value from one windows form to another windows form using c#.net.

Suppose, I have two forms parent form and child form. Parent form consists of a

combo box & button and I want to pass selected item of combo box to my child form and that

item will be showing in a text box of child form. How can I do that?


Thanks and Regards

 
0
 

Here's My code :

using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
     
    namespace WindowsFormsApplication1
    {
    public partial class Form1 : Form
    {
    Form2 ob;
    public Form1()
    {
    InitializeComponent();
    }
     
    private void button1_Click(object sender, EventArgs e)
    {
    string s;
    s = comboBox.SelectedIndex;
    ob = new Form2(s);
    ob.Show();
    }
    }
    }
     
    for form2 :


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
     
    namespace WindowsFormsApplication1
    {
    public partial class Form2 : Form
    {
    string ss;
    public Form2(string s)
    {
    InitializeComponent();
    ss = s;
    comboBox.SelectedIndex = ss;
    }
    }
    }

I want to compare the value of selected item in combo box and display its data accordingly(on click of a button). However it gives me error "The name comboBox.selectedIndex doesn't exist in current context". What do i do now?

 
0
 

create a constructor in child form to accept data from parent form
example
in child form

public Form2(string s)
        {
            s1 = s;
            InitializeComponent();
        }

call this form from parent form like this

Form2 f = new Form2(comboBox1.Text);
            f.Show();

Check this and feel free to ask any query.....

 
0
 
s = comboBox.SelectedIndex;

this line return a integer value and you try to assign this value in string type variable.

and same in your child form trying to assign string variable into integer type

 
0
 

Hey People thanks for your help.
Thanks praveen.

Regards

Ajinkya

Question Answered as of 3 Years Ago by prvnkmr194 and __avd
 
0
 

Hi there, you CAN pass a parameter through to the form2, you just do it wrong.

check out the code i modified for you

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{

public partial class Form1 : Form
{

public Form1()
{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)
{

Form2 ob = new Form2(comboBox.SelectedItem.Text);


ob.ShowDialog();

}

}

}

for form2 :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{

public partial class Form2 : Form

{

private string m_SelectedItem;

public Form2(string s)
{

InitializeComponent();

m_SelectedItem = s;

}

private void Form2_Load(object sender, EventArgs e)
{

TextBox1.Text = m_SelectedItem;
            
}

}

}
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article