There was a request to provide tooltip for the items in the Listbox control.
The key idea is, selecting the listbox Index from the system point.
Here is the code.
Happy coding !
if (this.listBoxControl.SelectedIndex == -1)
{
TurnOffTooltip();
return;
}
if (sender is ListBox)
{
int selectedIndex = this.listBoxControl.SelectedIndex;
ListBox listBox = (ListBox)sender;
System.Drawing.Point point = new System.Drawing.Point(e.X, e.Y);
int hoverIndex = listBox.IndexFromPoint(point);
if (hoverIndex >= 0 && hoverIndex < listBox.Items.Count && hoverIndex == selectedIndex)
{
TurnOnTooltip();
}
else
{
TurnOffTooltip();
}
}
}
private void listBoxControl_MouseLeave(object sender, EventArgs e)
{
TurnOffTooltip();
}
private void TurnOffTooltip()
{
this.toolTip.SetToolTip(this.listBoxControl, "");
this.toolTip.Hide(this.listBoxControl);
return;
}
private void TurnOnTooltip()
{
SomeClass someClassObject = (SomeClass)this.listBoxControl.SelectedItem;
DisplayTooltip(someClassObject);
}
private void DisplayTooltip(SomeClass someClassObject)
{
string toDisplay = "Overlay Item : " + someClassObject.m_ILAOverlayItem.Name;
toDisplay = toDisplay + "\n" + "Impact Analysis Config : " + someClassObject.m_ILAimpactLayerConfig.ILConfigName;
toDisplay = toDisplay + "\n" + "GIS Layer : " + someClassObject.m_ILAimpactLayerConfig.GetGISLayerName();
toDisplay = toDisplay + "\n" + "Overlay Item : " + someClassObject.m_ILAOverlayItem.Name;
toDisplay = toDisplay + "\n" + "Inside Status Code : " + someClassObject.m_ILAInsideStatusCode[0].ToString();
toDisplay = toDisplay + "\n" + "Is Published : " + someClassObject.m_IsPublished.ToString();
this.toolTip.SetToolTip(this.listBoxControl, toDisplay);
this.toolTip.Show(toDisplay, this.listBoxControl);
}
private void listBoxControl_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.listBoxControl.SelectedItem != null)
{
SomeClass someClassObject = (SomeClass)this.listBoxControl.SelectedItem;
TurnOnTooltip();
}
}
The key idea is, selecting the listbox Index from the system point.
Here is the code.
Happy coding !
private void listBoxControl_MouseMove(object sender, MouseEventArgs e)
{if (this.listBoxControl.SelectedIndex == -1)
{
TurnOffTooltip();
return;
}
if (sender is ListBox)
{
int selectedIndex = this.listBoxControl.SelectedIndex;
ListBox listBox = (ListBox)sender;
System.Drawing.Point point = new System.Drawing.Point(e.X, e.Y);
int hoverIndex = listBox.IndexFromPoint(point);
if (hoverIndex >= 0 && hoverIndex < listBox.Items.Count && hoverIndex == selectedIndex)
{
TurnOnTooltip();
}
else
{
TurnOffTooltip();
}
}
}
private void listBoxControl_MouseLeave(object sender, EventArgs e)
{
TurnOffTooltip();
}
private void TurnOffTooltip()
{
this.toolTip.SetToolTip(this.listBoxControl, "");
this.toolTip.Hide(this.listBoxControl);
return;
}
private void TurnOnTooltip()
{
SomeClass someClassObject = (SomeClass)this.listBoxControl.SelectedItem;
DisplayTooltip(someClassObject);
}
private void DisplayTooltip(SomeClass someClassObject)
{
string toDisplay = "Overlay Item : " + someClassObject.m_ILAOverlayItem.Name;
toDisplay = toDisplay + "\n" + "Impact Analysis Config : " + someClassObject.m_ILAimpactLayerConfig.ILConfigName;
toDisplay = toDisplay + "\n" + "GIS Layer : " + someClassObject.m_ILAimpactLayerConfig.GetGISLayerName();
toDisplay = toDisplay + "\n" + "Overlay Item : " + someClassObject.m_ILAOverlayItem.Name;
toDisplay = toDisplay + "\n" + "Inside Status Code : " + someClassObject.m_ILAInsideStatusCode[0].ToString();
toDisplay = toDisplay + "\n" + "Is Published : " + someClassObject.m_IsPublished.ToString();
this.toolTip.SetToolTip(this.listBoxControl, toDisplay);
this.toolTip.Show(toDisplay, this.listBoxControl);
}
private void listBoxControl_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.listBoxControl.SelectedItem != null)
{
SomeClass someClassObject = (SomeClass)this.listBoxControl.SelectedItem;
TurnOnTooltip();
}
}
No comments:
Post a Comment