C # tooltip

Source: Internet
Author: User
Since: http://blog.5d.cn/user2/skyline-moon/200901/511240.html recently because of their own a small program needs, the requirements in the ComboBox drop-down Project (each item) display tooltip, search several times with Google, codeproject browsing several times, I found that there are few introductions and the methods described are mainly based on API capture. The code of this method seems complicated (for more complete implementation code, see [3]). After carefully reading the ComboBox content on msdn [1], you can see that the simpler implementation is through the drawitem of ComboBox (events generated when drawing the drop-down menu ). The following is the implementation code obtained by referring to [1] and [2. The main idea is to display the tooltip when the drop-down project is highlighted (drawitemstate. Selected) (here we need an instance of a new tooltip, And this is tooltip1 ). If you have multiple comboboxes that need to display tooltip, you should consider creating an inherited class for ComboBox. Here, I suggest using the method described in document [2]. The method in document [2] can also implement a function, that is, if the content length of the drop-down project does not exceed the width of the ComboBox, tooltip is not displayed. Note that when using the code in document [2], you need to change the condition for displaying tooltip to (E. State
& Drawitemstate. Selected) = drawitemstate. Selected
Delete the condition statement after Else. Otherwise, the expected result may not be obtained. Recently, due to the need of a small program, it is required to display ToolTip in the ComboBox drop-down Project (each item), search several circles with Google, and view the ToolTip several times on the Codeproject, I found that there are few introductions and the methods described are mainly based on api capture. The code of this method seems complicated (for more complete implementation code, see [3]). After carefully reading the ComboBox content on msdn [1], you can see that the simpler implementation is through the DrawItem of ComboBox (events generated when drawing the drop-down menu ). The following is the implementation code obtained by referring to [1] and [2. The main idea is to display the TooLTip when the drop-down project is highlighted (DrawItemState. Selected) (here we need an instance of a new ToolTip, And this is toolTip1 ). If you have multiple comboboxes that need to display ToolTip, you should consider creating an inherited class for ComboBox. Here, I suggest using the method described in document [2]. The method in document [2] can also implement a function, that is, if the content length of the drop-down project does not exceed the width of the ComboBox, toolTip is not displayed. Note that when using the code in document [2], you need to change the condition for displaying ToolTip to (e. State
& DrawItemState. Selected) = DrawItemState. Selected and delete the condition statement after Else. Otherwise, the expected result may not be obtained.
// [Code in the interface design, desigener] // create the kineticFileComboBox drop-down form this. kineticFileComboBox. formattingEnabled = true; this. kineticFileComboBox. location = new System. drawing. points (213,197); this. kineticFileComboBox. name = "kineticFileComboBox"; this. kineticFileComboBox. size = new System. drawing. size (170, 20); this. kineticFileComboBox. tabIndex = 14; this. kineticFileComboBox. text = "Please load the file first... "; // redraw the drop-down list window. You need to add the following sentence in the window design code: this. kineticFileComboBox. drawMode = System. windows. forms. drawMode. ownerDrawFixed; // The drop-down form redraws the event this. kineticFileComboBox. drawItem + = new System. windows. forms. drawItemEventHandler (kineticFileComboBox_DrawItem); this. kineticFileComboBox. dropDownClosed + = new System. eventHandler (kineticFileComboBox_DropDownClosed); // [code in the main program] private void kineticFileComboBox_DrawItem (object sender, System. windows. forms. drawItemEventArgs e) {// draw background e. drawBackground (); // draw a list Project e. graphics. drawString (kineticFileComboBox. items [e. index]. toString (), e. font, System. drawing. brushes. black, e. bounds); // pass the text of the highlighted list item to toolTip1 (an instance of ToolTip created earlier) if (e. state & DrawItemState. selected) = DrawItemState. selected) toolTip1.Show (kineticFileComboBox. items [e. index]. toString (), kineticFileComboBox, e. bounds. X + e. bounds. width, e. bounds. Y + e. bounds. height); e. drawFocusRectangle ();} // when the list is closed, the private void kineticFileComboBox_DropDownClosed (object sender, System. eventArgs e) {toolTip1.Hide (kineticFileComboBox );}
()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.