In my windows forms application, I added a SplitContainer control. In the panel1 of SplitContainer, I have a ListBox and in the panel2 of SpliContainer, I have two buttons. on the mouse move of ListBox I want to select the ListBox item. below are my codes to select the listbox item,
private void ListBox1_MouseMove(object sender, MouseEventArgs e)
{
int i;
this.SuspendLayout();
for (i = 0; i < (this.listBox1.Items.Count); i++)
{
if (this.listBox1.GetItemRectangle(i).Contains(this.listBox1.PointToClient(MousePosition)))
{
this.listBox1.SelectedIndex = i;
return;
}
}
this.ResumeLayout(true);
}
The SuspendLayout() and ResumeLayout() are called to avoid the overlapping of panels when the form is loading.
If I restore the form, mouse move on the ListBox and again maximize the form then the buttons in the SplitContainer panel2 is not loading properly. If I remove the Suspend and ResumeLayout the restore and maximize works fine. I referred this stackoverflow query link.
The ListBox does not have Resize and dock property. So shouldn't I call the SuspendLayout and ResumeLayout? anyone suggest where to use SuspendLayout/ResumeLayout and where to not?
Try it like this:
private void ListBox1_MouseMove(object sender, MouseEventArgs e)
{
int newindex = ListBox1.IndexFromPoint(e.Location);
if (newindex != index) //avoid flickering
{
int i;
this.SuspendLayout();
for (i = 0; i < (this.listBox1.Items.Count); i++)
{
if (this.listBox1.GetItemRectangle(i).Contains(this.listBox1.PointToClient(MousePosition)))
{
this.listBox1.SelectedIndex = i;
index = newindex;
//return; why return?
}
}
this.ResumeLayout(true);
}
}
Just declare index as a global variable.