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

Recommended Answers

All 6 Replies

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?

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.....

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

Hey People thanks for your help.
Thanks praveen.

Regards

Ajinkya

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;
            
}

}

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.