I did not win
Lately, all of my focus has been on the Olympics so things have been a little quiet. In my last post I talked about an ASP.NET Random Number Generator created with BDS 2006. Believe it or not, it is surprisingly simple to get something like this up and running. I have worked on a number of ASP.NET applications developed with BDS 2006, in both the internally and externally hosted environments. Typically, any SNAFUs that I have encountered (excluding code typos and incorrectness) have been due to server configuration issues and not a problem with the tool used to create the application.
To get the Random Number Generator running I first needed a Virtual Directory on the server hosting the application. I started out with a new ASP.NET Web Application and saved it with the same name as the application name for the Virtual Directory (by default it the name of the virtual directory).

I then created a ‘form’ and decorated it with a few items from the Web Controls section of the Tool Palette.

With the layout of the form in place a few lines of code were needed to get things going (I created a few functions of my own for dealing with input):
function TWebForm1.IsNumeric(str: string): Boolean;
var
code: integer;
v: integer;
begin
Val(str,v,code);
Result:= (code = 0);
end;
function TWebForm1.ToInteger(str: string): Integer;
var
code: integer;
v: integer;
begin
Val(str,v,code);
if (code = 0) then
Result:= v
else
Result:= 0;
end;
procedure TWebForm1.txtCount_TextChanged(sender: System.Object; e: System.EventArgs);
begin
if Not(IsNumeric(TextBox(Sender).Text)) then
begin
TextBox(Sender).ForeColor:= Color.Red;
TextBox(Sender).Font.Bold:= True;
TextBox(Sender).Text:= '#ERROR#';
end
else
begin
TextBox(Sender).ForeColor:= Color.Black;
TextBox(Sender).Font.Bold:= False;
end;
end;
procedure TWebForm1.btnGenerate_Click(sender: System.Object; e: System.EventArgs);
var
i, y: Integer;
z: single;
lowerbound, upperbound, counter: integer;
intRnd: Single;
// mylist: System.Collections.Hashtable;
mylist: System.Collections.SortedList;
st: System.Text.StringBuilder;
DictEntry: DictionaryEntry;
Enumerator: IEnumerator;
begin
// mylist:= Hashtable.Create;
myList:= SortedList.Create;
lbNumbers.Items.Clear;
lbStats.Items.Clear;
upperbound:= ToInteger(txtUpperBound.Text);
lowerbound:= ToInteger(txtLowerBound.Text);
counter:= ToInteger(txtCount.Text);
if (IsNumeric(txtUpperBound.Text) and
IsNumeric(txtLowerBound.Text) and
IsNumeric(txtCount.Text)) then
begin
Randomize;
For i := 1 To counter do
begin
intRnd := Int((upperbound - lowerbound + 1) * Random + lowerbound);
// HashTable for statistics
if Not mylist.ContainsKey(intRnd.ToString('#,###')) then
mylist.Add(intRnd.ToString('#,###'),'1')
else
begin
y:= ToInteger(mylist.Item[intRnd.ToString].ToString) + 1;
mylist.Item[intRnd.ToString]:= y.ToString('#,###');
end;
if (chkDupes.Checked and
(lbNumbers.Items.FindByText(intRnd.ToString('#,###')) = Nil))
or Not(chkDupes.Checked) then
lbNumbers.Items.Add(intRnd.ToString('#,###'));
end;
// enumerate hash for statistical output
Enumerator := Mylist.GetEnumerator;
st:= StringBuilder.Create;
while Enumerator.MoveNext do
begin
st.Remove(0,st.Length);
DictEntry := DictionaryEntry(Enumerator.Current);
z:= (ToInteger(DictEntry.Value.ToString)/counter * 100);
st.Append(z.ToString('00.00') + '% - ');
st.Append(DictEntry.Key.ToString + ' - (');
st.Append(DictEntry.Value.ToString + ' entries)');
lbStats.Items.Add(st.ToString);
end;
end;
end;
That’s pretty much all the code that is needed. In order to ‘manually publish’ the application, after testing the code and ensuring the application functions as desired, copy the .DLL from application’s Bin directory and the form’s .aspx file to the hosting site (if you do not have a web.config file on the published site you will need to copy that file as well).
With
BDS 2006 you can get this up and running rather quickly, I think it takes longer to write this post; unfortunately it didn’t serve up any winning numbers for me…….
CursorText
Looking back on a post made in response to a question about changing a cursor to text, I decided to change the code around a little for use in a local utility. The idea is to display ‘text’ as a cursor instead of an ‘image’. I toyed with passing a TIcon (a cursor and icon are the same structure) back as a back as a function result and passing a preexisting TIcon as a parameter. After coming up with an overloaded function, passing it back ‘felt better’. However, I prefer that the ‘cursor’ be created and destroyed outside of the function.
A function is as follows:
function TForm1.CreateCursorText(thecursor: TIcon; const cursortext: string;
fontcolor: TColor; fontsize: Integer; hs: TPoint): Integer;
var
iconwidth,
iconheight : integer;
bmpColor: TBitmap;
r: TRect;
iconinfo: TIconInfo;
begin
bmpColor:= TBitmap.Create;
try
iconwidth:= GetSystemMetrics(SM_CXCURSOR);
iconheight:= GetSystemMetrics(SM_CYCURSOR);
r.Top:= 0;
r.Left:= 0;
r.Bottom:= iconheight;
r.Right:= iconwidth;
bmpColor.Height:= iconheight;
bmpColor.Width:= iconwidth;
bmpColor.Canvas.Brush.Color:= clBlack;
bmpColor.Canvas.FillRect(r);
bmpColor.Canvas.Font.Size:= fontsize;
bmpColor.Canvas.Font.Color:= fontcolor;
bmpColor.Canvas.TextOut(2,2,cursortext);
bmpColor.TransparentColor:= clBlack;
iconinfo.fIcon:= False;
iconinfo.xHotspot:= hs.X;
iconinfo.yHotspot:= hs.Y;
iconinfo.hbmMask:= bmpColor.MaskHandle;
iconinfo.hbmColor:= bmpColor.Handle;
thecursor.Handle:= CreateIconIndirect(iconinfo);
DeleteObject(iconinfo.hbmColor);
Result:= 1;
finally
bmpColor.Free;
end;
end;
An example of how to use it would be something like:
const
crMyCursor = 5;
var
aicon: TIcon;
oldcursor: TCursor;
hotspot: TPoint;
begin
aicon:= TIcon.Create;
try
hotspot.X:= 3;
hotspot.Y:= 2;
CreateCursorText(aicon, edit1.Text, clBlue,8,hotspot);
oldcursor:= Screen.Cursor;
Screen.Cursors[crMyCursor]:= aicon.Handle;
Screen.Cursor:= crMyCursor;
//do something
finally
Screen.Cursor:= oldcursor;
aicon.Free;
end;
end;
A Few Good Shows
I have never been a television or movie buff. A vast majority of my time is spent in front of a computer. A typical day finds me with about 13 hours in front of ye ole' monitor, followed up by some technical reading (two of my recent selections are here and here). This has been going on for so long that I haven’t a clue about much primetime television. If I am watching television it is typically sports, TLC, Discovery or the History channel. (TechTV used to be there until G4Tv completely killed it) Besides the Revolution, Patriots, Red Sox and the morning news it is rare for me to have a ‘must see’ television show.
I do have shows that I enjoy a lot (Dirty Jobs), but none I can say that cause me to drop everything. Someone did recently tell me to watch The Office. I caught an episode last week and though it was hilarious. The jury is still out though....
Over the past few months I must say I finally found a 'must see' --Criminal Minds. This show has me hooked and I can now say that there is a show that without a doubt I’d turn of the computer for. Wednesday nights now are on my calendar. With my luck, they’ll end up canceling it, which, well, would piss me off. Remember The 4400?