Reputation: 5
C# windows form How to save text from textboxes into list when list is in a different class. I have a form1 with a number of textboxes. I want the input to be saved in a list that is in a different class. I ahve the code in form1
public void button1_Click(object sender, EventArgs e)
{
minLista.add(textBox1.Text);
Form2 Form1 = new Form2();
this.Hide();
Form1.Show();
}
in the other class I have
List <string> minLista = new List<string>();
what am I doing wrong?
Upvotes: 0
Views: 3184
Reputation:
Make minlista public
public List<string> minLista = new List<string>();
Then use
public void button1_Click(object sender, EventArgs e)
{
Classname a = new Classname();
a.minLista.add(textBox1.Text);
Form2 Form1 = new Form2();
this.Hide();
Form1.Show();
}
Where is Classname
is the name of the class where you declared minLista
.
Upvotes: 1
Reputation: 2910
Assuming minList is a public property in Form2.cs you should do something like
public void button1_Click(object sender, EventArgs e)
{
ThirdClass newthirdClass = new ThirdClass();
newthirdClass.MinLista = new List<string>();
newthirdClass.MinLista.Add(textBox1.Text);
Form2 myForm2 = new Form2(newthirdClass);
this.Hide();
myForm2 .Show();
}
Keep in mind that the convention is for properties (like I propose you for minLista) to start with a capital letter (i.e. MinLista)
EDIT
Since you need it to be in a third class I would do this
public class ThirdClass
{
public List<string> MinLista {get; set;}
}
public Form Form2
{
private List<string> minLista;
public Form2(List<string> mlist)
{
minLista = mlist;
}
}
This way you inject the object you created (which has the reference to the list you want) to Form2.
Upvotes: 2