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)
{
}
}
}
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)
{
}
}
}
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)
{
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With