VB/Web Help |
Author: |
Message: |
DragonX
Full Member
Posts: 226 Reputation: 10
40 / / –
Joined: Aug 2005
|
O.P. VB/Web Help
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 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.
|
|
10-04-2005 07:11 AM |
|
|
rav0
Veteran Member
i have an avatar
Posts: 1419 Reputation: 29
35 / /
Joined: Aug 2003
|
RE: VB/Web Help
I'm no coder, but AFAIK you pass the URI to the shell (or whatever). Same way you run an exe, but you give the URI instead of the path to the exe.
| |
(\ /)
(O.o)
(> <)
This is Bunny. Copy Bunny into your signature to help him on his way to world domination
|
|
10-04-2005 09:44 AM |
|
|
brian
Senior Member
Posts: 819 Reputation: 43
– / / –
Joined: Sep 2004
|
|
10-04-2005 10:22 AM |
|
|
CookieRevised
Elite Member
Posts: 15517 Reputation: 173
– / /
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.
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
This post was edited on 10-04-2005 at 03:58 PM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
|
|
10-04-2005 03:52 PM |
|
|
Stigmata
Veteran Member
Posts: 3520 Reputation: 45
21 / /
Joined: Jul 2003
|
RE: VB/Web Help
just improving on cookies code
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 Command1_Click()
Dim URLString As String
URLString = "http://www.google.com/search?q=" & Encode(Text1.Text)
ShellExecute 0&, vbNullString, URLString, vbNullString, vbNullString, vbNormalNoFocus
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
Command1_Click
End If
End Sub
Public Function Encode(ByVal s As String) As String
Dim sChar As String, sAsc As String, sHex As String, sName As String
Dim I As Integer
For I = 1 To Len(s)
sChar = Mid$(s, I, 1)
sAsc = Asc(sChar)
If sAsc = 32 Then
sHex = "+"
Else
sHex = sChar
End If
sName = sName & sHex
Next I
Encode = sName
End Function
i quickly made a encode function that checks for a space(ascii 32) and then replaces it with a +.
Works perfectly
(in vb... )
This post was edited on 10-04-2005 at 04:19 PM by Stigmata.
|
|
10-04-2005 04:14 PM |
|
|
CookieRevised
Elite Member
Posts: 15517 Reputation: 173
– / /
Joined: Jul 2003
Status: Away
|
RE: VB/Web Help
this is totally as a sidenote:
Stigmata's extra encoding function is indeed what I meant with the extra checking/validating that needs to be done (although it misses many other special characters). Anyways, the Encode function as Stigmata posted it, can be made much more efficient and smaller
1) to begin with loose the sAsc variable. Great for explanation, but it is only used once and directly in the IF THEN ELSE, so no need for that.
2) No need for getting the ascii value of the character and comparing it to 32, if you can directly compare the character itself to a space (although, this is some clock ticks slower, the code is much shorter)
3) No need to slowly build a new string again when you're only replacing individual characters. You can directly replace them in the original string.
4) Loose the ByVal. ByVal's are very slow compared to ByRef's (with a ByVal, VB needs to create another instance of the variable). To overcome writing over the original variable you can directly manipulate the function's output string.
5) Longs work faster than Integers (in this case) so declare I as a long...
So we get:
code: Public Function Encode(s As String) As String
Dim I As Long
Encode = s
For I = 1 To Len(Encode)
If Mid$(Encode, I, 1) = " " Then
Mid$(Encode, I, 1) = "+"
End If
Next I
End Function
But hey, isn't that simply replacing every space with a "+"? Well yes, so... simply use VB's Replace function:
[code]Replace(URLString, " ", "+")
PS: nevertheless, you must make an "Encode" routine anyways, to encode your text into the proper characters and "escape codes" (forgot the correct name for them) so you can use them in URLs...
EDIT: Mike, you're late hihi
This post was edited on 10-04-2005 at 06:19 PM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
|
|
10-04-2005 04:26 PM |
|
|
Mike
Elite Member
Meet the Spam Family!
Posts: 2795 Reputation: 48
32 / /
Joined: Mar 2003
Status: Online
|
RE: VB/Web Help
quote: Originally posted by Stigmata
i quickly made a encode function that checks for a space(ascii 32) and then replaces it with a +.
You can easly do it with VB's Replace() function
code: Dim URLString As String
URLString = "http://www.google.com/search?q=" & Replace$(Text1.Text, " ", "+")
Edit: Bah, cookie beat me to it, as I was writing it...
But cookie's code is slower, because he didn't include an $ in his replace function, so VB thinks that it should be used as a variant
Strings are faster than variants, because, variants can be anything, so they use more memory (cookie, please correct me on this one )
This post was edited on 10-04-2005 at 04:36 PM by Mike.
|
|
10-04-2005 04:29 PM |
|
|
CookieRevised
Elite Member
Posts: 15517 Reputation: 173
– / /
Joined: Jul 2003
Status: Away
|
RE: RE: VB/Web Help
.-= A 'frrrrrrrituurrr' for Wacky =-.
|
|
10-04-2005 04:49 PM |
|
|
DragonX
Full Member
Posts: 226 Reputation: 10
40 / / –
Joined: Aug 2005
|
O.P. RE: VB/Web Help
Thx so much to all, u guys are the best!!
And yes, that is VB.NET.
quote: Originally posted by CookieRevised
code: Command1.Value = True
Becomes
code: cmdSearch.PerformClick()
In .NET (i think)
quote: Originally posted by CookieRevised
code: Dim URLString As String
URLString = "http://www.google.com/search?q=" & Replace$(Text1.Text, " ", "+")
What is the $ used for? I've never used it in a replace() before.
quote: Originally posted by CookieRevised
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
Must i use the API ? Did it that way anyways, for now
code: ShellExecute(0&, vbNullString, URLString, vbNullString, vbNullString, vbNormalNoFocus)
Converted in .NET
What is '0&' for ?
This post was edited on 10-04-2005 at 09:18 PM by DragonX.
|
|
10-04-2005 07:48 PM |
|
|
DragonX
Full Member
Posts: 226 Reputation: 10
40 / / –
Joined: Aug 2005
|
O.P. RE: VB/Web Help
Sorry for the double post, just thought it would be better this way
Ok, so here is my code
code: Public Class miniSearch
Inherits System.Windows.Forms.Form
Private URLString As String
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 txtSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSearch.Click
If txtSearch.Text = "Enter search words here" Then
txtSearch.Text = ""
End If
End Sub
Private Sub txtSearch_KeyPress(ByVal KeyAscii As Integer)
If KeyAscii = 13 Then
cmdSearch.PerformClick()
ElseIf KeyAscii = 27 Then
End
End If
End Sub
Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
URLString = "http://www.google.ca/search?q=" & Replace$(txtSearch.Text, " ", "+")
ShellExecute(0&, vbNullString, URLString, vbNullString, vbNullString, vbNormalNoFocus)
MsgBox(URLString)
End
End Sub
End Class
First off, the "cmdSearch.PerformClick()" doesn't work, in fact the whole KeyPress part doesnt' work. I tried sending my KeyPress into a msgBox and that doesn't even work, could it be because i don't ahve a hangle on it ?
Edit: OK, i tried with a handle "Handles txtSearch.KeyPress" and i get this error: "Method 'txtSearch_KeyPress' cannot handle Event 'KeyPress' because they do not have the same signature."
Edit2: Alright, after some digging, i found out the KeyPress was redefined in .NET. And to make it work, u need code that looks like this
code: Private Sub txtSearch_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSearch.KeyPress
Dim KeyAscii As Integer
KeyAscii = AscW(e.KeyChar) <-- This is the code to make it work
If KeyAscii = 13 Then
cmdSearch.PerformClick()
ElseIf KeyAscii = 27 Then
End
End If
End Sub
So that thing is solved
And second, it doesn't the launch my browser. Maybe i just overlooked something
Edit: Woohoo, i got it wo work by bypassing the API too Here's how i did it
code: System.Diagnostics.Process.Start(URLString)
Well all works now Once it'll all finalized and what not i'll post it here, if anyone wants it
This post was edited on 10-04-2005 at 09:16 PM by DragonX.
|
|
10-04-2005 07:52 PM |
|
|
Pages: (2):
« First
[ 1 ]
2
»
Last »
|
|
|