Reputation: 25
i want to call a windows form from my main class which contains a combobox. i want to call it with a string[] or List, and the items of this string[]/List shall be the items of the combobox.
it should work like this:
string[] data;
Auswahl auswahl = new Auswahl();
auswahl.ShowDialog();
but with giving other the string[]
i want to give other my string[] as a parameter. Is there a possible way to do that? I tried to find solutions on google. My problem is that i cant access the string[] from the Form class, eventhough its declared as public. If i try to do so, i get null.
Anyone knows what im doing wrong?
Upvotes: 0
Views: 228
Reputation: 6562
As CCrew stated. Here is an example.
Form
public partial class Auswahl : Form
{
public Auswahl(string[] array)
{
InitializeComponent();
comboBox1.DataSource = array;
}
}
Calling it from another class
string[] array = { "Hello", "World" };
new Auswahl(array).ShowDialog();
Upvotes: 1
Reputation: 33
Pass the string[] into your Auswahl class as a parameter for the constructor. Check out this MDSN article for more information: http://msdn.microsoft.com/en-us/library/ace5hbzh.aspx
A constructor is basically a function on your form that's called to generate anything the form will need to perform it's duties. Classes have default constructors (which I assume you're currently calling) that take no actions. You can set them up as you would any other function and populate your combo box with the list or array of strings you pass in.
Upvotes: 1