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.
I was also wondering how to to get a keypress throught the txtBoxcode:
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