Question

I'm a week into learning visual basic and I'm completely stuck.

I have a TabControl with 2 TabItems in the XAML of a WPF:

<TabControl Name="browser_tabs" Width="Auto" Height="Auto" BorderThickness="0">

        <TabItem Header="My Page">
            <DockPanel Name="tab1" Width="Auto" Height="Auto" Background="#fefefe" />
        </TabItem>

        <TabItem Header="Browser" Name="browserTab">
            <DockPanel Name="tab2" Width="Auto" Height="Auto" Background="#fefefe" />
        </TabItem>

    </TabControl>

and I am trying to change the tab (programmatically / problematically) from a class, bookmarkControlsObj, within the MainWindow class:

Class MainWindow
    ...

    Public Class bookmarkControlsObj
        Public Sub follow(ByVal url As String)
            browser_tabs.selectedIndex = 1 '<--- ahhhhhhhhhhhhhhhhhhh!'
            web_view2.Load(url)
        End Sub
    End Class

End Class

But I keep getting "Reference to a non-shared member requires an object reference." -- I have no idea how to share the XAML TabControl within the bookmarkControlsObj class.. and it's driving me crazy. Please help.

EDIT:

The reason I have a nested class is because I'm calling the "follow" Sub within the class from a cefsharp (chrome embedded) WebView housing a page with a Javascript function -- using the RegisterJsObject I'm able to call a vb.net method via Javascript. In this case I'm sending a url to the "follow" Sub which loads it in a browser contained in the second TabItem, but I want it to automatically switch to that tab before it loads -- Long story short, RegisterJsObject seems to only work if it is contained within a class.. though it's only nested because that's where it ended up.

I'm able to access the browser in the second tab from the class because I declared it "shared" and added it to the TabItem -- but I'm not sure how to declare the TabControl (set visually) as Shared

Here is a link to the RegisterJSObject if it helps any: https://github.com/ataranto/CefSharp/blob/master/CefSharp/CefSharp.h#L116

Was it helpful?

Solution

"I have no idea how to share the XAML TabControl within the bookmarkControlsObj class.."

Make an overloaded constructor so when you instantiate the bookmarkControlsObj class you can pass in a reference to the Tab control. eg:

Public Class bookmarkControlsObj

Private browser_Tabs As TabControl
Public Sub New bookmarkControlsObj()
 //Default constructor
End Sub

Public Sub New bookmarkControlsObj(ByVal tc As TabControl)
 //Overloaded constructor
 browser_Tabs = tc
End Sub

Calling code:

Dim bookmarkCntrlObj As New bookmarkControlsObj(browser_tabs)
bookmarkCntrlObj.follow("aUrl")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top