How to send date from a listbox to textbox of another form in C#

Category: visual studio visualiaztion

Question

Preshan Pradeepa on Fri, 21 Nov 2014 11:13:07


I have a listbox with multiple items in a form.I need to select listbox item and click a button and then selected item should appear in anothe forms text box.how can i do this.

I use this code to put items to my form 1 list box after I click a button.

using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();


    }
    private void button1_Click(object sender, EventArgs e)
    {
        SqlConnection conn2 = new SqlConnection("Data Source(LocalDB)\\v11.0;AttachDbFilename=\"C:\\Users\\Preshan\\documents\\visual studio 2012\\Projects\\WindowsFormsApplication1\\WindowsFormsApplication1\\Database1.mdf\";Integrated Security=True");
        conn2.Open();


        ArrayList al = new ArrayList();
        SqlCommand commandtwo = new SqlCommand("SELECT name FROM [dbo].[Table2]", conn2);
        SqlDataReader dr2 = commandtwo.ExecuteReader();

        while (dr2.Read())
        {
            al.Add(dr2[0].ToString());
        } 
         try
        {
            listBox1.Items.Clear();
            listBox1.Items.AddRange(al.ToArray());
            conn2.Close();

        }
        catch (Exception) { }

     public void button2_Click(object sender, EventArgs e)
     {

        Form2 f2 = new Form2();
        f2.Show();

     }
    }
   }
  }

I need to select item in this listbox and click Update button in this form,it will open another form called Form2.I need to get selected item from previous form(Form1) and show it in a textbox in anothe form (Form2).

This is the codes of my second form(Form2)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{

    public Form2()
    {

        InitializeComponent();

    }
    private void Form2_Load(object sender, EventArgs e)
    {
     Form1 f1 = new Form1();

     textBox1.Text = f1.listBox1.SelectedItem.ToString();
    }

Replies

Doanh, Qh on Thu, 27 Nov 2014 10:12:33


 public static string ListBoxValue
       {
           get;
           set;
       }

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListBoxValue = listBox1.SelectedItem.ToString();           
        }  

 private void button4_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.Show();
        }     

 public Form2()
        {
            InitializeComponent();
            textBox1.Text = DirectoryAndFolder.ListBoxValue;
        }