Okay,
I assume you must be using VS2008, since it is less forgiving than VS2005.
You need to change it a little, and add some error checking.
public partial class Form1 : Form
{
private Form2 form2;
public Form1()
{
InitializeComponent();
}
private void listBox1_Click(object sender, EventArgs e)
{
int i = form2.listBox1.SelectedIndex;
if (i > -1)
{
string item = form2.listBox1.Items[i].ToString();
int y = checkedListBox1.Items.IndexOf(item);
checkedListBox1.SetItemCheckState(y, CheckState.Checked);
}
}
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (e.NewValue == CheckState.Checked)
{
string item = checkedListBox1.Items[e.Index].ToString();
if (form2.listBox1.Items.IndexOf(item) == -1)
form2.listBox1.Items.Add(item);
}
}
private void Form1_Load(object sender, EventArgs e)
{
if (form2 == null)
{
form2 = new Form2();
form2.listBox1.Click += new EventHandler(listBox1_Click);
form2.Show();
}
}
}
As I stated earlier, there was no error checking provided in the example.
Anyway, the listBox1_Click event handler now checks to make sure the index value is valid before processing it.