-
1 Attachment(s)
[RESOLVED] [2008] Internet Connected indicators
Filezilla has 2 leds on the statusbar to indicate internet connection status.
Green led for connected, red led for not connected.
I'd like to mimic that on my webbrowser form's open event.
Can someone please let me the best way to do this. Image is attached.
-
Re: [2008] Internet Connected indicators
what about calling below method in Timer function
Code:
Private sub CheckInternet() As
Dim urlTest As New System.Uri("http://www.google.com/")
Dim wreqTest As WebRequest
wreqTest = WebRequest.Create(urlTest)
Dim wrespTest As System.Net.WebResponse
Try
wrespTest = wreqTest.GetResponse()
wrespTest.Close()
wreqTest = Nothing
'Here set the Green Image to Picture box
Catch ex As Exception
wreqTest = Nothing
'Here set the Red Image to Picture box
End Try
End sub
Edit: Dont forget to Imports System.Net namespace
-
Re: [2008] Internet Connected indicators
check My.Computer.Network.IsAvailable to see if the user is connected to a network. This of course doesn't 100% mean they have internet access, but MOST cases of people connected to a network have internet access.
So I would check that first, and if its true, then ping the specific server you want to see if they can connect to. If you wanted to use Google as your test server, like the example above, it would look something like this:
Code:
If My.Computer.Network.IsAvailable AndAlso My.Computer.Network.Ping("www.google.com") Then
MessageBox.Show("You are connected to the internet")
Else
MessageBox.Show("You are NOT connected to the internet")
End If
-
Re: [2008] Internet Connected indicators
There is also a NetworkAvailablityChanged event in the MyApplication class
Code:
Private Sub MyApplication_NetworkAvailabilityChanged(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.Devices.NetworkAvailableEventArgs) Handles Me.NetworkAvailabilityChanged
End Sub
You can find it under My Project -> Application -> View Application Events.
-
Re: [2008] Internet Connected indicators
High time to migrate from 02/03 :cry:
-
Re: [2008] Internet Connected indicators
yes sir ;)
many improvements. Not too many down sides.
The only negative I have seen about developing against .NET 2.0 versus 1.1 is that XP MUST have SP2 installed. Many of my customers did not have SP2 installed, so I had to guide them through that a few times.
-
Re: [2008] Internet Connected indicators
Thanks for this info..... hopefully soon i will get to work with 2005/08
-
Re: [2008] Internet Connected indicators
you can always download the express version of VS2008 which is free
-
Re: [2008] Internet Connected indicators
Matt, thanks very much. Your solution works great.
Is there any way to detect if/when internet connection is lost ?
-
Re: [2008] Internet Connected indicators
I don't think that the NetworkAvailabilityChanged event will fire if JUST the internet connection is lost. I think it will fire in scenarios where
1) you disable the network adapter on the PC
2) you unplug the network cable
3) the network device you were connecting to loses power or you unplug network cable from there (usually hub/switch or router)
If you stay connected to the actual network, but just internet goes out, and you wanted to know that, I am not sure how you could do that other than some sort of polling for a given internet site that SHOULD be reachable, like the ping code does to try to ping google.com.
-
Re: [2008] Internet Connected indicators
Thanks. I am using this code on a form with a webbrowser.
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If My.Computer.Network.IsAvailable AndAlso My.Computer.Network.Ping("www.google.com") Then
Me.ToolStripStatusLabel1.Image = My.Resources.Resources.green_on_16 'green led
Me.ToolStripStatusLabel1.Text = "Internet Connected"
Else
Me.ToolStripStatusLabel1.Image = My.Resources.Resources.red_on_16 'red led
Me.ToolStripStatusLabel1.Text = "Internet Not Connected"
End If
End Sub
If a website is down, along with the "page cannot be loaded" notification on the webpage, how can I use the ping test before webbrowser1.navigate("url") ?
That way if the website is offline, I can have a messagebox.show("website is down")
-
Re: [2008] Internet Connected indicators
The problem is that the managed browser control included in .NET 2.0 and higher is missing some key events. I have no idea why, but my best guess it has something to do with compatibility where the framework versions are supposed to work so they had to only include managed methods that were compatible with the framework. Again just a guess.
However one of the events they do not include (although it is present in the activex com based browser control) is the NavigateError event, which would fire when page navigation failed. This would allow you to handle such an event in code.
There is a way to extend the managed browser to allow it to listen for and fire that event, but it is not really an easy task. I have that code somewhere, but I would have to find it.
Another problem you will likely run into with the managed browser control, is that any popup windows, actually spawn in a seperate instance of IE. What this means is, if you are on a site that uses sessions, and you click something that causes a popup page, and that popup page makes use of the same session (usually, but not limited to authentication purposes on sites you need to login to use) the session state is lost, because the popup is its own instance. This makes many sites not work correctly.
-
Re: [2008] Internet Connected indicators
Thanks for the explanation. Would love to see that code if you can find it. What about pinging the website on a timer. Is that too resource intensive?
-
Re: [2008] Internet Connected indicators
well of course you could do some tests and see the results, but of course I would say to you its not the "best" way to do things.
I will see if I can dig up the code I had that extended the browser.
-
Re: [2008] Internet Connected indicators
Matt, on a side note, what do you use to archive your code snippets ?
-
Re: [2008] Internet Connected indicators
lol unfortunately, mostly my brain...
-
Re: [2008] Internet Connected indicators
Matt, any joy in finding stuff in the attic ?
-
Re: [2008] Internet Connected indicators
here you go
Code:
'2007 KLEINMA
'www.zerosandtheone.com
Option Strict On
Imports System.Runtime.InteropServices
Imports System.Security.Permissions
Namespace ZerosAndTheOne.Controls
Class WebBrowserEx
Inherits WebBrowser
Private cookie As AxHost.ConnectionPointCookie
Private helper As WebBrowser2EventHelper
'NEW EVENTS THAT WILL NOW BE EXPOSED
Public Event NewWindow2 As WebBrowserNewWindow2EventHandler
Public Event NavigateError As WebBrowserNavigateErrorEventHandler
'DELEGATES TO HANDLE PROCESSING OF THE EVENTS
Public Delegate Sub WebBrowserNewWindow2EventHandler(ByVal sender As Object, ByVal e As WebBrowserNewWindow2EventArgs)
Public Delegate Sub WebBrowserNavigateErrorEventHandler(ByVal sender As Object, ByVal e As WebBrowserNavigateErrorEventArgs)
Public Delegate Sub AuthenticateEventHandler(ByVal sender As Object, ByVal e As AuthenticateEventArgs)
#Region " PROTECTED METHODS FOR EXTENDED EVENTS "
Protected Overridable Sub OnNewWindow2(ByVal e As WebBrowserNewWindow2EventArgs)
RaiseEvent NewWindow2(Me, e)
End Sub
Protected Overridable Sub OnNavigateError(ByVal e As WebBrowserNavigateErrorEventArgs)
RaiseEvent NavigateError(Me, e)
End Sub
#End Region
#Region "WB SINK ROUTINES"
<PermissionSetAttribute(SecurityAction.LinkDemand, Name:="FullTrust")> _
Protected Overrides Sub CreateSink()
MyBase.CreateSink()
helper = New WebBrowser2EventHelper(Me)
cookie = New AxHost.ConnectionPointCookie(Me.ActiveXInstance, helper, GetType(DWebBrowserEvents2))
End Sub
<PermissionSetAttribute(SecurityAction.LinkDemand, Name:="FullTrust")> _
Protected Overrides Sub DetachSink()
If cookie IsNot Nothing Then
cookie.Disconnect()
cookie = Nothing
End If
MyBase.DetachSink()
End Sub
#End Region
#Region "PROPERTIES EXPOSED THROUGH THE COM OBJECT"
<System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Hidden)> _
<System.Runtime.InteropServices.DispIdAttribute(200)> _
Public ReadOnly Property Application() As Object
Get
If IsNothing(Me.ActiveXInstance) Then
Throw New AxHost.InvalidActiveXStateException("Application", AxHost.ActiveXInvokeKind.PropertyGet)
End If
Return CallByName(Me.ActiveXInstance, "Application", CallType.Get, Nothing)
'THIS IS COMMENTED. UNCOMMENT AND REMOVE LINE BEFORE IF YOU CAN NOT USE CALLBYNAME()
'Return Me.ActiveXInstance.Application
End Get
End Property
<System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Hidden)> _
<System.Runtime.InteropServices.DispIdAttribute(552)> _
Public Property RegisterAsBrowser() As Boolean
Get
If IsNothing(Me.ActiveXInstance) Then
Throw New AxHost.InvalidActiveXStateException("RegisterAsBrowser", AxHost.ActiveXInvokeKind.PropertyGet)
End If
Dim RetVal As Boolean = False
If Not Boolean.TryParse(CallByName(Me.ActiveXInstance, "RegisterAsBrowser", CallType.Get, Nothing).ToString, RetVal) Then RetVal = False
Return RetVal
'THIS IS COMMENTED. UNCOMMENT AND REMOVE 3 LINES BEFORE IF YOU CAN NOT USE CALLBYNAME()
'Return Me.ActiveXInstance.RegisterAsBrowser
End Get
Set(ByVal value As Boolean)
If IsNothing(Me.ActiveXInstance) Then
Throw New AxHost.InvalidActiveXStateException("RegisterAsBrowser", AxHost.ActiveXInvokeKind.PropertySet)
End If
CallByName(Me.ActiveXInstance, "RegisterAsBrowser", CallType.Let, True)
'THIS IS COMMENTED. UNCOMMENT AND REMOVE LINE BEFORE IF YOU CAN NOT USE CALLBYNAME()
'Me.ActiveXInstance.RegisterAsBrowser = value
End Set
End Property
#End Region
'HELPER CLASS TO FIRE OFF THE EVENTS
Private Class WebBrowser2EventHelper
Inherits StandardOleMarshalObject
Implements DWebBrowserEvents2
Private parent As WebBrowserEx
Public Sub New(ByVal parent As WebBrowserEx)
Me.parent = parent
End Sub
Public Sub NewWindow2(ByRef ppDisp As Object, ByRef cancel As Boolean) Implements DWebBrowserEvents2.NewWindow2
Dim e As New WebBrowserNewWindow2EventArgs(ppDisp)
Me.parent.OnNewWindow2(e)
ppDisp = e.ppDisp
cancel = e.Cancel
End Sub
Public Sub NavigateError(ByVal pDisp As Object, ByRef URL As Object, ByRef frame As Object, ByRef statusCode As Object, ByRef cancel As Boolean) Implements DWebBrowserEvents2.NavigateError
' Raise the NavigateError event.
Me.parent.OnNavigateError( _
New WebBrowserNavigateErrorEventArgs( _
CStr(URL), CStr(frame), CInt(statusCode), cancel))
End Sub
End Class
End Class
Public Class WebBrowserNewWindow2EventArgs
Inherits System.ComponentModel.CancelEventArgs
Private ppDispValue As Object
Public Sub New(ByVal ppDisp As Object)
Me.ppDispValue = ppDisp
End Sub
Public Property ppDisp() As Object
Get
Return ppDispValue
End Get
Set(ByVal value As Object)
ppDispValue = value
End Set
End Property
End Class
Public Class WebBrowserNavigateErrorEventArgs
Inherits EventArgs
Private urlValue As String
Private frameValue As String
Private statusCodeValue As Int32
Private cancelValue As Boolean
Public Sub New( _
ByVal url As String, ByVal frame As String, _
ByVal statusCode As Int32, ByVal cancel As Boolean)
Me.urlValue = url
Me.frameValue = frame
Me.statusCodeValue = statusCode
Me.cancelValue = cancel
End Sub
Public Property Url() As String
Get
Return urlValue
End Get
Set(ByVal value As String)
urlValue = value
End Set
End Property
Public Property Frame() As String
Get
Return frameValue
End Get
Set(ByVal value As String)
frameValue = value
End Set
End Property
Public Property StatusCode() As Int32
Get
Return statusCodeValue
End Get
Set(ByVal value As Int32)
statusCodeValue = value
End Set
End Property
Public Property Cancel() As Boolean
Get
Return cancelValue
End Get
Set(ByVal value As Boolean)
cancelValue = value
End Set
End Property
End Class
Public Class AuthenticateEventArgs
Inherits System.EventArgs
Public Sub New()
End Sub
Public Sub SetParameters()
Me.handled = False
Me.username = String.Empty
Me.password = String.Empty
Me.retvalue = 0
'Hresults.S_OK
End Sub
Public username As String
Public password As String
Public retvalue As Integer
Public handled As Boolean
End Class
<ComImport(), Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"), _
InterfaceType(ComInterfaceType.InterfaceIsIDispatch), _
TypeLibType(TypeLibTypeFlags.FHidden)> _
Public Interface DWebBrowserEvents2
<DispId(DISPID.NEWWINDOW2)> Sub NewWindow2( _
<InAttribute(), OutAttribute(), MarshalAs(UnmanagedType.IDispatch)> ByRef ppDisp As Object, _
<InAttribute(), OutAttribute()> ByRef cancel As Boolean)
<DispId(DISPID.NAVIGATERROR)> Sub NavigateError( _
<InAttribute(), MarshalAs(UnmanagedType.IDispatch)> _
ByVal pDisp As Object, _
<InAttribute()> ByRef URL As Object, _
<InAttribute()> ByRef frame As Object, _
<InAttribute()> ByRef statusCode As Object, _
<InAttribute(), OutAttribute()> ByRef cancel As Boolean)
End Interface
<ComImport()> _
<GuidAttribute("79eac9d0-baf9-11ce-8c82-00aa004ba90b")> _
<InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)> _
Public Interface IAuthenticate
<PreserveSig()> _
Function Authenticate(ByRef phwnd As IntPtr, ByRef pszUsername As IntPtr, ByRef pszPassword As IntPtr) As Integer
End Interface
Public Enum DISPID
NEWWINDOW2 = 251
NAVIGATERROR = 271
End Enum
End Namespace
-
Re: [2008] Internet Connected indicators
Thanks Matt. So I should paste the code into a separate class.
My next question is how to add this to a form instead of webbrower.
How do I add WebBrowserEx to my toolbox so that it can be a reusable component ?
-
Re: [2008] Internet Connected indicators
put it in its own class file, compile the project. Then WebBrowserEx will show up in your toolbox, so you can drag to a form like the regular browser.
-
Re: [2008] Internet Connected indicators
Whoa, got it working and there's a lot more features on webbrowserEx. Thanks.
I got NavigateError working nicely.
-
Re: [2008] Internet Connected indicators
glad you got it working. Those 2 extra events I added into the browser are really very important to making any sort of app that uses the browser control.
I would like to actually find out why they are not in the stock managed control.
-
Re: [2008] Internet Connected indicators
That together with your NetworkAvailabilityChanged seem to do the job admirably.
Code:
Private Sub MyApplication_NetworkAvailabilityChanged(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.Devices.NetworkAvailableEventArgs) Handles Me.NetworkAvailabilityChanged
If My.Computer.Network.IsAvailable AndAlso My.Computer.Network.Ping("www.ccli.com") Then
Form1.ToolStripStatusLabel2.Image = My.Resources.Resources.green_on_16 'green led
Form1.ToolStripStatusLabel2.Text = "connected to website"
Else
If Form1.Created Then
Form1.ToolStripStatusLabel2.Image = My.Resources.Resources.red_on_16 'red led
Form1.ToolStripStatusLabel2.Text = "no connection"
End If
MessageBox.Show("Network connection has been lost...", "No Network", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
End Sub
-
Re: [2008] Internet Connected indicators
Good. sounds like your project is moving along nicely.
-
Re: [2008] Internet Connected indicators
Matt, this was a great help. You should put this together and pop it in your signature.
-
Re: [2008] Internet Connected indicators
I know.. I am working on it... just been too busy with various projects.
-
Re: [RESOLVED] [2008] Internet Connected indicators
Hello,
I'm trying to use your code in C# but i'm getting some errors on
#region "PROPERTIES EXPOSED THROUGH THE COM OBJECT"
return this.ActiveXInstance.Application;
and
return this.ActiveXInstance.RegisterAsBrowser;
Do you have a working C# version
Thanks
-
Re: [RESOLVED] [2008] Internet Connected indicators
No I don't have a translated C# version. You could try running it through an online converter.
Of course, you didn't mention what error you were getting, so there is no way to provide any help.
-
Re: [RESOLVED] [2008] Internet Connected indicators
Hello,
Thanks for your quick reply.
I get errors on the return this.ActiveXInstance.Application;
AND return this.ActiveXInstance.RegisterAsBrowser;
ERROR:
'object' does not contain a definition for 'Application' and no
extension method 'Application' accepting a first argument of
type 'object' could be found (are you missing a using directive or an assembly reference?)
Code:
[System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Hidden)]
[System.Runtime.InteropServices.DispIdAttribute(200)]
public object Application
{
get
{
if ((this.ActiveXInstance == null))
{
throw new AxHost.InvalidActiveXStateException("Application", AxHost.ActiveXInvokeKind.PropertyGet);
}
return this.ActiveXInstance.Application;
//return Interaction.CallByName(this.ActiveXInstance, "Application", CallType.Get, null);
}
//THIS IS COMMENTED. UNCOMMENT AND REMOVE LINE BEFORE IF YOU CAN NOT USE CALLBYNAME()
//Return Me.ActiveXInstance.Application
}
[System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Hidden)]
[System.Runtime.InteropServices.DispIdAttribute(552)]
public bool RegisterAsBrowser
{
get
{
if ((this.ActiveXInstance == null))
{
throw new AxHost.InvalidActiveXStateException("RegisterAsBrowser", AxHost.ActiveXInvokeKind.PropertyGet);
}
return this.ActiveXInstance.RegisterAsBrowser;
//bool RetVal = false;
//if (!bool.TryParse(Interaction.CallByName(this.ActiveXInstance, "RegisterAsBrowser", CallType.Get, null).ToString, RetVal)) RetVal = false;
//return RetVal;
}
//THIS IS COMMENTED. UNCOMMENT AND REMOVE 3 LINES BEFORE IF YOU CAN NOT USE CALLBYNAME()
//Return Me.ActiveXInstance.RegisterAsBrowser
set
{
if ((this.ActiveXInstance == null))
{
throw new AxHost.InvalidActiveXStateException("RegisterAsBrowser", AxHost.ActiveXInvokeKind.PropertySet);
}
return this.ActiveXInstance.RegisterAsBrowser = value;
//Interaction.CallByName(this.ActiveXInstance, "RegisterAsBrowser", CallType.Let, true);
}
//THIS IS COMMENTED. UNCOMMENT AND REMOVE LINE BEFORE IF YOU CAN NOT USE CALLBYNAME()
//Me.ActiveXInstance.RegisterAsBrowser = value
}
#endregion