CheckedListBox 항목에 대한 툴팁?
-
03-07-2019 - |
문제
사용자의 마우스가 checkedlistbox에서 항목 위에 고정 될 때 툴팁에 추가 텍스트를 설정할 수있는 방법이 있습니까?
내가 무엇을하든 예상하다 코드에서 할 수있는 것은 다음과 같습니다.
uiChkLstTables.DisplayOnHoverMember = "DisplayOnHoverProperty"; //Property contains extended details
누구 든지이 일을하는 올바른 방향으로 나를 지적 할 수 있습니까? 나는 이미 마우스가 현재 끝나는 항목을 감지하고 새로운 툴팁 인스턴스를 생성하는 것과 관련된 몇 가지 기사를 이미 발견했지만 이는 최선의 방법으로 너무 고안된 것 같습니다.
미리 감사드립니다.
해결책
양식에 툴팁 객체를 추가 한 다음 method showtooltip ()를 호출하는 checkedlistbox.mousehover에 대한 이벤트 핸들러를 추가하십시오. CheckedListbox의 MousEmove 이벤트 추가 다음 코드가 있습니다.
//Make ttIndex a global integer variable to store index of item currently showing tooltip.
//Check if current location is different from item having tooltip, if so call method
if (ttIndex != checkedListBox1.IndexFromPoint(e.Location))
ShowToolTip();
그런 다음 showtooltip 메소드를 만듭니다.
private void ShowToolTip()
{
ttIndex = checkedListBox1.IndexFromPoint(checkedListBox1.PointToClient(MousePosition));
if (ttIndex > -1)
{
Point p = PointToClient(MousePosition);
toolTip1.ToolTipTitle = "Tooltip Title";
toolTip1.SetToolTip(checkedListBox1, checkedListBox1.Items[ttIndex].ToString());
}
}
다른 팁
또는 a를 사용할 수 있습니다 ListView
대신 확인란과 함께. 이 컨트롤에는 있습니다내장 을지 지하다 툴팁.
고안 여부; 그게 ...
나는 당신이 이미 설명한 것보다 쉬운 방법을 알지 못합니다 (항상 새로운 것을 만들기보다는 툴팁 인스턴스를 재사용 할 수 있습니다). 이것을 보여주는 기사가있는 경우, 그것들을 사용하거나, 이것을 기본적으로 지원하는 타사 컨트롤을 사용하십시오 (아무도 떠오르지 않음).
아마도 그의 훌륭한 솔루션을 약간 더 명확하게하기 위해 Fermin의 답변을 확장하고 싶습니다.
(.designer.cs 파일에있을 가능성이 높음)에서 작업하는 형태로 MouseMove 이벤트 핸들러를 CheckedListbox에 추가해야합니다 (Fermin은 원래 Mousehover 이벤트 핸들러를 제안했지만 이것은 작동하지 않았습니다).
this.checkedListBox.MouseMove += new System.Windows.Forms.MouseEventHandler(this.showCheckBoxToolTip);
다음으로 양식에 두 가지 클래스 속성을 추가하십시오. 도구 팁 객체 및 정수를 추가하여 도구 팁이 표시된 마지막 확인란을 추적합니다.
private ToolTip toolTip1;
private int toolTipIndex;
마지막으로 showcheckboxtooltip () 메소드를 구현해야합니다. 이 방법은 이벤트 콜백 메소드를 showtooltip () 메소드와 결합했다는 점을 제외하고는 Fermin의 답변과 매우 유사합니다. 또한 메소드 매개 변수 중 하나는 Mouseeventargs입니다. 이는 MouseMove 속성에 Mouseeventhandler가 필요하고 Mouseeventargs를 공급하기 때문입니다.
private void showCheckBoxToolTip(object sender, MouseEventArgs e)
{
if (toolTipIndex != this.checkedListBox.IndexFromPoint(e.Location))
{
toolTipIndex = checkedListBox.IndexFromPoint(checkedListBox.PointToClient(MousePosition));
if (toolTipIndex > -1)
{
toolTip1.SetToolTip(checkedListBox, checkedListBox.Items[toolTipIndex].ToString());
}
}
}
항목의 확인란 목록에서 목록을 실행하고 적절한 텍스트를 항목 'Title'속성으로 설정하면 호버에 표시됩니다 ...
foreach (ListItem item in checkBoxList.Items)
{
//Find your item here...maybe a switch statement or
//a bunch of if()'s
if(item.Value.ToString() == "item 1")
{
item.Attributes["title"] = "This tooltip will display when I hover over item 1 now, thats it!!!";
}
if(item.Value.ToString() == "item 2")
{
item.Attributes["title"] = "This tooltip will display when I hover over item 2 now, thats it!!!";
}
}