Saturday, March 16, 2013

ComboBox with ToolTip to each item


When we use ComboBox Control in a form we assume certain length for the control.
In case the data is from query (Active Directory, SQL) we can't predict the item length.

And in some case the item is longer than the control. It can be solved by enlarge the ComboBox control length, or add ToolTip to Combobox so after selection you can see the full string , or to ignore long item (kidding..J). Other option (more user friendly) to add ToolTip to each item's In this case the user sees right way if is selection is the right one.

To enable tooltip for each item in the ComboBox control, you must interfering the process of drawing the items. Actually you have to build small routine to draw the item's in the dropdown list & to add to each item a ToolTip.
First we have to catch the event of drawing the item of ComboBox

Then we have to build a small routing to draw the items of combobox .

13: [System.Windows.Forms.DrawItemEventHandler] to catch the Event.
14:
15: Get the location of drawing item.
16: Set the Text for Drawing item.
18: set the color for (foreground Color) it can be name or FromArgb
          (http://msdn.microsoft.com/en-us/library/system.windows.media.colors.aspx).
19: Draw the background for the item.
20: Set the fore color to a brush object.
21: Set the font for Drawing items.
22: Drawing the Item use DrawString method.
24: condition to check if the user is go over the drop down item list
          ($ComboBox1.SelectedIndex -gt -1)
          and that the mouse move to other item .
26: Set the ToolTip for the current select item.

To download the example press here.

RTL ComboBox with ToolTip:


In case your language is RTL Hebrew, Arabic. it's more complicate but it's possible. The complicity comes from the ToolTip object. ComboBox or ListBox or TextBox Have RTL build into the control, his is not the case with ToolTip (at least I didn't find a way).
Because we catch the Event of drawing item we now have to draw the item from right to left. another thing is that ToolTip is always draw to the right and if we want to create the feel that the toolTip is draw from the most left corner of the item to the left ,we have to place it in different X position for every item depending the item length.

We will use the same routine


We add format "String Format" with DirectionRightToLeft.
When we draw the item we have to start in the Right most ($Location.x+$ComboBox1.Size.Width)



Sizef : calculate the length of the tooltip according to the text ($atext) & the Font ($afont). We reduce the SizeF from the left position of the ToolTip. Last & nice we can add timeout for the ToolTip 3 sec' in this example.

  

ToolTip with more the one line RTL aligment:

In my example the ToolTip has just one line ,So the problem of RTL was not seen.
But when there is more than one line it's clear that we have a problem.


You can see on the right what happen when we have tow line. The text is alignment to the left instead to right.
To solve it we have to adjust the ToolTip object and to add routing that will draw the ToolTip text
.


                                                                                                                                               
75: OwnerDraw = $true - allow as to take control over the ToolTip Drawing Proccess.
76: Add_Draw  -
to catch the event of ToolTip drawing.


42: set the color for (Background Color) it can be name or FromArgb.
          (http://msdn.microsoft.com/en-us/library/system.windows.media.colors.aspx).
43: Set the Brush object.
44: drawing the ToolTip frame & background.
45: Draw the border.
46: Draw the ToolTip text according to our definition
.

Important to understand!This routing is activate when $ToolTip.Show is execute.
if you prefer you can set the ToolTip Background color direct in the object
$toolTip1.BackColor = [System.Drawing.Color]::FromArgb(255,224,224,224
).

Hope that this will be useful to someone.
To download the example press here.
I would like to thanks to:renuka krishnan for his article "Usinga Combobox to Select Colors"
PHeiberg from stackoverflow about is answer "Right to left tooltip text c#"Their answer/article makes sense and helps me to understand the MSDN example from MSDN.To draw centered text with GDI+ (DrawString)
http://msdn.microsoft.com/en-us/library/332kzs7c.aspx ComboBox:http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.aspx

Doron Zilber D/..\Z
---------------------------------------------------------------------------------------------

Fix

When you go over the items in the drop down list, the select one is difficult to sea.
the rezone is that in the code I didn't change is color to white to be more visible





If you add the tow line the Select item will be more visible.


 
















The code for the tow line i add it below.
37    $aBrush = new-object Drawing.SolidBrush "white"
38    $_.Graphics.DrawString($atext,$Combobox1.font,$aBrush,$Location.x + `
          $ComboBox1.Size.Width),$Location.y,$format1)

No comments:

Post a Comment