VB.NET | Cannot duplicate tabs properly - vb.net

I'm trying to create a browser in VB.NET, but I'm having problems to create a button that can "duplicate tabs" in my tabcontrol (called Child_TabControl). Well, I have this code and it seems to works fine in the first tab, but when I try to click on the duplicate button in the generated tab, it duplicates again the URL in the first tab. Anyone can help me to solve this?
Private Async Sub Btn_Duplicate_Click(sender As Object, e As EventArgs) Handles Btn_Duplicate.Click
Dim newTab As New TabPage
Dim child_header As New Tabs_Child_Header
Dim browser As New WebView2
'Events
Await browser.EnsureCoreWebView2Async
AddHandler browser.CoreWebView2.NewWindowRequested, AddressOf Browser_NewWindowRequested
AddHandler browser.CoreWebView2.NavigationCompleted, Sub()
child_header.Text_Url.Text = browser.Source.AbsoluteUri
End Sub
AddHandler child_header.Btn_Back.Click, Sub()
browser.GoBack()
End Sub
AddHandler child_header.Btn_Foward.Click, Sub()
browser.GoForward()
End Sub
AddHandler child_header.Btn_Update.Click, Sub()
browser.Reload()
End Sub
AddHandler child_header.Btn_Duplicate.Click, AddressOf Btn_Duplicate_Click
'Load Child
Child_TabControl.TabPages.Add(newTab)
child_header.TopLevel = False
child_header.Visible = True
child_header.Dock = DockStyle.Fill
newTab.Controls.Add(child_header)
'Child_TabControl.SelectTab(newTab)
'Load Webview
newTab.Text = Child_TabControl.SelectedTab.Text
child_header.Panel_Navigate.Controls.Add(browser)
browser.Dock = DockStyle.Fill
browser.Visible = True
browser.Source = New Uri(Text_Url.Text)
End Sub
I don't know how to reference (as Me) to my control Tabs_Child_Header dinamically created previously.

Related

InvokeMember("click") does not trigger WebBrowser.DocumentCompleted event

This old unresolved question seems to relate most to my issue WebBrowser control not responding to InvokeMember("click")
The main difference is where the conversation petered out, my webpage does respond to ContentPlaceHolder1_btnComtrsView.click() correctly, where in the original question it did not.
I am trying to pull up a lat/long result after clicking the "View" button after entering in a value for COMTRS (use "M11S19E20" for example): https://www.earthpoint.us/TownshipsCaliforniaSearchByDescription.aspx
I've got the 2 separate document completed event handlers working correctly and all of that. So is it possible to handle the event of the document updating after clicking view? My code does work if I just click once to load the page and click, and a second time to pull the data out.
WebBrowserTRS.ScriptErrorsSuppressed = True
AddHandler WebBrowserTRS.DocumentCompleted, AddressOf ClickViewButton
WebBrowserTRS.Navigate("https://www.earthpoint.us/TownshipsCaliforniaSearchByDescription.aspx")
Private Sub ClickViewButton(sender As Object, e As WebBrowserDocumentCompletedEventArgs)
If e.Url.ToString() = "about:blank" Then Return
RemoveHandler WebBrowserTRS.DocumentCompleted, AddressOf ClickViewButton
Dim trsDoc As HtmlDocument = WebBrowserTRS.Document
Dim elem_Input_Submit As HtmlElement = trsDoc.GetElementById("ContentPlaceHolder1_btnComtrsView")
trsDoc.GetElementById("ContentPlaceHolder1_Comtrs").InnerText = _comtrs
AddHandler WebBrowserTRS.DocumentCompleted, AddressOf GetLatLong
elem_Input_Submit.InvokeMember("click")
End Sub
Private Sub GetLatLong(sender As Object, e As WebBrowserDocumentCompletedEventArgs)
RemoveHandler WebBrowserTRS.DocumentCompleted, AddressOf GetLatLong
Dim trsDoc As HtmlDocument = WebBrowserTRS.Document
Dim centroidFound As Boolean = False
For Each el As HtmlElement In trsDoc.GetElementsByTagName("tr")
Dim val As String
For Each el1 As HtmlElement In el.GetElementsByTagName("TD")
val = el1.InnerText
If val IsNot Nothing AndAlso val.Contains("Centroid") Then
centroidFound = True
' ...
WebBrowserTRS = New WebBrowser
Return
End If
Next
Next
If Not centroidFound Then
MsgBox("Unable to parse the township and range.",
MsgBoxStyle.Information, "Error in location lookup")
End If
Cursor = Cursors.Default
toolstripViewMap.Enabled = True
End Sub

Index Selection in ListView created at runtime in Visual Basic (Visual Studio 2019)

I am trying to use a ListView box created at runtime and I am able to populate items but the SelectedIndexChanged event is not working. I know I am missing something really simple. Below is a minimal working example where I am creating the ListView on a button click and populating with a couple of items. When I select an item nothing happens in the SelectedIndexChanged event.
Public Class Form1
Dim lstMylist As ListView
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
lstMylist = New ListView()
lstMylist.Location = New Point(37, 312)
lstMylist.Size = New Size(150, 150)
Me.Controls.Add(lstMylist)
lstMylist.View = View.SmallIcon
Dim myListItem1 As ListViewItem
myListItem1 = lstMylist.Items.Add("Item 1")
Dim myListItem2 As ListViewItem
myListItem2 = lstMylist.Items.Add("Item 2")
End Sub
Private Sub lstMylist_SelectedIndexChanged(sender As Object, e As EventArgs)
MessageBox.Show("I am here")
Select Case lstMylist.FocusedItem.Index
Case 0
MessageBox.Show("item 1")
Case 1
MessageBox.Show("item 2")
Case Else
MessageBox.Show("invalid")
End Select
End Sub
End Class
You need to add the event handler to the ListView SelectedIndexChanged event
lstMylist = New ListView()
lstMylist.Location = New Point(37, 312)
lstMylist.Size = New Size(150, 150)
' Add the event handler for the listview
AddHandler lstMyList.SelectedIndexChanged, AddressOf lstMylist_SelectedIndexChanged
Me.Controls.Add(lstMylist)
lstMylist.View = View.SmallIcon
As outlined by djv it is important to call RemoveHandler if you remove the ListView
RemoveHandler lstMyList.SelectedIndexChanged, AddressOf lstMylist_SelectedIndexChanged
Steve's answer will work. But an alternative is to simply make your ListView WithEvents
Dim WithEvents lstMylist As ListView
and add Handles to the method declaration
Private Sub lstMylist_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstMylist.SelectedIndexChanged
This is more a VB.NET way of doing things. AddHandler is similar to C# Event += new EventHandler syntax.
Note, if using AddHandler, a matching RemoveHandler should appear if the ListView is to be removed and added repeatedly.

SelectionChanged event occurs more times than it should

I have grid and those grid is populate on Form's load event. At the end line of that event i am hooking method handler for my SelectionChanged event of this grid. I want to get current selected row's zero cell's 1 value. Unfortunately when i run program my SelectionChanged event method handler is called infinite times... And i have no idea why is that.
So its basically like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
some code which populating data to grid...
'here hooking up method after data is there already to not fire it up during grid population
AddHandler gridArtikels.SelectionChanged, AddressOf gridArtikels_SelectionChanged
End Sub
and this is event handler method itself:
Private Sub gridArtikels_SelectionChanged(sender As Object, e As GridEventArgs)
RemoveHandler gridArtikels.SelectionChanged, AddressOf gridArtikels_SelectionChanged
If gridArtikels.PrimaryGrid.Rows.Count > 0 Then
gridArtikels.PrimaryGrid.SetSelectedRows(0, 1, True)
ItemPanelImgs.Items.Clear()
'Dim images As New List(Of Article_Image)
Dim selectedNummer As String = String.Empty
selectedNummer = gridArtikels.PrimaryGrid.SelectedRows(0).Cells(1).Value.ToString()
'images = ArtikelsAndTheirVariationsFinal.GetImagesForArticle(selectedNummer)
'ItemPanelImgs.DataSource = images
End If
AddHandler gridArtikels.SelectionChanged, AddressOf gridArtikels_SelectionChanged
End Sub
P.S I am using to be concrete supergrid control from DotnetBar devcomponenets but it shouldn't be diffrent from ordinary controls behaviour.
What could be wrong here?
For those whom would like to debug here is sample app
EDIT:
I also tried this way but its still going to infinitive loop...
Public IgnoreSelectionChanged As Boolean = False
Private Sub gridArtikels_SelectionChanged(sender As Object, e As GridEventArgs) Handles gridArtikels.SelectionChanged
If IgnoreSelectionChanged Then Exit Sub
IgnoreSelectionChanged = True
If gridArtikels.PrimaryGrid.Rows.Count > 0 Then
gridArtikels.PrimaryGrid.SetSelectedRows(0, 1, True)
ItemPanelImgs.Items.Clear()
'Dim images As New List(Of Article_Image)
Dim selectedNummer As String = String.Empty
selectedNummer = gridArtikels.PrimaryGrid.SelectedRows(0).Cells(1).Value.ToString()
'images = ArtikelsAndTheirVariationsFinal.GetImagesForArticle(selectedNummer)
'ItemPanelImgs.DataSource = images
End If
IgnoreSelectionChanged = False
End Sub

WebBrowser, right click on links to open new tab

I have made this code to open new TABs, if I click "right click" on links then I choose "open in new window"
it's open a new TAB in my browser but at the same time Internet Explorer will be opened too.
I just want to make the IE silent, or making new tab option not gray to use it. I don't want to make my custom menu, I am sure there is a solution for this.
I have searched on my question here it's different problem so please it's not duplicate
This is what I have used to make that works
Private Sub WebBrowser1_NewWindow1(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
Dim Browser As New WebBrowser
TabControl1.TabPages.Add("New Page")
TabControl1.SelectTab(int)
Browser.Name = "Web Browser"
Browser.Dock = DockStyle.Fill
TabControl1.SelectedTab.Controls.Add(Browser)
AddHandler Browser.ProgressChanged, AddressOf Loading
AddHandler Browser.DocumentCompleted, AddressOf Done
int = int + 1
End Sub
I solve it.
It's simple this command line will make IE silent.
e.Cancel = True
You don't need to build other menu and turn off the main menu of the IE then build all Items again.
This is the final code to open links when you click "right click on them" and will be opened in a new tab
Private Sub WebBrowser1_NewWindow1(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
e.Cancel = True
Dim thiselement As HtmlElement = WebBrowser1.Document.ActiveElement
Dim Browser As New WebBrowser
Dim targeturl As String = thiselement.GetAttribute("href")
With Browser
.Navigate(targeturl)
.Dock = DockStyle.Fill
End With
TabControl1.TabPages.Add(targeturl)
TabControl1.SelectTab(int)
TabControl1.SelectedTab.Controls.Add(Browser)
AddHandler Browser.ProgressChanged, AddressOf Loading
AddHandler Browser.DocumentCompleted, AddressOf Done
int = int + 1
End Sub

How do I add then select the dynamic control I just added

I've got an application in VB.net which adds a picturebox to the selected mouse position within another picturebox control. I need to create a click event to select that new picturebox so I can drag it and drop it to a new location in the event that the first one was wrong or use a keypress event, those events I will code later, but I can not figure out how to select ANY of the dynamic controls.
In vb6 there was a way to select an index of the control, but there is no such animal in VB.net.
I've tried control groups, but for some reason I'm not getting results from them.
Here is the code I have so far
Private Sub PictureBox1_Click(sender As System.Object,
e As System.EventArgs) Handles PictureBox1.Click
Dim pb As New PictureBox
pb.BackColor = Color.Blue
Me.PictureBox1.Controls.Add(pb)
pb.Size = New Size(64, 110)
pb.Location = New Point(Cursor.Position.X - 64, Cursor.Position.Y - 110)
pb.Visible = True
End Sub
What in the name of all good things am I doing wrong here?
You need to write a generic event handler before time, using the sender parameter to refer to the object that raised the event.
Private Sub PictureBoxes_Click(sender As Object, e As EventArgs)
Dim pb = DirectCast(sender, PictureBox)
'Use pb here.
End Sub
When you create your control at run time, use an AddHandler statement to attach the method to the event.
Dim pb As New PictureBox
AddHandler pb.Click, AddressOf PictureBoxes_Click
That said, if you want to implement drag-n-drop then it's not the Click event you should be handling.
This little bit of code took some time, but I was able to do what I set out to so far...
This is before the Sub Main event
Public Class dynamicPB 'create a picturebox element which
'can be called anytime
Inherits PictureBox 'sets the type of control to a
'picturebox
Public Sub New() 'sets the function of the new box to
'default values
MyBase.BackColor = Color.AliceBlue
MyBase.BorderStyle = Windows.Forms.BorderStyle.Fixed3D
MyBase.Height = 50
MyBase.Width = 26
End Sub
End Class
in the actual main class
Private Sub <control_event> (blah...) Blah...
Dim intPosAdj_X As Integer = 13 'get an offset for the cursor
Dim intPosAdj_Y As Integer = 25
Dim newPictureBox As New dynamicPB 'sets the click of the mouse into a
'mode of drawing a new PB
With newPictureBox 'clean use of the code
AddHandler newPictureBox.Click, _
AddressOf PictureBox_Click 'establishes events for the mouse
'activity on the objects
AddHandler newPictureBox.MouseEnter, _
AddressOf PictureBox_MouseEnter
AddHandler newPictureBox.MouseLeave, _
AddressOf PictureBox_MouseLeave
pbName += 1 'gives a unique name to the
'picturebox in an "array" style
.Location = New System.Drawing.Point _
(xPos - intPosAdj_X, yPos - intPosAdj_Y) 'establish where the box goes
'and center the object on the
'mouse pointer
.Visible = True 'ensures that the box is visible
.Name = pbName 'names the new control
End With
Controls.Add(newPictureBox) 'add control to form
End Sub
Private Sub PictureBox_Click(sender As System.Object, e As System.EventArgs)
Dim dblMouseClick As Double = CType(DirectCast _
(e, System.Windows.Forms.MouseEventArgs).Button, MouseButtons) _
'make it simple to manipulate
'the event by putting the long
'code into a variable
If dblMouseClick = MouseButtons.Left Then
MsgBox("Left CLick")
ElseIf dblMouseClick = MouseButtons.Right Then
MsgBox("right click")
Else
MsgBox("Center")
End If
This actually resolves the issue of adding and being able to select the object
Thanks for all of the suggestions and help