Results 1 to 15 of 15

Thread: Tip of the day - make it shorter?

  1. #1

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253

    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

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    Call me crazy, but shouldn't the entire section "//Show tip number" just be simply this:
    Code:
    lblTipNumber.Caption:= 'Tip ' + (iTemp+1) + ' of 8'
    end;
    or am I missing something?

  3. #3
    Lively Member CORONA BEER's Avatar
    Join Date
    May 2004
    Location
    THE INTERNETS!
    Posts
    82

    Smile

    damn that might work, but i already wrote this for him

    Code:
    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.
    Last edited by CORONA BEER; Jun 17th, 2004 at 07:33 AM.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    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):
    Code:
    //Show tip number
      edit1.Text:= 'Tip ' + (i2 + 1) + ' of ' + i1;
      lblTipNumber.Caption:= edit1.Text
    oh, and the variable i3 isn't used.

  5. #5
    Lively Member CORONA BEER's Avatar
    Join Date
    May 2004
    Location
    THE INTERNETS!
    Posts
    82
    Originally posted by si_the_geek

    Code:
    //Show tip number
      edit1.Text:= 'Tip ' + (i2 + 1) + ' of ' + i1;
      lblTipNumber.Caption:= edit1.Text
    oh, and the variable i3 isn't used.
    good idea i forgot about i3 i'll remove
    Last edited by CORONA BEER; Jun 17th, 2004 at 07:27 AM.

  6. #6

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Ok thanks si mate , that was something like what i was after

  7. #7

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Originally posted by si_the_geek
    Call me crazy, but shouldn't the entire section "//Show tip number" just be simply this:
    Code:
    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'

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974


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

  9. #9

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Just as well

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

  10. #10

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253

    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

  11. #11
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Tip of the day - make it shorter?

    Its not perfect, but it should get you started.
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim lngRandemString As Long
    3. Dim strString As String
    4. Dim arrString() As String
    5. strString = "abc def ghi"
    6. arrString = Split(strString, " ")
    7. Randomize
    8. lngRandemString = (2 * Rnd)
    9. Text1.Text = arrString(lngRandemString)
    10. End Sub

  12. #12
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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.

  13. #13
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    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:
    1. Private Function RandomElement(strArray() As String) As String
    2.     Dim intUpperLimit As Integer
    3.     Dim intLowerLimit As Integer
    4.     intUpperLimit = UBound(strArray)
    5.     intLowerLimit = LBound(strArray)
    6.     Randomize
    7.     RandomElement = strArray(Int((intUpperLimit - intLowerLimit) * Rnd + intLowerLimit))
    8. End Function

    Example:

    VB Code:
    1. Private Sub Form_Load()
    2.     Dim strArray(0 To 3) As String
    3.     strArray(0) = "abc"
    4.     strArray(1) = "def"
    5.     strArray(2) = "ghi"
    6.     MsgBox RandomElement(strArray)
    7. End Sub

    Hows that?

    Cheers,

    RyaNJ
    My Blog.

    Ryan Jones.

  14. #14

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253

    Re: Tip of the day - make it shorter?

    Looks good thanks pal, will try it later

  15. #15
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Tip of the day - make it shorter?

    Quote Originally Posted by Madboy
    Looks good thanks pal, will try it later
    Great - just report back and tell me if it works

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width