What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » VB/Web Help

VB/Web Help
Author: Message:
CookieRevised
Elite Member
*****

Avatar

Posts: 15517
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: VB/Web Help
quote:
Originally posted by DragonX
Ok, basically i'm makin an app that will take the string entered, convert it to a web adrs and then send that adrs to the default browser which will open that page. Now the thing i have no idea how to do is how to pass that adrs to the default browser.

Here's ascreenshot of it.
[Image: MiniSearch.jpg]

I was also wondering how to to get a keypress throught the txtBox
code:
Private Sub txtSearch_KeyPress(ByVal sender As Object, ByVal e As_
    System.Windows.Forms.KeyPressEventArgs)
        If key pressed is "return" Then
            call search
        End If
End Sub

Thx in advance for the help.
That isn't VB code! At first sight that is VB.NET code. VB.NET is not VB !!! Although they are similar, they are completely different languages...


---------


Anyways, for VB (not VB.NET, although it will be similar):

First tip: Don't call the search procedure inside that KeyPress event. You have a "search" button on your form for that. Use it... (otherwise you must write double code, one time in this textbox's KeyPress event and one time in the Click event of that button.)


Second tip: To open an URL you simply execute that URL like you execute any other program (aka you "Shell" it). The URL will then open in your default browser.

What brian linked to is how to use the Windows ShellExecute API to do this.

However, on that page there are a few things which aren't mentionned or are explaned not-so-correctly. eg: When you open a local HTML file, it will NOT be opened with your default browser! It will be opened with the associated program. This can be totally different than your default browser (eg: notepad). The default browser is only used when you have a command string which starts with the "http://" protocol string as it is your browser which is associated with that protocol and thus your browser is called to execute this 'command'. vb-helper is full of these kind of small, but important mistakes.

Also, to know how an API works, _never_ trust only on the explanation of such sites. Check and double check it in the msdn library instead! eg: on vb-helper they constantly speak of/use the handle of a window -hwnd- (as in the form's handle). This parameter can/must be null if you don't have a need for it. Don't set it to Me.hWnd as shown in the example if you want to open an URL as you most likely want the browser window to be independant of your program (aka: not setting it as a child window of your program). Again, this can be found in the msdn library...

Another note on this API and what is not said on vb-helper: If you pass strings in VB to Windows API's, the strings are converted to ANSI (=equivalent of ascii to keep it simple) and thus you can't pass unicode strings explicitly to API's (if you wanna do that you need to convert the unicode string first to unicode again, so VB converts the double unicode to normal unicode when it calls the API. Or, instead, you need to pass the pointer to the unicode string if you don't want to do that stupid unicode to unicode conversion.)

And again something they forget to mention, if you ant to use API's you first need to declare the API inside a module (note: a form is also a module. In fact, a form is a class module):
code:
Private Declare Function ShellExecute Lib "SHELL32" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long



Third tip: The ASCII code for the <enter> key is 13 (this can be looked up in any ascii table on the net or in any book). Use the textbox's keypress event to catch this ascii key and then simulate a click on the search button:
code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
        Command1.Value = True
    End If
End Sub
It is only inside the Click event of the command button that you handle, test, create the full URL string and execute it. No need to do this inside the KeyPress event of the textbox (of course you could add a validation routine when the focus of the textbox is lost or something (=see 'Validate' event and 'CausesValidation' property of the textbox)).


-----------------

Thus:

code:
Private Declare Function ShellExecute Lib "SHELL32" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
        Command1.Value = True
    End If
End Sub

Private Sub Command1_Click()
    Dim URLString As String
    URLString = "http://www.google.com/search?q=" & Text1.Text 
    ShellExecute 0&, vbNullString, URLString, vbNullString, vbNullString, vbNormalNoFocus
End Sub
Note that this VB code does not check on the validity of the text entered in the textbox. eg: it doesn't convert the special characters to entities.

eg: the texbox input "testing" AND "trashing" will be wrongly parsed as
http://www.google.com/search?q="testing" AND "trashing"
instead of:
http://www.google.com/search?q=%22testing%22+AND+%22trashing%22

This post was edited on 10-04-2005 at 03:58 PM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
10-04-2005 03:52 PM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »

Messages In This Thread
VB/Web Help - by DragonX on 10-04-2005 at 07:11 AM
RE: VB/Web Help - by rav0 on 10-04-2005 at 09:44 AM
RE: VB/Web Help - by brian on 10-04-2005 at 10:22 AM
RE: VB/Web Help - by CookieRevised on 10-04-2005 at 03:52 PM
RE: VB/Web Help - by Stigmata on 10-04-2005 at 04:14 PM
RE: VB/Web Help - by CookieRevised on 10-04-2005 at 04:26 PM
RE: VB/Web Help - by Mike on 10-04-2005 at 04:29 PM
RE: RE: VB/Web Help - by CookieRevised on 10-04-2005 at 04:49 PM
RE: VB/Web Help - by DragonX on 10-04-2005 at 07:48 PM
RE: VB/Web Help - by DragonX on 10-04-2005 at 07:52 PM
RE: VB/Web Help - by Ezra on 10-05-2005 at 02:28 PM
RE: RE: VB/Web Help - by CookieRevised on 10-05-2005 at 03:33 PM
RE: VB/Web Help - by YottabyteWizard on 10-05-2005 at 04:15 PM
RE: VB/Web Help - by segosa on 10-05-2005 at 07:19 PM
RE: RE: VB/Web Help - by CookieRevised on 10-05-2005 at 09:48 PM
RE: VB/Web Help - by Ezra on 10-05-2005 at 07:23 PM
RE: VB/Web Help - by YottabyteWizard on 10-05-2005 at 08:08 PM
RE: VB/Web Help - by DragonX on 10-05-2005 at 09:00 PM
RE: VB/Web Help - by YottabyteWizard on 10-06-2005 at 04:44 AM


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On