PDA

Click to See Complete Forum and Search --> : Tip of the day - make it shorter?


Madboy
Jun 16th, 2004, 02:51 PM
Its in Delphi, the source was initially converted from VB6, but with modifications:

procedure TfrmTipOfTheDay.RandomizeTips;
var
sPossible: array[0..6] of string;
iTemp: integer;
begin
//Possible tips
sPossible[0]:= 'You can change the background colour of each individual document by selecting the Format, Background Colour menu.';
sPossible[1]:= 'You can quickly close all open documents by using the Window, Close All menu.';
sPossible[2]:= 'You can goto a certain line number by clicking Edit, Goto.' + #13#10 + #13#10 + 'You can then goto the beginning or end of the document as well as a sepcified line.';
sPossible[3]:= 'You can save time browsing through the menus for actions such as Cut, Copy, Paste etc when you can use the corresponding shortcut key.' + #13#10 + #13#10 + 'For example in the Edit menu Ctrl+V is shown next to Paste, so if you click Ctrl+V it will paste text from the clipboard into your document.';
sPossible[4]:= 'You can hide Super Editor in the system tray and use it when you need to.' + #13#10 + #13#10 + 'Simply right click the Super Editor icon from the system tray to show a menu of options.' + #13#10 + #13#10 + 'You can disable the Super Editor icon from showing in the system tray by unselecting it from the Options window.';
sPossible[5]:= 'When saving your document the document colour is not saved with the file.' + #13#10 + #13#10 + 'Super Editor does however store text formatting such as colour, bold, alginment etc depending on the saved format.' + #13#10 + #13#10 + 'For help on saving your documents in different file types please see the Help file.';
sPossible[6]:= 'You can stop the Tip of the day from showing when Super Editor loads.' + #13#10 + #13#10 + 'To do this goto View, Options and unselect the Show tip of the day at startup checkbox.' + #13#10 + #13#10 + 'When Tip of the day is turned off you can still view it by clicking Help, Tip of the day.';
//Randomize tips
Randomize;
repeat
iTemp:= Random(7);
Until iLast <> iTemp;
iLast:= iTemp;
lblMain.Caption:= sPossible[iTemp];
//Show tip number
if lblMain.Caption = 'You can change the background colour of each individual document by selecting the Format, Background Colour menu.' then
lblTipNumber.Caption:= 'Tip 1 of 8'
else if lblMain.Caption = 'You can quickly close all open documents by using the Window, Close All menu.' then
lblTipNumber.Caption:= 'Tip 2 of 8'
else if lblMain.Caption = 'You can quickly close all open documents by using the Window, Close All menu.' then
lblTipNumber.Caption:= 'Tip 3 of 8'
else if lblMain.Caption = 'You can goto a certain line number by clicking Edit, Goto.' + #13#10 + #13#10 + 'You can then goto the beginning or end of the document as well as a sepcified line.' then
lblTipNumber.Caption:= 'Tip 4 of 8'
else if lblMain.Caption = 'You can save time browsing through the menus for actions such as Cut, Copy, Paste etc when you can use the corresponding shortcut key.' + #13#10 + #13#10 + 'For example in the Edit menu Ctrl+V is shown next to Paste, so if you click Ctrl+V it will paste text from the clipboard into your document.' then
lblTipNumber.Caption:= 'Tip 5 of 8'
else if lblMain.Caption = 'You can hide Super Editor in the system tray and use it when you need to.' + #13#10 + #13#10 + 'Simply right click the Super Editor icon from the system tray to show a menu of options.' + #13#10 + #13#10 + 'You can disable the Super Editor icon from showing in the system tray by unselecting it from the Options window.' then
lblTipNumber.Caption:= 'Tip 6 of 8'
else if lblMain.Caption = 'When saving your document the document colour is not saved with the file.' + #13#10 + #13#10 + 'Super Editor does however store text formatting such as colour, bold, alginment etc depending on the saved format.' + #13#10 + #13#10 + 'For help on saving your documents in different file types please see the Help file.' then
lblTipNumber.Caption:= 'Tip 7 of 8'
else if lblMain.Caption = 'You can stop the Tip of the day from showing when Super Editor loads.' + #13#10 + #13#10 + 'To do this goto View, Options and unselect the Show tip of the day at startup checkbox.' + #13#10 + #13#10 + 'When Tip of the day is turned off you can still view it by clicking Help, Tip of the day.' then
lblTipNumber.Caption:= 'Tip 8 of 8'
end;

Instead of me comparing the current caption to displaying the current Tip number, im sure there is a way to check by the tip value. For example, what i had in mind was:

If sPossible = [0] Then
lblTipNumber.Caption:= 'Tip 1 of 8'

But i run into problems, appreciate if anyone can give maybe not code, but advice on what to do?

Cheers

si_the_geek
Jun 17th, 2004, 05:22 AM
Call me crazy, but shouldn't the entire section "//Show tip number" just be simply this:

lblTipNumber.Caption:= 'Tip ' + (iTemp+1) + ' of 8'
end;


or am I missing something?

CORONA BEER
Jun 17th, 2004, 06:19 AM
damn that might work, but i already wrote this for him


procedure TfrmTipOfTheDay.RandomizeTips;
var
Form1: TForm1;
Button1: TButton;
Edit1: TEdit;
tiplist: tstringlist;
i1: integer;
i2: integer;
begin
//Possible tips
tiplist := tstringlist.Create;
tiplist.Add('You can change the background colour of each individual document by selecting the Format, Background Colour menu.');
tiplist.Add('You can quickly close all open documents by using the Window, Close All menu.');
tiplist.Add('You can goto a certain line number by clicking Edit, Goto.' + #13#10 + #13#10 + 'You can then goto the beginning or end of the document as well as a sepcified line.');
tiplist.Add('You can save time browsing through the menus for actions such as Cut, Copy, Paste etc when you can use the corresponding shortcut key.' + #13#10 + #13#10 + 'For example in the Edit menu Ctrl+V is shown next to Paste, so if you click Ctrl+V it will paste text from the clipboard into your document.');
tiplist.Add('You can hide Super Editor in the system tray and use it when you need to.' + #13#10 + #13#10 + 'Simply right click the Super Editor icon from the system tray to show a menu of options.' + #13#10 + #13#10 + 'You can disable the Super Editor icon from showing in the system tray by unselecting it from the Options window.');
tiplist.Add('When saving your document the document colour is not saved with the file.' + #13#10 + #13#10 + 'Super Editor does however store text formatting such as colour, bold, alginment etc depending on the saved format.' + #13#10 + #13#10 + 'For help on saving your documents in different file types please see the Help file.');
tiplist.Add('You can stop the Tip of the day from showing when Super Editor loads.' + #13#10 + #13#10 + 'To do this goto View, Options and unselect the Show tip of the day at startup checkbox.' + #13#10 + #13#10 + 'When Tip of the day is turned off you can still view it by clicking Help, Tip of the day.');
//Randomize tips
Randomize;
i1 := tiplist.Count;
i2 := Random(i1);
//Show tip number
if i2 = 0
then
lblTipNumber.Caption:= 'Tip ' +inttostr(i2 + 1) + ' of ' +inttostr(i1);
if i2 = 1
then
lblTipNumber.Caption:= 'Tip ' +inttostr(i2 + 1) + ' of ' +inttostr(i1);
if i2 = 2
then
lblTipNumber.Caption:= 'Tip ' +inttostr(i2 + 1) + ' of ' +inttostr(i1);
if i2 = 3
then
lblTipNumber.Caption:= 'Tip ' +inttostr(i2 + 1) + ' of ' +inttostr(i1);
if i2 = 4
then
lblTipNumber.Caption:= 'Tip ' +inttostr(i2 + 1) + ' of ' +inttostr(i1);
if i2 = 5
then
lblTipNumber.Caption:= 'Tip ' +inttostr(i2 + 1) + ' of ' +inttostr(i1);
if i2 = 6
then
lblTipNumber.Caption:= 'Tip ' +inttostr(i2 + 1) + ' of ' +inttostr(i1);
//Show tip
showmessage(tiplist.Strings[i2]);
lblMain.Caption:= tiplst.Strings[i2];
end;
end.

si_the_geek
Jun 17th, 2004, 06:58 AM
That looks much better... I'd still modify the tip number bit so you can add new tips without any additional re-coding (as it is you would need to add extra If's etc for each new tip):

//Show tip number
edit1.Text:= 'Tip ' + (i2 + 1) + ' of ' + i1;
lblTipNumber.Caption:= edit1.Text

oh, and the variable i3 isn't used.

CORONA BEER
Jun 17th, 2004, 07:09 AM
Originally posted by si_the_geek


//Show tip number
edit1.Text:= 'Tip ' + (i2 + 1) + ' of ' + i1;
lblTipNumber.Caption:= edit1.Text

oh, and the variable i3 isn't used.
good idea :D i forgot about i3 i'll remove

Madboy
Jun 17th, 2004, 11:46 AM
Ok thanks si mate , that was something like what i was after;) :blush:

Madboy
Jun 22nd, 2004, 11:16 AM
Originally posted by si_the_geek
Call me crazy, but shouldn't the entire section "//Show tip number" just be simply this:

lblTipNumber.Caption:= 'Tip ' + (iTemp+1) + ' of 8'
end;


or am I missing something?

It would actually be:

lblTipNumber.Caption:= 'Tip ' + intTostr((iTemp+1)) + ' of 8'
:D

si_the_geek
Jun 22nd, 2004, 11:19 AM
:lol:

I've never used Delphi so I'll believe you!

Madboy
Jun 22nd, 2004, 02:46 PM
Just as well;)

I though id post it incase anybody tried to simulate this in VB and came along problems:afrog:

Madboy
May 26th, 2005, 06:36 AM
Does anybody know a way to generate a random string with less code? Say if i had:

abc
def
ghi

and i pressed generate, it would show a random one of them?

Thanks

Hack
May 26th, 2005, 01:29 PM
Its not perfect, but it should get you started.Private Sub Command1_Click()
Dim lngRandemString As Long
Dim strString As String
Dim arrString() As String
strString = "abc def ghi"
arrString = Split(strString, " ")
Randomize
lngRandemString = (2 * Rnd)
Text1.Text = arrString(lngRandemString)
End Sub

si_the_geek
May 26th, 2005, 01:41 PM
If you still want Delphi - The way I see it the code below (basically the same as above) is pretty well optimised - the longest part of the code is setting the strings. The only possible improvement would be to store them in different type of array (depending on availability), or a single string as in Hacks example.
procedure TfrmTipOfTheDay.RandomizeTips;
var
tiplist: tstringlist;
i1: integer;
i2: integer;
begin
//Possible tips
tiplist := tstringlist.Create;
tiplist.Add('abc');
tiplist.Add('def');
tiplist.Add('ghi');
//Randomize tips
Randomize;
i1 := tiplist.Count;
i2 := Random(i1);
//Show tip number
lblTipNumber.Caption:= 'Tip ' + intTostr((iTemp+1)) + ' of ' +inttostr(i1);
//Show tip
showmessage(tiplist.Strings[i2]);
lblMain.Caption:= tiplst.Strings[i2];
end;
end.
you will probably want to change the "Show tip" section, or possibly make it a function which accepts the strings and returns the selected one.

sciguyryan
May 26th, 2005, 01:43 PM
Does anybody know a way to generate a random string with less code? Say if i had:

abc
def
ghi

and i pressed generate, it would show a random one of them?

Thanks

How about this:


Private Function RandomElement(strArray() As String) As String
Dim intUpperLimit As Integer
Dim intLowerLimit As Integer
intUpperLimit = UBound(strArray)
intLowerLimit = LBound(strArray)
Randomize
RandomElement = strArray(Int((intUpperLimit - intLowerLimit) * Rnd + intLowerLimit))
End Function


Example:


Private Sub Form_Load()
Dim strArray(0 To 3) As String
strArray(0) = "abc"
strArray(1) = "def"
strArray(2) = "ghi"
MsgBox RandomElement(strArray)
End Sub


Hows that?

Cheers,

RyaNJ

Madboy
May 26th, 2005, 03:07 PM
Looks good thanks pal, will try it later :afrog:

sciguyryan
May 26th, 2005, 03:17 PM
Looks good thanks pal, will try it later :afrog:

Great - just report back and tell me if it works :)

Cheers,

RyanJ