Tip of the day - make it shorter?
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
Re: Tip of the day - make it shorter?
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
Re: Tip of the day - make it shorter?
Its not perfect, but it should get you started.
VB Code:
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
Re: Tip of the day - make it shorter?
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.
Code:
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.
Re: Tip of the day - make it shorter?
Quote:
Originally Posted by Madboy
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:
VB Code:
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:
VB Code:
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
Re: Tip of the day - make it shorter?
Looks good thanks pal, will try it later :afrog:
Re: Tip of the day - make it shorter?
Quote:
Originally Posted by Madboy
Looks good thanks pal, will try it later :afrog:
Great - just report back and tell me if it works :)
Cheers,
RyanJ