Results 1 to 10 of 10

Thread: How to prevent a TabItem from being selected?

  1. #1
    .NET Framework
    .NET 4.0
    Join Date
    Oct 2015
    Posts
    46
    Reputation
    36

    How to prevent a TabItem from being selected?

    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/

    Code:
    public Window1(){
         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;
         } }

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

    Code:
    Public Sub Window1()    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 End Sub

    My XAML codes;

    Code:
    <Window x:Class="MainWindow"  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> </Window>

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


    To be honest I am not good at with constructors.

    So I need you support.
    I am VBA expert.
    I am trying to be vb.net expert.

  2. #2
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,191
    Reputation
    2856
    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
    [xcode=vb] code here [/xcode] - see bbcode list or use formatting buttons in posting editor.

    Visual Studio Community 2017

  3. #3
    .NET Framework
    .NET 4.0
    Join Date
    Oct 2015
    Posts
    46
    Reputation
    36
    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.
    I am VBA expert.
    I am trying to be vb.net expert.

  4. #4
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,191
    Reputation
    2856
    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.
    [xcode=vb] code here [/xcode] - see bbcode list or use formatting buttons in posting editor.

    Visual Studio Community 2017

  5. #5
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,965
    Reputation
    1725
    If you had downloaded Instant VB, as I suggested a day or two ago, then it would have handled that for you.

  6. #6
    .NET Framework
    .NET 4.0
    Join Date
    Oct 2015
    Posts
    46
    Reputation
    36
    Hi jmcilhinney,


    If you still interested in this issue I think you are going to help me
    I am VBA expert.
    I am trying to be vb.net expert.

  7. #7
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,965
    Reputation
    1725
    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.

  8. #8
    .NET Framework
    .NET 4.0
    Join Date
    Oct 2015
    Posts
    46
    Reputation
    36
    You know what, one day I will come back here with correct code Wait me here
    I am VBA expert.
    I am trying to be vb.net expert.

  9. #9
    .NET Framework
    .NET 4.0
    Join Date
    Oct 2015
    Posts
    46
    Reputation
    36
    Solved by myself.

    Code:
    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
    I am VBA expert.
    I am trying to be vb.net expert.

  10. #10
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,965
    Reputation
    1725
    Congratulations on doing the things we told you to do all by yourself.

Similar Threads

  1. Question Prevent empty textbox
    By Iswan in forum Windows Forms
    Replies: 1
    Last Post: 09-09-2011, 8:55 PM
  2. Replies: 5
    Last Post: 02-02-2010, 10:27 PM
  3. selected row is not visually selected
    By guysl in forum Winforms Grids
    Replies: 1
    Last Post: 08-04-2008, 2:40 PM
  4. prevent access to drives
    By enzom83 in forum Windows Services
    Replies: 2
    Last Post: 01-10-2008, 6:56 AM
  5. Prevent A Form From Getting Focus
    By hoverctafter in forum Windows Forms
    Replies: 2
    Last Post: 10-18-2005, 10:39 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •