One happy day
Posted January 21, 2008 by toomCategories: personal
Well, if this isn’t one happy day, then what is?
Force TDateTimePicker to drop down
Posted August 30, 2007 by toomCategories: datetimepicker, delphi
Unfortunately there’s no message to send to TDateTimePicker which would cause it to drop down the calendar. Since the picker is designed to drop down by pressing either F4 or Alt-Down arrow, one can mimic the keypress and get the desired result with the following code:
... DateTimePicker1.Perform(WM_KEYDOWN, VK_F4, 0); DateTimePicker1.Perform(WM_KEYUP, VK_F4, 0); ...
Show hint on disabled control
Posted January 29, 2007 by toomCategories: delphi
If a control is disabled (Enable = False) then the mouseover hint is not shown. If You need to show the hint then put the control on a TPanel and set TPanel’s Hint property. Or just put a TPanel behind the control (send to back) – this works also.

Popup menu with MDI
Posted January 11, 2007 by toomCategories: delphi, popup
Situation: we have a MDI application whose main form (FormStyle = fsMDIForm) has a TPopupMenu (MainForm.PopupMenu = PopupMenu1) for some shortcut functions when user clicks mouse on the main form.
Problem: this PopupMenu1 is popping up everywhere in all the MDI children windows, but we want to use for example the Windows default popup menu in edit boxes:

Solution: Remove the PopupMenu1 from main form’s properties and create an OnContextPopup event handler:
procedure TMainForm.FormContextPopup(Sender: TObject;
MousePos: TPoint; var Handled: Boolean);
var
ScreenPoint: TPoint;
begin
// convert client area coordinates
// to global screen coordinates
ScreenPoint := ClientToScreen(MousePos);
// see if the control under cursor in our main form
if (FindDragTarget(ScreenPoint, True) is TMainForm)
then begin
// show our shortcut menu
PopupMenu1.Popup(ScreenPoint.X, ScreenPoint.Y);
// we handled the request for context menu
Handled := True;
end;
end;
Testing Estonian National identification number (isikukood)
Posted January 7, 2007 by toomCategories: delphi
As a followup to the SOTU checking function, here’s a function that checks the validity of Estonian National identification number.
Read the rest of this post »
Testing Finnish National identification number (SOTU)
Posted January 5, 2007 by toomCategories: delphi, string
Here’s a sample function to test the validity of Finnish National identification number (henkilötunnus (HETU)), formerly known as sosiaaliturvatunnus (SOTU)).
Read the rest of this post »
Align values separated by TAB in dropdown
Posted December 24, 2006 by toomCategories: combobox, delphi
We have put a ID-value pair items to TComboBox (using AddItem() method) and want to show both the ID and a value to user (using Additem() Item parameter). We might put the name of the value and append the ID in parentheses like this:

Not very nice, is it? Another option is to use tabulator (#9) to align the id-value pairs. Let’s try this out:

UGHH! The tabulator is just ignored. Luckily we have a Win32 API function TabbedTextOut():
The TabbedTextOut function writes a character string at a specified location, expanding tabs to the values specified in an array of tab-stop positions. Text is written in the currently selected font.
Now, we must write our own TComboBox.OnDrawItem event handler:
procedure TForm1.ComboBox1DrawItem(
Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
cb: TComboBox;
s: string;
t: integer;
begin
// check control type
Assert(Control is TComboBox);
// tab position (in pixels)
t := 50;
cb := TComboBox(Control);
// take the font
cb.Canvas.Font.Assign (cb.Font);
// take colours
if (odFocused in State) or
(odSelected in State) then begin
cb.Canvas.Brush.Color := clHighlight;
cb.Canvas.Font.Color := clHighlightText;
end else begin
cb.Canvas.Brush.Color := cb.Color;
if (odDisabled in State) then
cb.Canvas.Font.Color := clGrayText
else
cb.Canvas.Font.Color := cb.Font.Color;
end;
// get the item we have to draw
s := cb.Items[Index];
// show only the first value as selected value
if odComboBoxEdit in State then
s := Copy(s, 1, Pos(#9, s) - 1);
cb.Canvas.FillRect(Rect);
// ah boy, win32 rules to some extent
TabbedTextOut(cb.Canvas.Handle, Rect.Left,
Rect.Top, pChar(s), Length(s), 1,
t, Rect.Left);
end;
Let’s take a look at the result:

Cool!
Mind You: TComboBox.Style must be either csOwnerDrawFixed or csOwnerDrawVariable, otherwise the OnDrawItem event is not fired.


