C# .NET - copy data from listbox at form one to the text box at another form

Asked By ram on 25-Nov-10 07:05 AM
i want to copy data from listbox at form one to the text box at another form..plz guide me....
Thangaraj replied to ram on 25-Nov-10 07:12 AM
listbox1_selectionchanged()
{
   form2.textbox1.text=listbox.selecteditem.Tostring();
}
Mitesh Darji replied to ram on 25-Nov-10 07:15 AM
you need to store that selected vlaue in to Session Variable so that you can get the vaue from another page,
and make sure that whenever you dont required that session remove that

If you want to select single record then follow below code:

Session("SeleVal") = ListBox1.SelectedValue

or If you want to multiple selection then follow below code:
Dim x As String = String.Empty  
Try 
   For Each drv As System.Data.DataRowView In Me.ListBox1.SelectedItems  
     x &= drv.Item(0).ToString & ","   
     
   Next 
Catch ex As IndexOutOfRangeException  
 
End Try 

Jatin Prajapati replied to ram on 25-Nov-10 07:42 AM
For that first you have to create a public property on Form2 which represent your textbox's Text property like this.

public string MyProperty
{
get{ return txtMyProperty.Text; }
set{ txtMyProperty.Text = value;}
}

Reason to do this is that you can't directly access the TextBox of Form2 on Form1 becuase it is defined as private.

Now on form1, in list's selected indexchanged you can set that property value of Form2 like this

private void listBox_SelectedIndexChanged(object sender, EventArgs e)
{
form2.MyProperty = listBox.SelectedText;
}
ram replied to Jatin Prajapati on 26-Nov-10 08:33 AM
thanx