Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying data entered in one form to another form

I want another form to display the selected items from the listbox of one form to rich text box of another form that opens up on button click. I used the following snippet in form 1 to display the content in message box, but now i want it another form. Need help...

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

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

        private void button1_Click(object sender, EventArgs e)
        {



            StringBuilder message = new StringBuilder();

            foreach (object selectedItem in listBox1.SelectedItems)
            {
               message.Append(selectedItem.ToString() + Environment.NewLine);
            }
            MessageBox.Show("You selected: \n" +message.ToString());         


        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

    }
}
like image 820
ankita alung Avatar asked Jan 31 '26 00:01

ankita alung


2 Answers

I used the following code to achieve what I wanted. And its doing just fine! :)

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

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

        private void button1_Click(object sender, EventArgs e)
        {



            StringBuilder message = new StringBuilder();


            foreach (object selectedItem in listBox1.SelectedItems)
            {
               message.Append(selectedItem.ToString() + Environment.NewLine);
            }
            MessageBox.Show("Your Selected Cities :\n" + message.ToString() + "\n");

            Form2 childForm = new Form2();
            childForm.Controls["richTextBox1"].Text = message.ToString();
            childForm.Show();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

    }
}
like image 143
ankita alung Avatar answered Feb 01 '26 12:02

ankita alung


From the First come just call the Second form like this

Form2 ob = new Form2(message);
ob.show();

This is the 2 nd form to set the Messge

  public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
            public Form2(String message)
            {
                InitializeComponent();
                richTextBox1.Text = message;
            }

            private void Form2_Load(object sender, EventArgs e)
            {

            }
        }
like image 36
Akshay Joy Avatar answered Feb 01 '26 12:02

Akshay Joy