Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have list of string values pass form one to another form the same list of string values. 
Posted
Comments
DamithSL 10-Jul-14 1:31am    
winform!
jeevakumar.T 10-Jul-14 1:35am    
yes windows form
jo.him1988 10-Jul-14 1:53am    
take one static property in program.cs or any class
public static List<string> myStringList { get; set; }
assign its value on form 1
and when you move to form 2 you can get your list of string



:)
[no name] 10-Jul-14 2:08am    
Use static object for this. And access previous page public static field

 
Share this answer
 
v2
As per your scenario i have created an example to get you understand how we can pass string collection from one form to another :-

Let say i have two forms as below :-
ContainForm : Holds data which needs to be send.
GettingForm : Form which gets data from ContainForm.

Now we can have property in GettingForm to set the stringcollection before opening the form from first form.

GettingForm.cs
--------------
C#
public partial class GettingForm : Form
    {
        public StringCollection DummyList { get; set; }

        public GettingForm()
        {
            InitializeComponent();
        }

        private void GettingForm_Load(object sender, System.EventArgs e)
        {
            foreach (string str in this.DummyList)
            {
                MessageBox.Show(str);
            }
        }
    }


ContainForm.cs
---------------
C#
public partial class ContainForm : Form
    {
        public StringCollection DummyList { get; set; }

        public ContainForm()
        {
            InitializeComponent();
        }

        private void ContainForm_Load(object sender, System.EventArgs e)
        {
            DummyList = new StringCollection();
            DummyList.Add("Item 1");
            DummyList.Add("Item 2");
            DummyList.Add("Item 3");
            DummyList.Add("Item 4");
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            GettingForm gtForm = new GettingForm(); // create instance of GettingForm
            gtForm.DummyList = this.DummyList; // Assign value to the string collection propery of GettingForm
            gtForm.Show(); // Display the form
        }
    }



Hope this will be of help to you.

There are also more alternatives you can get here[^].
 
Share this answer
 
v2
This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA
 
Share this answer
 
You can do it using constructor.
 
Share this answer
 
Comments
jeevakumar.T 10-Jul-14 1:34am    
i am not using constructor.i am using only public variable to pass another form but it didn't work.
DamithSL 10-Jul-14 1:38am    
update question with your code
jeevakumar.T 10-Jul-14 1:57am    
form1()
public List<string> myList = new List<string>();
public string txt;
private void button2_Click(object sender, EventArgs e)
{

try
{

foreach (DataRowView drv in listBox2.Items)
{
txt = (drv.Row[listBox2.DisplayMember].ToString());
// StringBuilder s = new StringBuilder(txt);

myList.Add(txt);
}
}

form2()

form1 sp = new form1();
public List<string> myList1 = new List<string>();
public string tx;


public void addtabpage()
{
tx = sp.myList.ToString();

// myList1.Add(sp.myList.ToString());
myList1.Add(tx);
TabControl tabControl = new TabControl();
tabControl.Dock = DockStyle.Fill;

foreach (string c in myList1)
{
TabPage tabPage = new TabPage();
tabPage.Text = c.ToString();

if (tabPage.Text == "DRILLING")
{
UserCtrlSample sam = new UserCtrlSample();
sam.Dock = DockStyle.Fill;
tabPage.Controls.Add(sam);
}
tabControl.Controls.Add(tabPage);
}

this.Controls.Add(tabControl);


}

}

i select multiple value in listbox but it pass only SYSTEM.COLLECTION.GENERIC.LIST"1[SYSTEM.STRING]
this my problem please help how can i pass the listbox values.

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