How to prevent a TabItem from being selected?

Herry Markowitz

Well-known member
Joined
Oct 22, 2015
Messages
46
Programming Experience
1-3
There is no TabControl.Selecting Event in the WPF while there is in the Winform as you can see following.
https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx


I have found following source in order to prevent a TabItem from being selected.
https://joshsmithonwpf.wordpress.com...eing-selected/

VB.NET:
[COLOR=#333333]public Window1()[/COLOR]{
     InitializeComponent();

     base.DataContext = new DataSource();

     var items = new string[] { "A", "B", "C" };
     this.tab.ItemsSource = items;
     var collView = CollectionViewSource.GetDefaultView(items);
     collView.CurrentChanging += this.OnTabItemSelecting;
}

void OnTabItemSelecting(object sender, CurrentChangingEventArgs e)
{
     bool allow = this.chk.IsChecked.GetValueOrDefault();
     if (!allow)
     {
          // SelectedContent hasn't changed yet, so use it to figure out
          // the index of the previously selected tab
          int prevIdx = this.tab.Items.IndexOf(this.tab.SelectedContent);
          this.tab.SelectedIndex = prevIdx;
     } [COLOR=#333333]}[/COLOR]


Converted code via http://converter.telerik.com/;

VB.NET:
[COLOR=#333333]Public Sub Window1()[/COLOR]    InitializeComponent()
    MyBase.DataContext = New DataSource()
    Dim items = New String() {"A", "B", "C"}
    Me.tab.ItemsSource = items
    Dim collView = CollectionViewSource.GetDefaultView(items)
    collView.CurrentChanging += AddressOf Me.OnTabItemSelecting
End Sub

Private Sub OnTabItemSelecting(ByVal sender As Object, ByVal e As CurrentChangingEventArgs)
    Dim allow As Boolean = Me.chk.IsChecked.GetValueOrDefault()
    If Not allow Then
        Dim prevIdx As Integer = Me.tab.Items.IndexOf(Me.tab.SelectedContent)
        Me.tab.SelectedIndex = prevIdx
    End If [COLOR=#333333]End Sub[/COLOR]


My XAML codes;

VB.NET:
[COLOR=#333333]<Window x:Class="MainWindow"[/COLOR]  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="TabControlSample" Height="200" Width="250">
    <Grid>
        <TabControl x:Name="tab">
            <TabItem Header="General">
                <Label Content="Content goes here..." />
            </TabItem>
            <TabItem Header="Security" />
            <TabItem Header="Details" />
        </TabControl>
    </Grid> [COLOR=#333333]</Window>[/COLOR]


Here is the error picture: https://prnt.sc/ijtvm4


To be honest I am not good at with constructors.

So I need you support.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,439
Location
Norway
Programming Experience
10+
The "+=" in C# adds to delegate list for event handler, in VB you use AddHandler Statement to add the event handler, like:
Addhandler collView.CurrentChanging, AddressOf Me.OnTabItemSelecting
 

Herry Markowitz

Well-known member
Joined
Oct 22, 2015
Messages
46
Programming Experience
1-3
Hi JohnH.
I am very fresh in vb.net programming.
So, I need completed code which is ready to run.
So, please post whole code.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,439
Location
Norway
Programming Experience
10+
There's nothing more to post, that's how you add handler with vb.net instead of += in C#. I'm not looking at the other stuff, so if there are other issues you should ask specifically about them.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,350
Location
Sydney, Australia
Programming Experience
10+
I have helped you. I've told you that you should download Instant VB to get the best possible conversion of C# code to VB but you seem determined not to do so. If it is your opinion that the only way I can help is to write your code for you then I'm afraid that you'll be disappointed.
 

Herry Markowitz

Well-known member
Joined
Oct 22, 2015
Messages
46
Programming Experience
1-3
Solved by myself.

VB.NET:
Class MainWindow
PublicSubNew()
        InitializeComponent()
        Me.tab.ItemsSource=NewString(){"A","B","C"}
        AddHandler tab.SelectionChanged,AddressOfOnTabItemSelecting
    EndSub
    PrivateSubOnTabItemSelecting(ByVal sender AsObject,ByVal e AsSelectionChangedEventArgs)
        Dim allow AsBoolean=Me.chk.IsChecked.GetValueOrDefault()
        IfNot allow Then
            Dim prevIdx AsInteger=Me.tab.Items.IndexOf(Me.tab.SelectedContent)
            Me.tab.SelectedIndex= prevIdx
        EndIf
    EndSub
EndClass
 
Top Bottom