Click to See Complete Forum and Search --> : How to move data from one list box to another


dejan1
May 2nd, 2008, 09:40 PM
I'm trying to move data already present in listBox1 in Form1 to listBox2 in Form2. I have a button called "send"

here's what I have so far

/* I have a Data button that puts data into listBox1 in Form1. then I want to click the Send button below and have that data transfer to listBox2 in Form2
*/
private void Send_Click(object sender, EventArgs e)
{

Form2 inst = new Form2();
inst.ShowDialog();
//i'm thinking i should have to include listBox2 here somehow....
}

Arjay
May 2nd, 2008, 10:05 PM
One simple way is to pass the window of form 1 to the constructor of form 2.

private void Send_Click(object sender, EventArgs e)
{
Form2 inst = new Form2( this );
inst.ShowDialog();
}

The constructor in form 2 would have a form1 field.

class Form2 : Form
{
public Form2( Form1 form1 )
{
// Initialization...
_form1 = form1;
}

}

Then on Form2:OnLoad( ) you would use the _form1 variable to populate the listbox.

The are other more elegant ways to do it, but this is quick.

dejan1
May 2nd, 2008, 10:15 PM
Umm...I'm not quite sure what you mean...I did this...


public partial class Form2 : Form
{
public Form2(Form1 form1)
{
_form1 = form1;
InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)
{

}
}
}

TheCPUWizard
May 2nd, 2008, 11:11 PM
Then


this.MyListBox.Items.AddRange(_form.MyListBot.Items.ToArray());


should do the trick.

dejan1
May 2nd, 2008, 11:28 PM
Then


this.MyListBox.Items.AddRange(_form.MyListBot.Items.ToArray());


should do the trick.

Yeah, but it tells me that it can't recognize _form1....

TheCPUWizard
May 2nd, 2008, 11:33 PM
The how did:


public Form2(Form1 form1)
{
_form1 = form1;
InitializeComponent();
}


Ever compile???? :confused: :confused:

dejan1
May 2nd, 2008, 11:38 PM
Ok, so basically like this?


public partial class Form2 : Form
{
public Form2(Form1 form1)
{
_form1 = form1;
InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)
{
this.MyListBox.Items.AddRange(_form.MyListBot.Items.ToArray());
}
}
}



it keeps saying that _form1 doesn't exist in current context

TheCPUWizard
May 2nd, 2008, 11:40 PM
Did you DECLARE it???

dejan1
May 2nd, 2008, 11:44 PM
lol. ummm that's the part i'm having difficulty with...what should i declare it as...it's just not clicking somehow (and also i'm a bit new to this)

private Form1 form1; ?

TheCPUWizard
May 2nd, 2008, 11:47 PM
private Form1 _form1; :rolleyes:

dejan1
May 2nd, 2008, 11:50 PM
lol....yeah i finally caught on..it's kinda late here...and then do the property right..

public Form1 _Form1
{
get {return form1}
set {form1 = value}
}

dejan1
May 3rd, 2008, 12:20 AM
Still can't get it to work...it says that it doesn't contain a definition for "ItemSelected". that is my listBox name in Form1

this.listBox2.Items.AddRange(_form1.ItemSelected.Items.ToArray());

i don't understand why it's not recognizing ItemSelected....

TheCPUWizard
May 3rd, 2008, 12:45 AM
Still can't get it to work...it says that it doesn't contain a definition for "ItemSelected". that is my listBox name in Form1

this.listBox2.Items.AddRange(_form1.ItemSelected.Items.ToArray());

i don't understand why it's not recognizing ItemSelected....

Possibly because there is no such property?????

Are you capable of reading the documentation??????

Arjay
May 3rd, 2008, 03:49 AM
private Form1 _form1; :rolleyes:
I don't think the : rolleyes: is going to compile unless you are running a different compiler than I am. ;)

dejan1
May 3rd, 2008, 06:44 AM
yeah, i got it working. thanks guys.