Results 1 to 26 of 26

Thread: Hyperlinks in VB

  1. #1

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205

    Hyperlinks in VB

    I just whipped this up, thought it may be of use to people. add this code to a module, and you have the full functionality of a hyperlink in VB. Does display changes for MouseOver etc. Pass the LinkUp the function you want to call for the particular textbox 'link' and you're away. Have fun.


    VB Code:
    1. 'Constants
    2. Private Const HYPERLINK_HIGHLIGHT As Long = vbYellow
    3. Private Const HYPERLINK_COLOUR As Long = vbBlue
    4.  
    5. 'Hyperlink code selection flag
    6. Private flgHyperLinkSelected As Boolean
    7.  
    8. 'Depress textbox hyperlink
    9. Public Sub LinkDown(txtLink As TextBox)
    10.     'Remove focus from text box
    11.     HideCaret txtLink.hwnd
    12.    
    13.     'Change label to selected colour
    14.     txtLink.ForeColor = HYPERLINK_HIGHLIGHT
    15.    
    16.     'Enable option selected flag
    17.     flgHyperLinkSelected = True
    18.    
    19.     'Remove focus from text box
    20.     HideCaret txtLink.hwnd
    21. End Sub
    22.  
    23. 'Highlight textbox hyperlink
    24. Public Sub LinkOver(txtLink As TextBox, X As Single, Y As Single)
    25.     'Show highlight image when mouse moves over link text
    26.     If Not (flgHyperLinkSelected) Then
    27.         With txtLink
    28.             If (X < 0) Or (Y < 0) Or (X > .Width) Or (Y > .Height) Then
    29.                 'Release picture handle
    30.                 ReleaseCapture
    31.                 'Unhighlight incident code
    32.                 txtLink.FontUnderline = True
    33.                 txtLink.ForeColor = HYPERLINK_COLOUR
    34.             Else
    35.                 'Capture label handle
    36.                 SetCapture .hwnd
    37.                 'Highlight incident code
    38.                 txtLink.FontUnderline = False
    39.                 txtLink.ForeColor = HYPERLINK_HIGHLIGHT
    40.             End If
    41.         End With
    42.     End If
    43.    
    44.     'Remove focus from text box
    45.     HideCaret txtLink.hwnd
    46. End Sub
    47.  
    48. 'Click textbox hyperlink
    49. Public Sub LinkUp(txtLink As TextBox, X As Single, Y As Single, objActionObj As Object, strActionFunc As String, Optional varArgs As Variant)
    50.     'Remove focus from text box
    51.     HideCaret txtLink.hwnd
    52.    
    53.     'Check mouse location
    54.     If (X > 0 And X < txtLink.Width) And (Y > 0 And Y < txtLink.Height) Then
    55.         'Call action function
    56.         CallByName objActionObj, strActionFunc, VbMethod, varArgs
    57.     End If
    58.    
    59.     'Disable selected flag
    60.     flgHyperLinkSelected = False
    61.    
    62.     'Release label handle
    63.     ReleaseCapture
    64.    
    65.     'Unhighlight incident code
    66.     txtLink.FontUnderline = True
    67.     txtLink.ForeColor = HYPERLINK_COLOUR
    68.  
    69.     'Remove focus from text box
    70.     HideCaret txtLink.hwnd
    71. End Sub



    To use:
    VB Code:
    1. Private Sub Text1_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
    2.     'Press link
    3.     Call LinkDown(Text1(Index))
    4. End Sub
    5.  
    6. Private Sub Text1_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
    7.     'Highlight link
    8.     Call LinkOver(Text1(Index), X, Y)
    9. End Sub
    10.  
    11. Private Sub Text1_MouseUp(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
    12.     'Call LinkUp when mouse up on textbox
    13.     Call LinkUp(Text1(Index), X, Y, frmLesson, "FunctionName", Index)
    14. End Sub
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  2. #2
    PowerPoster
    Join Date
    Aug 2000
    Location
    India
    Posts
    2,288
    jyotiraditya What rjlohan has done is to provide a mouse hover state for the hyperlink, with all the color changes that you see in a browser.

  3. #3

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Indeed I have.
    I imagine there was another post in this thread, right?
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  4. #4
    PowerPoster
    Join Date
    Aug 2000
    Location
    India
    Posts
    2,288
    Yes, there was a post which showed how to execute the hyperlink.

  5. #5
    Fanatic Member SkiNLaB's Avatar
    Join Date
    Jan 2002
    Location
    Sydney, Australia
    Posts
    747
    hey im using your code, but whats ReleaseCapture Meant to be? is it a sub that im missing?

  6. #6

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Apparently I forgot to add the API functions...

    ReleaseCapture and HideCaret are Windows API calls. USe the API viewer to find the full declarations.
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  7. #7
    Fanatic Member SkiNLaB's Avatar
    Join Date
    Jan 2002
    Location
    Sydney, Australia
    Posts
    747
    ah i c, got 'em

    ummm its good, although if u click in the textbox to type something it loses focus of the window and nothing happens, which makes it basically impossible to enter anything in the textbox, also if u want 2 edit the contents its impossible to click on the textbox (for editing) without it bringing up IE.

    so itd be good if it were a link only when the mouse was over the text and not over the white of the textbox.

    this sounds pretty impossible...

  8. #8

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Well, that's not what this code was designed for. You have to think about design issues too. I have code here to turn a textbox into a hyperlink. Have you ever seen a hyperlink which also acts as an input box?
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  9. #9
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    It would make much more since to use a label.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  10. #10

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    No it wouldn't, which is precisely why I didn't do that. A label doesn't have the requisite Mouse events available.
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  11. #11
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    My example :

    http://www.galahtech.com/forums/show...&threadid=1303

    Different forum though...

    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  12. #12

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Can't view it here - wanna give me a brief summary?
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  13. #13
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    I see, well in that case a picturebox would make much more sense Set the border to 0 etc... works just like a label except with all the hwnd stuff.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  14. #14

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    That's even more absurd than your last idea... do you have any reason for your comments, or just like to think you're right?

    What possible reason would using a picturebox to display text do? I can see no advantage in that.
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  15. #15
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Uhh why is it absurd? Whats absurd is using a textbox to display a URL... How many times have you seen a textbox used as a URL? URL's are usualy flat with no background and they dont give the user the urge to type inside of them

    For the very reason that the guy was confused casue he couldnt type inside the textbox.... thats why I am saying to use a picturebox... Learn to take constructive critism you'll get alot further in life and might even have a friend or 2.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  16. #16
    Fanatic Member SkiNLaB's Avatar
    Join Date
    Jan 2002
    Location
    Sydney, Australia
    Posts
    747
    i rekon a hyperlink in a textbox would be good, for instance i have a form with a sponsor's details and one of the fields is for a website which can be edited, so if the user wishes to visit the website, view the details click on the text in the textbox, and done. if he wishes to edit the website address, click on the white inside the textbox and it is editable, where he then presses save. i guess ill have a button next to the textbox saying Launch or similar.

  17. #17
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    You fail to miss the point that the text inside the textbox is not editible.. Far as i can tell the textbox is merely a holder for the link... and serves no other purpose.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  18. #18

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Arc, you haven't provided any 'constructive criticism'. All you've done is poonce about throwing around stupid comments with no justification.

    Your stupid posts are just one of the many reasons this site continues to suck arse.
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  19. #19
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Wow...LOL. You really need help dude.

    What is so stupid about my critism other then the fact your mind is totaly closed to admitting that your textbox hyperlink needs to be changed to more resemble a Hyperlink rather than a textbox?


    You see if you use a picturebox with no border it will look like a link on the form rather than a textbox!


    Lol, whatever dude...suite yerself.



    P.S. just becasue you may not agree with my idea is really no reason to fly off the handle and call me stupid and say these forums suck cause of stupid people like me lol...going a little overboard there aint we? I feel as though you may have issues that need to be dealt with
    Last edited by Arc; Jan 30th, 2003 at 02:23 AM.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  20. #20

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    So set the textbox properties: .BorderStyle to None, and set the .Appearance to Flat. Bet Microsoft are real glad a twit like you is proudly displaying his 'qualifications'...
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  21. #21
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    WEll another advantage of the picture box is that you can have Faded text..etc.. You know like thiscouldbealink Overall the picture box offers many more possibilities than a text box if you choose to be creative.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  22. #22

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    And that requires creating pictures for every link. The textbox option is simpler. If someone really wants rainbow text, which is an utterly horrible design, they can make all the pictures they like.

    This doesn't make a picturebox a better option though, does it?
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  23. #23
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Well how do you figure that requires pictures? You simply use some API and paint the text on the fly... there is no need for pictures. You could have the user pick 2 colors for each part...Link,Rollover and visited... In fact this wouldnt be a bad idea for an ocx. If you wanted to get real fancy you could even do Shadow/3d text... or Marquee style links that scroll..the skies the limit.

    Granted the code is a bit complex, but if you're familiar with

    Public Declare Function GradientFillRect Lib "msimg32"Alias " GradientFill" (ByVal hdc As Long, _
    pVertex As TRIVERTEX, _
    ByVal dwNumVertex As Long, _
    pMesh As GRADIENT_RECT, _
    ByVal dwNumMesh As Long, _
    ByVal dwMode As Long) As Long

    Then you should be able to accomplish it wihtout much difficulty.


    You may think it's ugly or whatever but i actually think it would be quite innovative and alot of people like Faded text. Too bad theres no way to do it on the web... or maybe there is using xml or something.../shrug
    Last edited by Arc; Jan 30th, 2003 at 04:03 AM.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  24. #24
    Fanatic Member SkiNLaB's Avatar
    Join Date
    Jan 2002
    Location
    Sydney, Australia
    Posts
    747
    Originally posted by Arc
    You fail to miss the point that the text inside the textbox is not editible.. Far as i can tell the textbox is merely a holder for the link... and serves no other purpose.
    no i didnt fail to miss that point, therein lies my problem.....

    and maybe one day if i make an editable hyperlink textbox ill post it here and shut u both up!

  25. #25
    Fanatic Member MikkyThomeon's Avatar
    Join Date
    Oct 2002
    Location
    At work...
    Posts
    648

    MSAccess

    Doesn't MSAcess have a thing in a grid where you can edit a field that is also hyperlink (eg where the column is defined as type 'hyperlink')??? You click on the link when viewing the contents of the table...

  26. #26
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    Someone posted this a while back and it seem pretty good.
    VB Code:
    1. Option Explicit
    2. Dim blnClicked As Boolean
    3.  
    4. Private Sub Label1_DragDrop(Source As Control, X As Single, Y As Single)
    5. '   Use this event instead of the click event
    6.     blnClicked = True
    7.     Me.Print "Clicked"
    8. End Sub
    9.  
    10. Private Sub Label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    11.     With Label1
    12.         .ForeColor = vbBlue
    13.         .Font.Underline = True
    14.         'path to hand cursor
    15.         .DragIcon = LoadPicture("C:\Program Files\Microsoft Visual Studio\Common\Graphics\Cursors\hand.cur")
    16.         .Drag vbBeginDrag
    17.     End With
    18. End Sub
    19.  
    20. Private Sub Label1_DragOver(Source As Control, X As Single, Y As Single, State As Integer)
    21.     If State = vbLeave Then
    22.         With Label1
    23.             .ForeColor = vbBlack
    24.             .FontUnderline = False
    25.             .Drag vbEndDrag
    26.         End With
    27.     End If
    28. End Sub

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