Hey guys,

I have two forms, form1 and form2.
My form1 has a combo box and a button.
Here's form1 code.

Form1

public partial class Form1 : Form
    {
        bool s;
        public Form1()
        {
            InitializeComponent();
            s1 = comboBox1.Text;

        }

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

        public string s1;

        private void button1_Click(object sender, EventArgs e)
        {
            s = true;
            Form2 oj = new Form2(s1);
            oj.Show();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

                
    }
    public struct forms1
    {
        public string s;
        
        public forms1(string s)
        {
            this.s = s;
        }
        public string text()
        {
            return "this was selected" + s;
        }
    }

Now the thing is, as you can see i have declared a structure, with the help of this structure i am trying to show, in second form, the value that was selected from combo box.

Here's code for my Form2


Form2

public partial class Form2 : Form
    {
        forms1 fr;
        string ss1;
        public Form2(string re)
        {
            InitializeComponent();
            ss1 = re;
        }

        private void Form2_Load(object sender, EventArgs e)
        {
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            fr = new forms1(ss1);
            MessageBox.Show(fr.text());
            
        }
    }

The problem here is as structure is declared outside the class in form1, i cannot declare variables for 'combobox.text' in it. And so now, when i click the button
in form2 it shows the text that was declared in struct but not the text that was selected in combo box. So is there any way to display the text of combo box.

NOTE : I know there's another way without using structure but i need to do it using structure.


Thanks and regards
ajinkya

In Form1,
1. Store the combobox's selected value in the instance of structure.
2. When the button is pressed, pass that struct object to Form2.

In Form2,
1. Receive the struct object.
2. When the button is pressed, show the stored text by calling its method.

hey thanks for your reply.

I tried doing it this way.

But neither its showing the selected text nor its
showing the index value of that text.
Here's my CODE. Please let me know if there's something wrong in it.

Form1:

public partial class Form1 : Form
    {
        
       
        public Form1()
        {
            InitializeComponent();
            
           // forms1 fr2 = new forms1(s1);
        }

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

        public string s1;
        public int s2;   

        private void button1_Click(object sender, EventArgs e)
        {
            forms1 fs = new forms1(s1,s2);
            Form2 oj = new Form2(fs);
            oj.Show();
            
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            s1 = comboBox1.Text;
            s2 = comboBox1.SelectedIndex;
        }
       
                
    }
    public struct forms1
    {
        public string s;
        public int s4;

        public forms1(string s, int s4)
        {
            this.s = s;
            this.s4 = s4;

        }
        public string text()
        {
            return "this was selected"  +  s + "This was number" + s4;
        }
    }

FORM2 :

public partial class Form2 : Form
    {
        forms1 fr;

        

        

        public Form2(forms1 ew)
        {
            InitializeComponent();
            fr = ew;
        }

        private void Form2_Load(object sender, EventArgs e)
        {
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
                fr = new forms1();

                MessageBox.Show(fr.text().ToString());
            
            
        }
    }

Sample code:

Form1

namespace Struct_to_Form
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2(new ValuType(comboBox1.SelectedItem.ToString()));
            frm2.Show();
        }
    }
}

public struct ValuType
{
    private string s;

    public ValuType(string s)
    {
        this.s = s;
    }
    public void show()
    {
        MessageBox.Show(s);
    }
}

Form2

namespace Struct_to_Form
{
    public partial class Form2 : Form
    {

        ValuType ObjValuType;

        public Form2(ValuType ObjValuType)
        {
            InitializeComponent();
            this.ObjValuType = ObjValuType;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ObjValuType.show();
        }
    }
}

I have an example code with a bit different approach:

//form1:
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            string[] array = new string[] { "A", "B", "C" };
            this.comboBox1.Items.AddRange(array);
            this.comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
        }

        private void comboBox1_SelectedIndexChanged(object obj, EventArgs e)
        {
            string item = comboBox1.GetItemText(comboBox1.SelectedItem);
            if (item != String.Empty)
            {
                Form2 f2 = new Form2(item);
                f2.StartPosition = FormStartPosition.CenterScreen;
                f2.Show(this);
            }
        }
    }

//form2:
    public partial class Form2 : Form
    {
        string item;
        public Form2(string msg)
        {
            InitializeComponent();
            item = msg;
            
            //example to put the string into some control:
            Label label = new Label();
            label.Location = new Point(20, 20);
            label.Text = msg;
            this.Controls.Add(label);
        }

        protected override void OnShown(EventArgs e)
        {
            MessageBox.Show("Value from form1 is:\n" + item);
        }
    }

Hope it helps,
Mitja

This article has been dead for over six months. Start a new discussion instead.