Simple application with opening webpage in VB - vb.net

I would like to create simple exe app in VB, which will open default browser with specified page. I have this code
Public Class Form1
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
System.Diagnostics.Process.Start("www.google.com")
End Sub End Class
If I click on the button I get error message:
System.ComponentModel.Win32Exception: System cannot find this file
Could someone help me where is the problem? Or is there any other approach how to open webpage?

"System cannot find this file" means that your system is not reading the URL as one and it's trying to get the resource at the local path: "www.google.com" which obviously doesn't exist. Check this link because the issue seems to be the same.

Here in an application I call Best Links is the process I use to view URL's.
It works by storing website URL links in a SQLite Database (DB).
The DB also stores the Site Name and the Last Visit Date.To view the site the user just clicks on the DB entry displayed in DataGridView. Screen Shot Below. And the code that opens the link in what ever your default browser is.
Private Sub dgvLinks_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvLinks.CellClick
selRow = e.RowIndex
If e.RowIndex = -1 Then
gvalertType = "4"
frmAlert.ShowDialog()
Exit Sub
End If
Dim row As DataGridViewRow = Me.dgvLinks.Rows(e.RowIndex)
If row.Cells(2).Value Is Nothing Then
gvalertType = "5"
frmAlert.ShowDialog()
Return
Exit Sub
ElseIf gvTxType = "View" Then
webPAGE = row.Cells(2).Value.ToString()
siteID = CInt(row.Cells(0).Value.ToString())
UpdateSiteData()
Process.Start(webPAGE)
Data Grid View
If you would like the complete code or and exe file let me know and I will add it to my GitHub repository code will be a ZIP file.

Related

Why Is A FilePath Item With ~$ Added To ListBox

I am adding file paths from a folder to a List Box which are then opened as text in a Rich Text Box. I have used the same syntax as the code below for achieving the same purpose in another List Box and it works just fine. But, in the current example, I have two files in the default MyProjects folder (i.e. default folder is created by my app), but when I add the file paths from the folder as items to the List Box, I get a third item with ~$ in the file path? This item is obviously some kind of repetition of the first file path in the list? The two files in the default folder are also created by my app so, if this is a file access issue, I don't understand why I wouldn't have access to a file created by my app? Can anyone give me a clue what's happening here?
What I have Tried:
I have tried debugging to check where the extra file path is coming from. As far as I can tell, it is being created when I add the file paths to the List Box? i.e. commenting out the code for adding the items to the List Box stops all items being added, but doesn't tell me where this extra item is coming from?
The "Extra Item" Issue:
System.Windows.Forms.ListBox+ObjectCollectionC:\Users\username\Documents\MySolution\MyProjects\RTFdoc.rtf
C:\Users\username\Documents\MySolution\MyProjects\Testdoc.rtf
C:\Users\username\Documents\MySolution\MyProjects~$Fdoc.rtf
The Code:
lbxName.Items.AddRange(Directory.GetFiles("C:\Users\" + username + "\Documents\MySolution\MyProjects"))
lbxName.SelectedIndex = 0
Code For Loading:
For Each item In lbxName.SelectedItems
RTB.LoadFile(lbxName.SelectedItem, RichTextBoxStreamType.RichText)
Next
I cannot reproduce the error in the following code.
Private Sub FillListBox()
ListBox1.Items.AddRange(Directory.GetFiles("C:\Users\" & username & "\Documents\MySolution\MyProjects"))
ListBox1.SelectedIndex = 0
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
FillListBox()
End Sub
Please read the comments in the following code.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each item In ListBox1.SelectedItems
'The following will overwrite the contents of the RichTextBox on each iteration
'This overload of LoadFile will only handle .rtf files
RichTextBox1.LoadFile(item.ToString)
Next
End Sub
I suggest you set the SelectionMode property to One in the designer and do the following.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
RichTextBox1.LoadFile(ListBox1.SelectedItem.ToString)
End Sub
Try to use as follows:
Directory.GetFiles("C:\Users\" & username & "\Documents\MySolution\MyProjects").Where(Function(f)
Return New IO.FileInfo(f).Attributes & IO.FileAttributes.Hidden & IO.FileAttributes.System = 0

Simulate web search using the WebBrowser control

I am trying to write a program that simulates a simple browser search in www.google.com using the WebBrowser control. I'm really just wanting to simulate internet activity.
I came up with the idea of using a loop to send a number to the google search box and then pressing enter.
The line WebBrowser1.Document.GetElementById("q").SetAttribute("value", i) successfully sends each number in the loop to the google search box, but the next line WebBrowser1.Document.GetElementById("btnK").InvokeMember("Click") won't initiate the google search button. I don't get any errors.
Does anyone have any ideas why WebBrowser1.Document.GetElementById("btnK").InvokeMember("Click") doesn't work?
Also I've noticed that when I run this code and then launch Internet Explorer, the code stops. Does anyone have any ideas on this as well?
Most grateful for any help!
Regards
George
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call LoadBrowser()
End Sub
Private Sub LoadBrowser()
WebBrowser1.Navigate("http://www.google.com/")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Send search string 'i' to browser n times
Dim i As Integer
For i = 1 To 100
' Browser search
WebBrowser1.Document.GetElementById("q").SetAttribute("value", i)
WebBrowser1.Document.GetElementById("btnK").InvokeMember("Click")
' Pause n seconds before next loop
For x As Integer = 0 To 5 * 100 ' Pause for 5 seconds
Threading.Thread.Sleep(10)
Application.DoEvents()
Next
Next
End Sub
Ok, working off the example you have given us.
Try this.
Imports WindowsInput '' <---------
''' <summary>
''' This program will run quick slow, would be best to use another thread for it, I'll leave that to you.
''' </summary>
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
WebBrowser1.Navigate("www.google.com.au")
End Sub
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim SIM = New InputSimulator() ''Use NuGet package manager and search for Input Simulator at Import it at the top
For i = 0 To 100
WebBrowser1.Document.GetElementById("lst-ib").InvokeMember("Click") ''this will click on googles searchbox
WebBrowser1.Document.GetElementById("q").InnerText = i
Await Task.Delay(500) ''This will delay your code so you can see what is in the searchbox, I prefer this over Thread.Sleep, each to their own I guess.
WebBrowser1.Document.GetElementById("q").InvokeMember("Click") ''This will click on googles search box to get the delete simulator ready.
SIM.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.DELETE) '' This will delete whatever is in the searchbox since your original code was doing this and you didn't complain about it
Next
WebBrowser1.Document.GetElementById("lst-ib").InvokeMember("Click") ''ONCE the loop as completed it will click on googles search box
SIM.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.RETURN) '' Another keypress for the enter key (MAY NOT WORK DEPENDING IF YOU HAVE CLICK OFF THE WEBBROWSER CONTROL) maybe you can tab back to it?
'WebBrowser1.Document.GetElementById("sblsbb").InvokeMember("Click") ''if the above line doesn't work
End Sub
End Class
Sorry for the long comments, I'm just trying to help you understand what the code is doing and the possible errors you may or may not encounter along with possible fixes and or work arounds.Good luck.

How To Save/Recall Folderbrowserdialog SelectedPath

I'm currently teaching myself (with the help of SO & Google) VB.Net to create a launcher for a multiplayer mod and I need users upon first launch of my application to input where their folder is stored, so far I have;
Dim folderDlg As System.Windows.Forms.FolderBrowserDialog
folderDlg = New System.Windows.Forms.FolderBrowserDialog
folderDlg.Description = "Please select your multiplayer folder"
If My.Settings.isFirstRun Then
My.Settings.isFirstRun = False
My.Settings.Save()
folderDlg.ShowDialog()
Else
End If
The button to run the mod itself
Private Sub Launch_mp_Click(sender As Object, e As EventArgs) Handles Launch_mp.Click
If My.Computer.FileSystem.FileExists("launcher.exe") Then
Process.Start("launcher.exe")
Timer2.Interval = 1000
Timer2.Start()
End If
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
p = Process.GetProcessesByName("eurotrucks2")
If p.Count > 0 Then
Timer2.Stop()
Me.WindowState = FormWindowState.Minimized
Me.Visible = True
Else
End If
End Sub
I'm confused as to how I can store the users selected path and then recall it later on for the button without always asking for the dir.
You are almost there:
You have various options where to store the information: registry, old-style using ini-files or in the config file of your application. I would suggest using the config file since you already store the isFirstRun-varialbe in the config. In project explrorer look at the "My Project" folder and double click an item called "Settings". Add a setting of type string called "ModFolder". After that you will be able to access the value of that setting using My.Settings.ModFolder varialbe (see here).
Use the FolderBrowserDialog to store the folder (see here)
if folderDlg.ShowDialog() = DialogResult.Ok then
My.Settings.ModFoler = folderDlg.SelectedPath
My.Settings.Save
end if
When your application starts next time the ModFolder-variable will automaticall hold the value stored so instead of If My.Settings.isFirstRun Then I would check:
If File.Exists(Path.Combine(My.Settings.ModFolder, "AppToStart.Exe")) then
...
end if
If the file exists launch it, if not re-show the dialog to pick the folder.

VB.Net trial application

how can i make my application goes to function after certain usage
like if i click an button 10 times, then button is disabled
just like trial program,
so far, i can do that on run-time only,
how can i make it count clicks without using Registry?
my program is very simple: convert strings to Base64
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = Convert.ToBase64String(New System.Text.ASCIIEncoding().GetBytes(TextBox1.Text))
End Sub
You probably need Settings. Easily Save and Retrieve Application and User Settings in VB.NET or C# Apps. First create ClickCouter of type integer with value 0 in settings (see the article).
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
My.Settings.ClickCouter +=1
My.Settings.Save()
If My.Settings.ClickCouter>=10 then Button1.Enabled = False
End Sub
It is necessary to check ClickCouter also in Form_Load. The value is stored in a file in a user profile so the solution is not too "hacker proof" (but is fool proof :-D ).

WebBrowser ignores the code

I am trying to use Mibbit irc in my project, and so far is working well, but there is a flaw. Links pasted in the chat upon click are getting opened in Internet explorer, instead of users' default web browser. I tried implementing a simple code, but half of it seems to get ignored.
http://i.stack.imgur.com/FKGGr.jpg
WebBrowser Component Startup page: http://widget.mibbit.com/?settings=4abcd3a5f0bf25306d4c6d1968e28cb2&server=irc.mibbit.net&channel=%23Mytestchannel12345
Ignore if contains: mibbit.com(the chat widged) & ad4game.com(the stupid banner...)
If contains because it places different banners - thus, different links. As well for the widged, it obviously have several servers that is hosting it on and it redirects to some of them, like widged1.mibbit.com, widged2.mibbit.com, etc.
Open in Default user browser: All, except those 2 above.
Public Class Form1
Private Sub WebBrowser1_Navigating(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating
Dim navTo As String = e.Url.ToString
If Not (navTo.ToLower.Contains("mibbit.com") OrElse navTo.ToLower.Contains("ad4game.com") OrElse navTo.ToLower.Contains("about:blank")) Then
e.Cancel = True
System.Diagnostics.Process.Start(e.Url.ToString())
End If
End Sub
End Class
Nothing so far worked...
Okay, I've updated your code sample:
Add a new function to find out what the path to the default browser is:
Public Class Form1
Private Sub WebBrowser1_Navigating(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating
Dim navTo As String = e.Url.ToString
If Not (navTo.ToLower.Contains("mibbit.com") OrElse navTo.ToLower.Contains("ad4game.com") OrElse navTo.ToLower.Contains("about:blank")) Then
e.Cancel = True
System.Diagnostics.Process.Start(GetDefaultBrowserPath, e.Url.ToString())
End If
End Sub
' get the default folder path from the registry
Public Function GetDefaultBrowserPath() As String
Dim defaultbrowser As String = My.Computer.Registry.GetValue("HKEY_CLASSES_ROOT\HTTP\shell\open\command", "", "Not Found")
Return Split(defaultbrowser, """")(1)
End Function
End Class