Click here to Skip to main content
15,908,673 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a main form which has buttons n when i click on button one form 1 and similar to it form 2 when 2 button is clicked
So I was able to pass value from form 1 to form 2 but when form 1 is closed values come back to 0

What I have tried:

Public delegate void abc(list<string > p1)

Form 2 f =new form 2();
Abc del =new Abc(f. Fpass) ;
Del(lst) ;

In Form 2

Public void Fpass( list<string > p2) {pvalue. Add(p2. ToArray() ) ;
}
Posted
Updated 21-Feb-17 21:55pm
Comments
Graeme_Grant 22-Feb-17 3:05am    
What are you trying to do?
Member 12972565 22-Feb-17 3:28am    
Trying to pass the list values from form 1 to form 2, since I have many forms- constructor approach won't work. so m trying to use delegate.
Graeme_Grant 22-Feb-17 3:34am    
why not? a quick google search show that as a popular solution.
Member 12972565 22-Feb-17 3:48am    
I tried it... Unfortunately I couldn't find any so i posted here
Graeme_Grant 22-Feb-17 3:51am    
Seriously??? Google Search: winform passing variables between forms[^]

1 solution

Above is a Google Search link on how to pass variables in the Form's constructor...

Alternatively, if this approach is not suitable, ie: values need to be passed after initialization, then the following example will do the trick:

Second Form (receiver):
C#
using System.Windows.Forms;

namespace WFPassVariablesBetweenForms
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public void SetValue(string text)
        {
            textBox1.Text = text;
        }
    }
}


Main Form (Sender):
C#
using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace WFPassVariablesBetweenForms
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            var xxx = new Form2();
            xxx.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var xxx = Application.OpenForms.Cast<Form>().Where(x => x.Name == "Form2").FirstOrDefault();
            ((Form2)xxx).SetValue(textBox1.Text);
        }
    }
}
The danger with this solution isthat it is tightly bound. Can be refactored for loose dependency but that is not the purpose of this example.
 
Share this answer
 
Comments
BillWoodruff 22-Feb-17 5:11am    
When a query is this confused, I don't see how "exotic" code like this:

var xxx = Application.OpenForms.Cast().Where(x => x.Name == "Form2").FirstOrDefault();
((Form2)possibly ).SetValue(textBox1.Text);

Can help the OP.
Graeme_Grant 22-Feb-17 5:15am    
"exotic"? a simple Linq query... Nothing exotic there... Variable name xxx could have been named better but pushed it out quickly.

Create a project with two forms, Form1 with a textbox and a button, form2 with a text box, paste the code in and run ... data is passed from one form to the other and the text result is displayed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900