<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>AppBox</title>
	<atom:link href="http://appbox.ir/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://appbox.ir</link>
	<description>وب سایت آموزشی ایرانیان</description>
	<lastBuildDate>Thu, 12 Apr 2012 16:52:01 +0000</lastBuildDate>
	<language>fa</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Only numerical input in a TEdit</title>
		<link>http://appbox.ir/?p=219</link>
		<comments>http://appbox.ir/?p=219#comments</comments>
		<pubDate>Thu, 12 Apr 2012 16:52:01 +0000</pubDate>
		<dc:creator>Mah6447</dc:creator>
				<category><![CDATA[سورس کد دلفی]]></category>

		<guid isPermaLink="false">http://appbox.ir/?p=219</guid>
		<description><![CDATA[If you want to limit the input of a TEdit to numerical strings only, you can discard the &#8220;invalid&#8221; characters in its OnKeyPress event handler. Let&#8217;s assume that you only want to allow positive integer numbers. The code for the OnKeyPress event handler is as follows: procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); begin // #8 is Backspace [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: left;">If you want to limit the input of a TEdit to numerical strings only, you can discard the &#8220;invalid&#8221; characters in its <strong>OnKeyPress</strong> event handler.</p>
<p style="text-align: left;">Let&#8217;s assume that you only want to allow positive integer numbers. The code for the OnKeyPress event handler is as follows:</p>
<blockquote>
<pre dir="ltr">procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  <span style="color: #000099;"><em>// #8 is Backspace</em></span>
  if not (Key in [#8, '0'..'9']) then begin
    ShowMessage('Invalid key');
    <span style="color: #000099;"><em>// Discard the key</em></span>
    Key := #0;
  end;
end;</pre>
<p><span id="more-219"></span></p></blockquote>
<p dir="ltr">If you also want numbers with a decimal fraction, you must allow a POINT or a COMMA, but only once. For an international version that looks at the correct decimal separator, the code could be as follows:</p>
<blockquote dir="ltr">
<pre>procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if not (Key in [#8, '0'..'9', DecimalSeparator]) then begin
    ShowMessage('Invalid key: ' + Key);
    Key := #0;
  end
  else if (Key = DecimalSeparator) and
          (Pos(Key, Edit1.Text) &gt; 0) then begin
    ShowMessage('Invalid Key: twice ' + Key);
    Key := #0;
  end;
end;</pre>
</blockquote>
<pre dir="ltr">And here's a full blown version of the event handler, accepting a decimal separator and negative numbers (minus sign: only accepted once, has to be the first character):</pre>
<blockquote>
<pre dir="ltr">procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if not (Key in [#8, '0'..'9', '-', DecimalSeparator]) then
    ShowMessage('Invalid key: ' + Key);
    Key := #0;
  end
  else if ((Key = DecimalSeparator) or (Key = '-')) and
          (Pos(Key, Edit1.Text) &gt; 0) then begin
    ShowMessage('Invalid Key: twice ' + Key);
    Key := #0;
  end
  else if (Key = '-') and
          (Edit1.SelStart &lt;&gt; 0) then begin
    ShowMessage('Only allowed at beginning of number: ' + Key);
    Key := #0;
  end;
end;</pre>
</blockquote>
<p dir="ltr">How about giving that same behaviour to several TEdits on the same form, say 10 of them? In the Object Inspector, you change the name of the event handler of Edit1 from Edit1KeyPress to<em>Edit1_10KeyPress</em> or something similar. Delphi automatically changes the name in the code editor, don&#8217;t worry.</p>
<p dir="ltr">Then, for each next TEdit, you select its OnKeyPress event and you select <em>Edit1_10KeyPress</em> from the listbox next to the event.</p>
<p dir="ltr">Finally, we have to slightly adapt the code. Intead of pointing to Edit1, we must point to &#8220;the TEdit that generated the event&#8221;, in other words: the edit-box where the cursor was when a key was depressed. When you look at the template for the event handler that Delphi made, you see the parameter <strong>Sender</strong>: that&#8217;s a pointer to the component that generated the event. But we are not allowed to use it straight away in our code, we must specify that we&#8217;re dealing with a component of the type TEdit. That&#8217;s done with the code <strong>Sender as TEdit</strong>:</p>
<blockquote>
<pre dir="ltr">procedure TForm1.Edit1_10KeyPress(Sender: TObject; var Key: Char);
begin
  if not (Key in [#8, '0'..'9', '-', DecimalSeparator]) then
    ShowMessage('Invalid key: ' + Key);
    Key := #0;
  end
  else if ((Key = DecimalSeparator) or (Key = '-')) and
          (Pos(Key, (Sender as TEdit).Text) &gt; 0) then begin
    ShowMessage('Invalid Key: twice ' + Key);
    Key := #0;
  end
  else if (Key = '-') and
          ((Sender as TEdit).SelStart &lt;&gt; 0) then begin
    ShowMessage('Only allowed at beginning of number: ' + Key);
    Key := #0;
  end;
end;</pre>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://appbox.ir/?feed=rss2&#038;p=219</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Override Global Screen.Cursor Change for a Cancel type Button</title>
		<link>http://appbox.ir/?p=161</link>
		<comments>http://appbox.ir/?p=161#comments</comments>
		<pubDate>Mon, 02 Apr 2012 15:44:26 +0000</pubDate>
		<dc:creator>Mah6447</dc:creator>
				<category><![CDATA[سورس کد دلفی]]></category>

		<guid isPermaLink="false">http://appbox.ir/?p=161</guid>
		<description><![CDATA[When you want to run a lengthy process in your application that needs no user interaction, you can change the value of the Screen.Cursor property to have the mouse cursor display as a hourglass, for example, while the process is being executed. The Screen.Cursor property of the global Screen object has a global effect. It controls the cursor shape for all [...]]]></description>
			<content:encoded><![CDATA[<blockquote dir="ltr"><p>When you want to run a <strong>lengthy process</strong> in your application that needs <strong>no user interaction</strong>, you can change the value of the Screen.Cursor property to have the <strong>mouse cursor display as a hourglass</strong>, for example, while the process is being executed.</p>
<p>The Screen.Cursor property of the global <a href="http://delphi.about.com/od/vclusing/a/tscreen.htm">Screen object</a> has a global effect. It controls the cursor shape for all the forms, and the controls on the form, in the application.</p>
<p>When you change the global screen cursor, the user <strong>can still click any buttons on the form</strong> but visual appearance of the cursor does not imply so.</p>
<p>When you &#8220;surround&#8221; a consuming / critical operation with a global cursor change, you might want to have one button act as a <strong>cancel button</strong>, where the click to this button would stop the process.</p>
<p>Since the Screen.Cursor will affect all the controls on the form, you might want to make sure that this &#8220;cancel button&#8221; displays a &#8220;normal&#8221; cursor when the mouse pointer is over it.</p>
<p>Note that in the above scenario I&#8217;m not taking about the Cancel property of a Button which determines whether the button’s OnClick event handler executes when the Escape key is pressed.<br />
I&#8217;m talking of a button that when clicked will stop the lengthy process.</p>
<p><span id="more-161"></span></p>
<p>&nbsp;</p>
<h3>Force Button.Cursor to &#8220;Override&#8221; Screen.Cursor</h3>
<pre><code> Screen.Cursor := crHourglass; <strong>try</strong>   <em>// Do some </em></code></pre>
<pre><code><em><strong>"lengthy operation</strong>"</em>   <em>//have a button, btnCancel that can stop this operation</em> </code></pre>
<pre><code><strong>finally</strong>    </code></pre>
<pre><code>Screen.Cursor := crDefault; </code></pre>
<pre><code><strong>end</strong>; </code></pre>
<p>If the &#8220;lengthy operation&#8221; does not block message processing completely your &#8220;cancel&#8221; button will be clickable. To ensure messages are not blocked you can call <strong>Application.ProcessMessages</strong> in intervals, if the process is being executed in the main thread of the application.</p>
<p>While the lengthy process is being run your btnCancel will display a hourglass when the mouse cursor moves over it &#8211; since crHourglass is globally set by the Screen object.</p>
<p>If only for this button you want to display some other cursor shape, the one set using that button&#8217;s Cursor property, you can <em>hijack the Tag property</em> of the button to set the cursor shape when the mouse enters its client area and set it back to global Screen value when the mouse exits its client area. Here&#8217;s how:</p>
<pre><code> <em>//"cancel" button OnMouseEnter event handler</em> </code></pre>
<pre><code><strong>procedure</strong> TOperationForm.btnCancelMouseEnter(Sender: TObject) ; </code></pre>
<pre><code><strong>begin</strong>   <em>//store current </em></code></pre>
<pre><code><em>screen.cursor value</em>   </code></pre>
<pre><code>btnCancel.Tag := Screen.Cursor;   <em>//temporary set screen.cursor</em>   </code></pre>
<pre><code>Screen.Cursor := btnCancel.Cursor; </code></pre>
<pre><code><strong>end</strong>; <em>//"cancel" button OnMouseLeave event handler</em> </code></pre>
<pre></pre>
<pre><code><strong>procedure</strong> TOperationForm.btnCancelMouseLeave(Sender: TObject) ; </code></pre>
<pre><code><strong>begin</strong>   <em>//restore current screen.cursor value</em>   </code></pre>
<pre><code>Screen.Cursor := btnCancel.Tag; </code></pre>
<pre><code><strong>end</strong>; </code></pre>
<p>The above code presumes you are not already using the Tag property of the cancel button to store some other data.</p>
<p>The OnMouseEnter event handler is used to store the current value of the global mouse shape in the Tag property of the button. Screen.Cursor is set to the value of the button&#8217;s Cursor property.</p>
<p>The TCursor is defined as type TCursor = -32768..32767, and Tag is an integer property, you can safely store the value of Cursor to the Tag property.</p>
<p>When the mouse leaves the button, the Screen.Cursor is restored.</p>
<p>A neat trick. For a more global solution, you might want to create your own button and store the global cursor value in some integer type property.</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://appbox.ir/?feed=rss2&#038;p=161</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sort a Generic List using Anonymous Comparer Method</title>
		<link>http://appbox.ir/?p=157</link>
		<comments>http://appbox.ir/?p=157#comments</comments>
		<pubDate>Mon, 02 Apr 2012 15:42:11 +0000</pubDate>
		<dc:creator>Mah6447</dc:creator>
				<category><![CDATA[سورس کد دلفی]]></category>

		<guid isPermaLink="false">http://appbox.ir/?p=157</guid>
		<description><![CDATA[Delphi 2009 adds Generics and Anonymous methods to the Delphi language. When you have objects in some list &#8211; one of a commonly required tasks is to sort the list: either ascending, descending or using some list-specific algorithm. Using anonymous methods you can apply the sorting function directly &#8220;inline&#8221; with the call to the Sort method. TList&#60;integer&#62;.Sort(IComparer) Suppose you [...]]]></description>
			<content:encoded><![CDATA[<blockquote dir="ltr"><p>Delphi 2009 adds <strong>Generics</strong> and <strong>Anonymous methods</strong> to the Delphi language.</p>
<p>When you have objects in some list &#8211; one of a commonly required tasks is to sort the list: either ascending, descending or using some list-specific algorithm.</p>
<p>Using anonymous methods you can apply the sorting function directly &#8220;inline&#8221; with the call to the Sort method.</p>
<h3><span id="more-157"></span></h3>
<h3>TList&lt;integer&gt;.Sort(IComparer)</h3>
<p>Suppose you have a list of integer numbers. Your task is to sort the numbers in the list.</p>
<p>Here&#8217;s the code:</p>
<pre><code> <strong>uses</strong> generics.defaults, </code>generics.collections,...</pre>
<pre>...</pre>
<pre><strong>type</strong>   TIntegerComparer = TComparer&lt;integer&gt;;</pre>
<pre>...</pre>
<pre><strong>var</strong>   lg : TList&lt;integer&gt;;</pre>
<pre>n : integer;</pre>
<pre><strong>begin</strong></pre>
<pre>lg := TList&lt;integer&gt;.Create;</pre>
<pre><strong>try</strong></pre>
<pre>lg.Add(2) ;</pre>
<pre>lg.Add(4) ;</pre>
<pre>lg.Add(3) ;</pre>
<pre>lg.Add(4) ;</pre>
<pre>lg.Add(5) ;</pre>
<pre>lg.Add(1) ;</pre>
<pre><em>//will display 2,4,3,4,5,1</em></pre>
<pre><strong>for</strong> n <strong>in</strong> lg do ShowMessage(IntToStr(i)) ;</pre>
<pre><em>//default ascending comparer</em></pre>
<pre>lg.Sort(TComparer&lt;integer&gt;.Default) ;</pre>
<pre><em>//will display 1,2,3,4,4,5</em></pre>
<pre><strong>for</strong> n <strong>in</strong> lg do</pre>
<pre>ShowMessage(IntToStr(i)) ;</pre>
<pre><em>//descending using an anonymous method</em></pre>
<pre>lg.Sort(TIntegerComparer.Construct(</pre>
<pre><strong>function</strong> (<strong>const</strong> L, R: integer): integer</pre>
<pre><strong>begin</strong></pre>
<pre>result := R - L;</pre>
<pre><strong>end</strong> ;</pre>
<pre><em>//will display 5,4,4,3,2,1</em></pre>
<pre><strong>for</strong> n <strong>in</strong> lg do ShowMessage(IntToStr(i)) ;</pre>
<pre><strong>finally</strong></pre>
<pre>lg.Free;</pre>
<pre><strong>end</strong>;</pre>
<pre><strong>end</strong>;</pre>
<p>Note: the TList&lt;T&gt;.Sort methods expects an <strong>IComparer&lt;T&gt;</strong> object. IComparer&lt;T&gt; is an interface defined in the generics.defaults unit.</p>
<p>TComparer, also defined in the Generics.Defaults, is an abstract base class for IComparer&lt;T&gt; implementations.</p>
<p>The Construct method of the TComparer&lt;T&gt; is a <a href="http://delphi.about.com/od/objectpascalide/a/classmethods.htm">class method</a> defined as:</p>
<pre><code> <strong>class function</strong> Construct(<strong>const</strong> Comparison: TComparison&lt;T&gt;): IComparer&lt;T&gt;; </code></pre>
<p>The above returns an IComparer&lt;T&gt; we need for the Sort method AND accepts an anonymous method:</p>
<pre><code> TComparison&lt;T&gt; = <strong>reference</strong> to <strong>function</strong>(<strong>const</strong> Left, Right: T): Integer; </code></pre>
<p>Finally, you have a Sort method with an anonymous method as &#8220;parameter&#8221;:</p>
<pre><code> lg.Sort(TIntegerComparer.Construct(   <strong>function</strong> (<strong>const</strong> L, R: integer): integer   </code></pre>
<pre><code><strong>begin</strong>     </code></pre>
<pre><code>result := R - L;   </code></pre>
<pre><code><strong>end</strong> ; </code></pre>
<p>For a non-generic TList object: <a href="http://delphi.about.com/od/adptips2003/a/bltip1103_3.htm">How to sort items in a TList object.</a></p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://appbox.ir/?feed=rss2&#038;p=157</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cycle Enum Values in Delphi</title>
		<link>http://appbox.ir/?p=155</link>
		<comments>http://appbox.ir/?p=155#comments</comments>
		<pubDate>Mon, 02 Apr 2012 15:41:01 +0000</pubDate>
		<dc:creator>Mah6447</dc:creator>
				<category><![CDATA[سورس کد دلفی]]></category>

		<guid isPermaLink="false">http://appbox.ir/?p=155</guid>
		<description><![CDATA[An enumerated type in Delphi, or enum lets you define a list of values. The values have no inherent meaning, and their ordinality follows the sequence in which the identifiers are listed. For example, the Align property most Delphi controls expose is of type TAlign which is an enum type defined as TAlign = (alNone, alTop, alBottom, alLeft, alRight, alClient, alCustom) [...]]]></description>
			<content:encoded><![CDATA[<blockquote dir="ltr"><p>An <a href="http://delphi.about.com/od/objectpascalide/l/aa012500a.htm">enumerated type</a> in Delphi, or <strong>enum</strong> lets you define a list of values. The values have no inherent meaning, and their ordinality follows the sequence in which the identifiers are listed.</p>
<p>For example, the <em>Align</em> property most Delphi controls expose is of type TAlign which is an enum type defined as</p>
<p>TAlign = (alNone, alTop, alBottom, alLeft, alRight, alClient, alCustom)</p>
<p>Another example is the <em>ViewStyle</em> property of a <a href="http://delphi.about.com/od/delphitips2009/qt/cycle-enum.htm">TListView</a>. ViewStyle determines the visual display of items in a list view. The items can be displayed as a set of movable icons, or as columns of text.</p>
<p><span id="more-155"></span></p>
<p>The TViewStyle enum is defined as:</p>
<p>TViewStyle = (vsIcon, vsSmallIcon, vsList, vsReport)</p>
<h3>Round Robin Cycling Through Enum Values</h3>
<p>If you need to allow a user of your application to change the display of the ListView at run-time, you need to provide some sort of &#8220;selectable&#8221; user interface. You can choose to <a href="http://delphi.about.com/od/delphitips2007/qt/enum_selection.htm">display enumerated properties in a selectable list</a>.</p>
<p>If you want to <strong>cycle through the possible set of values defined by an enum</strong>, you can implement a so called &#8220;round robin&#8221; algorithm.</p>
<p>From the user interface point of view, you could have a button that when clicked changes, by &#8220;increasing&#8221; the current value, the current ViewStyle of a list view.</p>
<p>Here&#8217;s how to cycle through enumeration values for any enum:</p>
<pre><code> <em>//with ListView1 do</em> ViewStyle := TViewStyle((1 + Ord(ViewStyle)) <strong>mod</strong> (1 + Ord(High(TViewStyle)))) ; </code></pre>
<p>The above code cycles through the TViewStyle enumeration by assigning an increased value to the ViewStyle property.</p>
<p>The above cycling can be used for any enumeration type &#8211; just make sure what&#8217;s a property and what&#8217;s a type declaration, in this code snippet <img src='http://appbox.ir/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://appbox.ir/?feed=rss2&#038;p=155</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Delphi Interceptor Classes -&gt; TButton = class(TButton)</title>
		<link>http://appbox.ir/?p=153</link>
		<comments>http://appbox.ir/?p=153#comments</comments>
		<pubDate>Mon, 02 Apr 2012 15:40:11 +0000</pubDate>
		<dc:creator>Mah6447</dc:creator>
				<category><![CDATA[سورس کد دلفی]]></category>

		<guid isPermaLink="false">http://appbox.ir/?p=153</guid>
		<description><![CDATA[Have you ever needed for a specific Delphi control, like a TButton, to have just one more property or a method that is a &#8220;must have&#8221; for your current application? Experienced Delphi developers, when they need a TMySuperButton, would either look for a third-party VCL solution or would try creating a derived control. What if you [...]]]></description>
			<content:encoded><![CDATA[<blockquote dir="ltr"><p>Have you ever needed for a specific Delphi control, like a TButton, to have just one more property or a method that is a &#8220;must have&#8221; for your current application?</p>
<p>Experienced Delphi developers, when they need a <em>TMySuperButton</em>, would either look for a third-party VCL solution or would try creating a derived control.</p>
<p>What if you do not want to have this <em>MySuperButton</em> permanently in the Component/Tool Palette &#8211; since it solves one problem only for this particular application you are developing?</p>
<p>What&#8217;s more &#8230; how about having a TButton with more properties and methods, some application specific extra behavior, and not TMySupperButton? How about extending what TButton has without the need to create a derived class with a different name?</p>
<p>You could think now of <a href="http://delphi.about.com/od/delphifornet/a/classhelpers.htm">class helpers</a>, but class helper allow only <a href="http://delphi.about.com/od/objectpascalide/a/classmethods.htm">class methods</a> to be added to a class.</p>
<p>What most beginners do not know is that they *can* create their own custom controls derived from the existing VCL set by creating a, so called, <strong>interceptor class</strong> that has the <strong>same name as the class being extended</strong>.</p>
<h3><span id="more-153"></span></h3>
<h3>Delphi Interceptor Classes</h3>
<p>First, to have two classes with the same name is possible in Delphi if those two classes are in separate units.</p>
<p>Next, the following declaration is legal</p>
<pre><code> <strong>type</strong> TButton = <strong>class</strong>(StdCtrls.TButton) </code></pre>
<p>The above code creates an interceptor class for a TButton control by extending the &#8220;original&#8221; TButton declared in the VCL&#8217;s StdCtrls unit.</p>
<p>Finally, the above declaration needs to be placed in a separate unit.</p>
<p>Most important, the unit hosting the interceptor class needs to be listed in the uses clause *after* the unit defining the base class.</p>
<h3>TButton = class(TButton) &#8211; an Interceptor Class Example</h3>
<p>Here&#8217;s one interceptor class for the TButton.</p>
<p>A public property &#8220;LastClickTime&#8221; is added. The original TButton&#8217;s Click procedure is overridden &#8211; Click raises the OnClick event.</p>
<p>Using the overridden Click procedure our TButton interceptor will not allow running the OnClick event handler if the last click on the button happened during the last two second, only a Beep sound will play.</p>
<p>Ok, a dummy example, but serves the purpose. <a href="http://delphi.about.com/library/weekly/code/interceptor-class-tbutton.zip">Download an example project: TButton interceptor</a>.</p>
<pre><code> <strong>unit</strong> button_interceptor; <strong>interface</strong> <strong>uses</strong> stdctrls, sysutils, DateUtils; <strong>type</strong>   TButton = <strong>class</strong>(stdctrls.TButton)   <strong>private</strong>     fLastClickTime: TDateTime;   <strong>public</strong>     <strong>procedure</strong> Click; <strong>override</strong>;   <strong>public</strong>     <strong>property</strong> LastClickTime : TDateTime <strong>read</strong> fLastClickTime <strong>write</strong> fLastClickTime;   <strong>end</strong>; <strong>implementation</strong> <strong>procedure</strong> TButton.Click; <strong>const</strong>   ClickWaitPeriod = 2; <strong>var</strong>   clickTime : TDateTime; <strong>begin</strong>   clickTime := Now;   <strong>if</strong> SecondsBetween(clickTime, LastClickTIme) &gt; ClickWaitPeriod <strong>then</strong>      <strong>inherited</strong>   <strong>else</strong>     Beep;   LastClickTime := clickTime; <strong>end</strong>; <strong>end</strong>. </code></pre>
<p>Now, drop a standard button (TButton) on a Delphi form. Attach some event handler to its OnClick event.</p>
<p>Most importantly, <strong>add the &#8220;button_interceptor&#8221; unit in the uses list as the last unit listed</strong>, or at least after the &#8220;stdctrls&#8221; unit!</p>
<p>Try clicking the button &#8211; note that the code executes only if you wait for two second after the last click.</p>
<p>That&#8217;s it. Using the above you can easily extend Delphi classes by adding extra properties and methods &#8211; without the need to create derived classes and have them in the component palette.</p>
<p>Ah almost forgot: note that when you type &#8220;Button1.&#8221; and the list of button&#8217;s properties and methods pops up &#8211; you see the added &#8220;LastButtonClick&#8221; as if it belongs to the standard Delphi&#8217;s TButton. Delphi (compiler) magic <img src='http://appbox.ir/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Also note that you can use interceptor classes not only to extend VCL controls but to extend any Delphi class.</p>
<p>If you need to fake creating your own inherited VCL control, take a look at <a href="http://delphi.about.com/od/vclwriteenhance/a/vcl-template.htm">Fake Creating a New Delphi Control</a>.</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://appbox.ir/?feed=rss2&#038;p=153</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Force Display of ToolButton&#8217;s PopupMenu when Button is Clicked</title>
		<link>http://appbox.ir/?p=151</link>
		<comments>http://appbox.ir/?p=151#comments</comments>
		<pubDate>Mon, 02 Apr 2012 15:38:38 +0000</pubDate>
		<dc:creator>Mah6447</dc:creator>
				<category><![CDATA[سورس کد دلفی]]></category>

		<guid isPermaLink="false">http://appbox.ir/?p=151</guid>
		<description><![CDATA[Delphi&#8217;s TToolBar control manages tool buttons, TToolButton controls. Typically, the tool buttons correspond to items in an application&#8217;s menu and give the user more direct access to the application&#8217;s commands. Depending on the Style property of the button placed on a tool bar, a button, a drop down button, a text button or a divider will be [...]]]></description>
			<content:encoded><![CDATA[<blockquote dir="ltr"><p>Delphi&#8217;s TToolBar control manages tool buttons, TToolButton controls. Typically, the tool buttons correspond to items in an application&#8217;s menu and give the user more direct access to the application&#8217;s commands.</p>
<p>Depending on the <em>Style</em> property of the button placed on a tool bar, a button, a drop down button, a text button or a divider will be displayed to the user. Tool buttons can act like regular push buttons; toggle on and off when clicked and can act like a set of radio buttons.</p>
<h3><span id="more-151"></span></h3>
<h3>Style = tbsDropDown and the OnClick event</h3>
<p>When tool button&#8217;s Style is set to <em>tbsDropDown</em>, the button displays a down-pointing arrow (suitable for accessing a drop-down menu). When its <em>DropDownMenu</em> points to an instance of a<a href="http://delphi.about.com/sitesearch.htm?terms=TPopupMenu&amp;SUName=delphi&amp;TopNode=99">TPopupMenu</a> - the <strong>button when clicked will display the popup menu associated</strong>.</p>
<p>The actual popup menu will get displayed ONLY when you click the down-pointing arrow.</p>
<p>If a user clicks on the button itself and not on the down-pointing arrow &#8211; the popup menu will NOT get displayed.</p>
<h3>Display the Drop Down Menu when ToolButton is Clicked &#8220;Anywhere&#8221;</h3>
<p>To make sure that the drop down menu gets displayed when you click on the button, as opposed to clicking on the down-pointing arrow, you need to handle button&#8217;s OnClick event and <strong>force the display of the popup menu associated</strong>.</p>
<p>Fortunately, there&#8217;s a <strong>CheckMenuDropdown</strong> method of the tool button that you can call to manually display a button’s pop-up menu. It returns true and displays the menu if the tool button has an associated menu. It returns false if there is no menu to display.</p>
<pre><code> <em>//ensure DropDownMenu Popup to pop up when button is clicked.</em> </code></pre>
<pre><code><strong>procedure</strong> TForm.<strong>dropDownMenuButton</strong>Click(Sender: TObject) ; </code></pre>
<pre><code><strong>begin</strong>   </code></pre>
<pre><code><em>//dropDownMenuButton is the name of the tool button   </em></code></pre>
<pre><code><em>//with Style set to tbsDropDown and assigned DropDownMenu</em>   dropDownMenuButton.CheckMenuDropdown; </code></pre>
<pre><code><strong>end</strong>;</code></pre>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://appbox.ir/?feed=rss2&#038;p=151</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Convert Roman Numbers to Arabic / Integer Numbers</title>
		<link>http://appbox.ir/?p=149</link>
		<comments>http://appbox.ir/?p=149#comments</comments>
		<pubDate>Mon, 02 Apr 2012 15:36:30 +0000</pubDate>
		<dc:creator>Mah6447</dc:creator>
				<category><![CDATA[سورس کد دلفی]]></category>

		<guid isPermaLink="false">http://appbox.ir/?p=149</guid>
		<description><![CDATA[The number system in most common use today is the Arabic system. The ten single-digit numerals, &#8220;0&#8243; through &#8220;9&#8243;, make up the symbols of our numbering system. An example of a different numeral system is the system of Roman numerals, which could represent all the numbers from 1 to 1,000,000 using only seven symbols: I [...]]]></description>
			<content:encoded><![CDATA[<blockquote dir="ltr"><p>The number system in most common use today is the Arabic system. The ten single-digit numerals, &#8220;0&#8243; through &#8220;9&#8243;, make up the symbols of our numbering system. An example of a different numeral system is the system of Roman numerals, which could represent all the numbers from 1 to 1,000,000 using only seven symbols: I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000. Example: 1973 (Arabic) is MCMLXXIII (Roman).</p>
<h3><span id="more-149"></span></h3>
<h3>Delphi MMIIX</h3>
<p>Converting from 2009 to MMIX is easy, an algorithm is provided in <a href="http://delphi.about.com/od/mathematics/a/baseconvert.htm">How to convert numbers from one base to another</a>.</p>
<p>How to convert a roman number to its arabic representation?</p>
<p>Here&#8217;s a Delphi function that does the job:</p>
<pre><code> <strong>function</strong> RomanToArabic(<strong>const</strong> romanNumber : <strong>string</strong>) : integer ; </code></pre>
<pre><code><strong>const</strong>   romanChars = 'IVXLCDMvxlcdm?!#' ;   decades : </code></pre>
<pre><code><strong>array</strong> [0..8] <strong>of integer</strong> = (0, 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000) ;   </code></pre>
<pre><code>OneFive : <strong>array</strong> [boolean] <strong>of byte</strong> = (1, 5) ; </code></pre>
<pre><code><strong>var</strong>   newValue, oldValue : integer ;   </code></pre>
<pre><code>cIdx, P : byte ; </code></pre>
<pre><code><strong>begin</strong>   result := 0;   </code></pre>
<pre><code>oldValue := 0 ;   </code></pre>
<pre><code><strong>for</strong> cIdx := Length(romanNumber) <strong>downto</strong> 1 <strong>do</strong>   </code></pre>
<pre><code><strong>begin</strong>     P := Succ(Pos(romanNumber[cIdx], romanChars)) ;     </code></pre>
<pre><code>newValue := OneFive[Odd(P)] * decades[P div 2] ;     </code></pre>
<pre><code><strong>if</strong> newValue = 0 <strong>then</strong>     </code></pre>
<pre><code><strong>begin</strong>   result := -1;       </code></pre>
<pre><code>Exit;     </code></pre>
<pre><code><strong>end</strong> ;     </code></pre>
<pre><code><strong>if</strong> newValue &lt; oldValue <strong>then</strong> </code></pre>
<pre><code>newValue := - newValue ;     </code></pre>
<pre><code>Inc(result, newValue) ;     </code></pre>
<pre><code>oldValue := newValue   </code></pre>
<pre><code><strong>end</strong> ; </code></pre>
<pre><code><strong>end</strong>; </code></pre>
<p>Note: RomanToArabic would return -1 if the &#8220;romanNumber&#8221; parameter is *not* a roman number (for example &#8220;MIXKIX&#8221; is not a roman number).</p>
<p>If Douglas Adams was in Rome at the time of writing his Guide, the definitive answer would probably be XLII <img src='http://appbox.ir/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://appbox.ir/?feed=rss2&#038;p=149</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What does #13#10 stand for, in Delphi code?</title>
		<link>http://appbox.ir/?p=146</link>
		<comments>http://appbox.ir/?p=146#comments</comments>
		<pubDate>Mon, 02 Apr 2012 15:34:30 +0000</pubDate>
		<dc:creator>Mah6447</dc:creator>
				<category><![CDATA[سورس کد دلفی]]></category>

		<guid isPermaLink="false">http://appbox.ir/?p=146</guid>
		<description><![CDATA[You&#8217;ve certainly seen &#8220;#13#10&#8243; many times in Delphi source code. If you are wondering what those characters stand for, here&#8217;s the answer: A control string is a sequence of one or more control characters, each of which consists of the # symbol followed by an unsigned integer constant from 0 to 255 (decimal or hexadecimal) [...]]]></description>
			<content:encoded><![CDATA[<blockquote dir="ltr"><p>You&#8217;ve certainly seen &#8220;#13#10&#8243; many times in Delphi source code. If you are wondering what those characters stand for, here&#8217;s the answer:</p>
<p>A control string is a sequence of one or more control characters, each of which consists of the # symbol followed by an unsigned integer constant from 0 to 255 (decimal or hexadecimal) and denotes the corresponding <a href="http://delphi.about.com/library/glossary/bldefascii.htm">ASCII</a> character.</p>
<p><span id="more-146"></span></p>
<p>When you want to, for example, assign a two-line string to a Caption property (of a TLabel control), you can use the following &#8220;code:&#8221;</p>
<p>Label1.Caption := &#8216;First line&#8217; + #13#10 + &#8216;Second line&#8217;;</p>
<p>The &#8220;#13#10&#8243; part represents a carriage-return + line-feed combination.</p>
<p>The &#8220;#13&#8243; is the ASCII equivalent to the CR (carriage return) value; #10 represents LF (line feed).</p>
<p>Two more interesting control characters are:<br />
#0 &#8211; NULL character and<br />
#9 &#8211; (horizontal) TAB</p>
<p>Note: here&#8217;s how to <a href="http://delphi.about.com/cs/adptips2002/a/bltip0902_3.htm">translate</a> a <a href="http://delphi.about.com/od/objectpascalide/l/blvkc.htm">virtual-key</a> to ASCII code.</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://appbox.ir/?feed=rss2&#038;p=146</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Universal solution to formatting values for SQL statements</title>
		<link>http://appbox.ir/?p=144</link>
		<comments>http://appbox.ir/?p=144#comments</comments>
		<pubDate>Mon, 02 Apr 2012 15:33:40 +0000</pubDate>
		<dc:creator>Mah6447</dc:creator>
				<category><![CDATA[سورس کد دلفی]]></category>

		<guid isPermaLink="false">http://appbox.ir/?p=144</guid>
		<description><![CDATA[When working on database applications using dbGO (ADO) over SQL Server or MS Access, you might be having troubles formatting the SQL string expression used in various INSERT, UPDATE or SELECT statements. Here are some issues: 1. Depending on locale international settings date values, for example, when transformed to string values might not be formatted properly. Delphi [...]]]></description>
			<content:encoded><![CDATA[<blockquote dir="ltr"><p>When working on database applications using dbGO (ADO) over SQL Server or MS Access, you might be having troubles formatting the SQL string expression used in various INSERT, UPDATE or SELECT statements.</p>
<p><span id="more-144"></span></p>
<p>Here are some issues:</p>
<p>1. Depending on <a href="http://delphi.about.com/cs/adptips1999/a/bltip1299_3.htm">locale international settings</a> date values, for example, when transformed to string values might not be formatted properly. Delphi RTL&#8217;s function <a href="http://delphi.about.com/library/rtl/blrtlDateTimeToStr.htm">DateTimeToStr</a> might return a date value as &#8216;DD.MM.YYYY&#8217; while a valid SQL expression requires, for example (depending on the Windows international settings), &#8216;MM/DD/YYYY&#8217;.</p>
<p>2. When you need to &#8220;construct&#8221; a string value to a SQL statement string, you almost always need to use the <a href="http://delphi.about.com/library/rtl/blrtlQuotedStr.htm">QuotedStr</a> function (to &#8220;fix&#8221; the apostrophe &#8220;&#8216;&#8221; character issue).</p>
<p>3. Last (but not the least), SQL Server expects &#8220;.&#8221; (dot) for the decimal separator when formatting decimal values. Some countries use &#8220;,&#8221; (comma) for decimal separator. A simple FloatToStr will not do the trick.</p>
<p>With the above in mind, when you have many SQL expressions to create, a simple SQL like the following can turn into a nightmare (to create a valid INSERT statement):<br />
&#8216;INSERT INTO TableName (IntField, StringField) VALUES (&#8216; + IntToStr(2005) +&#8217;,'+ QuotedStr(&#8216;delphi.about.com&#8217;)+&#8217;)';</p>
<p>This tends to be even worse when date and money fields and involved.</p>
<p>The TSQLF (SQL Format) &#8220;helper&#8221; class With the help of the TSQLF class (exposing only <a href="http://delphi.about.com/od/objectpascalide/l/aa122998.htm">class methods</a>), the above SQL expression can be turned into:</p>
<p>&#8216;INSERT INTO TableName (IntField, StringField) VALUES (&#8216; + TSQLF.Int(2005) + &#8216;,&#8217;+ TSQLF.Text(&#8216;delphi.about.com&#8217;) + &#8216;)&#8217;;</p>
<p>And if it turns that you have to correct the way, for example, QuotedStr function operates you only need to change the code in one place (implementation of the TSQLF.Text method).</p>
<p>Here&#8217;s the full source of the TSQLF class (unit), taking local settings into account:</p>
<p>~~~~~~~~~~~~~~~~~~~~~~~~~<br />
unit SQLFormat;</p>
<p>interface</p>
<p>uses Windows, SysUtils, StrUtils;</p>
<p>type<br />
TSQLF = class<br />
public<br />
class function Date(const Date : TDateTime) : string;<br />
class function Money(const Money : Currency) : string;<br />
class function Int(const Int : integer) : string;<br />
class function Text(const str : string) : string;<br />
end;</p>
<p>implementation</p>
<p>var fs : TFormatSettings;</p>
<p>class function TSQLF.Date(const Date: TDateTime): string;<br />
var<br />
Year, Month, Day : Word;<br />
begin<br />
DecodeDate(Date, Year, Month, Day) ;</p>
<p>Result := QuotedStr(IntToStr(Year) + &#8216;-&#8217; + IntToStr(Month) + &#8216;-&#8217; + IntToStr(Day)) ;<br />
end;</p>
<p>class function TSQLF.Int(const Int: integer): string;<br />
begin<br />
Result := IntToStr(Int) ;<br />
end;</p>
<p>class function TSQLF.Money(const Money: Currency): string;<br />
var<br />
decSep : char;<br />
begin<br />
decSep := fs.DecimalSeparator;<br />
fs.DecimalSeparator := &#8216;.&#8217;;</p>
<p>Result := CurrToStr(Money,fs) ;</p>
<p>fs.DecimalSeparator := decSep;<br />
end;</p>
<p>class function TSQLF.Text(const str: string): string;<br />
begin<br />
result := QuotedStr(str) ;<br />
end;</p>
<p>initialization<br />
GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, fs) ;<br />
finalization<br />
//nothing special in the finalization</p>
<p>end.<br />
~~~~~~~~~~~~~~~~~~~~~~~~~</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://appbox.ir/?feed=rss2&#038;p=144</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The role of the &#8220;AOwner&#8221; parameter in the Create constructor</title>
		<link>http://appbox.ir/?p=142</link>
		<comments>http://appbox.ir/?p=142#comments</comments>
		<pubDate>Mon, 02 Apr 2012 15:32:46 +0000</pubDate>
		<dc:creator>Mah6447</dc:creator>
				<category><![CDATA[سورس کد دلفی]]></category>

		<guid isPermaLink="false">http://appbox.ir/?p=142</guid>
		<description><![CDATA[Whenever memory is allocated for an object, it must eventually be freed (or you&#8217;ll produce amemory leak). But with components, there is often little mention of the need to do this. Delphi must be destroying the objects, but how does it know when we’re through with them? Here&#8217;s a hint. TComponent declares the Create constructor [...]]]></description>
			<content:encoded><![CDATA[<blockquote dir="ltr"><p>Whenever memory is allocated for an object, it must eventually be freed (or you&#8217;ll produce a<a href="http://delphi.about.com/od/oopindelphi/a/memoryleak.htm">memory leak</a>). But with components, there is often little mention of the need to do this. Delphi must be destroying the objects, but how does it know when we’re through with them?</p>
<p><span id="more-142"></span></p>
<p>Here&#8217;s a hint. TComponent declares the Create constructor as follows:</p>
<p>constructor Create(AOwner: TComponent) ; virtual;</p>
<p>The answer is in the &#8220;AOwner&#8221; parameter to the Create constructor. The Create constructor is virtual (and is a <a href="http://delphi.about.com/od/objectpascalide/a/classmethods.htm">class method</a>).</p>
<p>Every component that resides on the palette inherits this constructor. Inside the constructor, Delphi creates the component, and if an owner (hint: <a href="http://delphi.about.com/od/objectpascalide/l/aa031301a.htm">Owner vs. Parent</a>) is specified (if AOwner is not nil), Delphi adds the newly-created component to the Owner&#8217;s internal owned component list.</p>
<p>This internal list is consulted by Delphi for three important events that can affect an owner (e.g., any TComponent descendant):</p>
<ul>
<li><strong>A new component is created and added to the internal component list.</strong> When this happens, every component on the internal list is notified by calling the virtual method, Notification, passing a reference to the new component and specifying opInsert as the Operation. The Notification method is critical to support component linking.</li>
<li><strong>A new component is destroyed and removed from the internal component list.</strong> When this happens, every component on the internal list is notified by calling the virtual method, Notification, passing a reference to the new component and specifying opRemove as the Operation.</li>
<li><strong>The owner component is destroyed.</strong> When this happens, every owned component on the internal list is destroyed (and any components owned by these components are also recursively destroyed). That’s why you don’t need to explicitly destroy components that are owned, and this is part of what makes programming in Delphi so easy.</li>
</ul>
<p>The role of the AOwner parameter is even more important when you are <a href="http://delphi.about.com/od/kbcurt/a/dynamiccreation.htm">creating Delphi components at run-time</a>. If you are <a href="http://delphi.about.com/od/beginners/l/aa071503a.htm">creating forms dynamically</a> the AOwner can be set to <a href="http://delphi.about.com/od/adptips2005/qt/nilselfapp.htm">nil, Self or Application</a>.</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://appbox.ir/?feed=rss2&#038;p=142</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

