I have been looking at the possible answers on various places on the Internet, and all don't seem to conform to my use case or are too vague for me to understand how to use them. Here is what I have come up with by looking at the top posts on Stack Overflow:
Delegate Sub delgation(text As String)
Public Sub SetToolText(text As String)
If ToolStrip1.InvokeRequired Then
Invoke(New delgation(AddressOf SetToolText), text)
End If
End Sub
I don't understand how to use this in order to change my ToolStrip's TextLabel for two reasons:
- How exactly do you use this in another class?
- How do you know what item you are targetting in the ToolStrip? This is what has me the most confused. If I have two Labels, then which one am I setting text to? This is extremely confusing.
I intend to use it on my Downloader class:
Imports CefSharp
Public Class Download_Handler
Implements IDownloadHandler
Public Event Finished()
Public Sub Before(Chromium As IWebBrowser, Browser As IBrowser, Item As DownloadItem, Callback As IBeforeDownloadCallback) Implements IDownloadHandler.OnBeforeDownload
If Not Callback.IsDisposed Then Callback.Continue(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), Item.SuggestedFileName), True)
End Sub
Public Sub Updated(Chromium As IWebBrowser, Browser As IBrowser, Item As DownloadItem, Callback As IDownloadItemCallback) Implements IDownloadHandler.OnDownloadUpdated
If Item.ReceivedBytes > 0 Then RaiseEvent Finished()
main_window.SetToolText(String.Format("{0}/{1} bytes received", Item.ReceivedBytes.ToString, Item.TotalBytes.ToString))
End Sub
End Class
Note this line: main_window.SetToolText(String.Format("{0}/{1} bytes received", Item.ReceivedBytes.ToString, Item.TotalBytes.ToString))
This on its own doesn't work, the text doesn't get set. I also tried to pass the label as a ToolStripLabel object when creating an instance for this class in order to reference it internally that way, but this causes the unsafe cross-threading error.
Thus I stumbled upon the endless samples of the aforementioned solution which makes little sense to me. How can I adapt it to my code?
(Edit) Someone asked for a reproducible sample:
- Create a WinForms .Net Framework Project
- Make sure your form is named as Form1
- Add the CefSharp Common and CefSharp WinForms Nuget Packages
- Add a ToolStrip named ToolStrip1
- Add a ToolStripLabel named DLProgressLabel within the ToolStrip
- Paste the below code and run:
Imports CefSharp
Imports CefSharp.WinForms
Public Class Form1
Dim Cef As ChromiumWebBrowser
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call InitCef()
End Sub
Private Sub InitCef()
'Create Cef Settings
Dim settings As New CefSettings()
settings.CachePath = Application.ExecutablePath + "\BrowserData"
settings.CefCommandLineArgs.Add("persist_session_cookies", "1")
'Create Cef instance
CefSharp.Cef.Initialize(settings)
Cef = New ChromiumWebBrowser("https://download.mozilla.org/?product=firefox-latest-ssl&os=linux64&lang=en-GB") With {.Dock = DockStyle.Bottom}
Cef.DownloadHandler = New Download_Handler()
Me.Controls.Add(Cef)
End Sub
Delegate Sub delgation(text As String)
Public Sub SetToolText(text As String)
If Me.ToolStrip1.InvokeRequired Then
Invoke(New delgation(AddressOf SetToolText), text)
Else
Me.DLProgressLabel.Text = text
End If
End Sub
End Class
Public Class Download_Handler
Implements IDownloadHandler
Public Event Finished()
Public Sub Before(Chromium As IWebBrowser, Browser As IBrowser, Item As DownloadItem, Callback As IBeforeDownloadCallback) Implements IDownloadHandler.OnBeforeDownload
If Not Callback.IsDisposed Then Callback.Continue(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), Item.SuggestedFileName), True)
End Sub
Public Sub Updated(Chromium As IWebBrowser, Browser As IBrowser, Item As DownloadItem, Callback As IDownloadItemCallback) Implements IDownloadHandler.OnDownloadUpdated
If Item.ReceivedBytes > 0 Then RaiseEvent Finished()
Call Form1.SetToolText(String.Format("{0}/{1} bytes received", Item.ReceivedBytes.ToString, Item.TotalBytes.ToString))
End Sub
End Class
As soon as it starts, it will begin downloading the Firefox file. Place a breakpoint on the Me.DLProgressLabel.Text = text
part of the SetToolTip
function and watch as it fails to change the text for no obvious reason.