[RESOLVED] 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 for this issue.
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
But I have no idea how that vb.net code works.
So, please somebody explain me how that vb.net code works in WPF.
Re: How to prevent a TabItem from being selected
There are lots of code converters around the web. Try using one or more of them and then, if you have any issues, we can help you fix them.
Alternatively, download Instant VB from Tangible Software Solutions. There's a free version and it will likely produce an error-free result.
Re: How to prevent a TabItem from being selected
I tried http://converter.telerik.com/.
But converted code doesnt work.
Re: How to prevent a TabItem from being selected
Quote:
Originally Posted by
Kram Kramer
But converted code doesnt work.
Did you miss this part of my previous post:
Quote:
if you have any issues, we can help you fix them.
I'm not seeing where you've provided any information about those issues. If you want use to help you with conversion issues then you should be providing the original code (as formatted text in your post, not an off-site image) and the converted code, along with specific details of what "doesn't work" actually means so that we have an idea what to look for. That's how you help us help you.
Quote:
Alternatively, download Instant VB from Tangible Software Solutions. There's a free version and it will likely produce an error-free result.
Did you try that?
Re: How to prevent a TabItem from being selected
@jmcilhinney
I have updated my question.
Re: How to prevent a TabItem from being selected
For one thing, the first method is a constructor, so the code it contains should be put into the New sub in your VB window, because that's the constructor. Also, does your window contain a TabControl named 'tab'?
1 Attachment(s)
Re: How to prevent a TabItem from being selected
xaml
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>
vb
Code:
Class MainWindow
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
End Class
Picture which shows errors https://prnt.sc/ijtvm4
Re: How to prevent a TabItem from being selected
So, you're just ignoring the bit about the constructor? Create an appropriate window, i.e. a window with the appropriate control(s) and then add a constructor to the code. Next, copy the code from the first method (which is a constructor in the C# code) and then copy the second method (the event handler) as is. If you still have issues, please describe them clearly. Don't post screenshots. Post the code, highlight the lines that are at issue and describe the issues, which means providing error messages if there are any.
Re: How to prevent a TabItem from being selected
@jmcilhinney
I am very fresh in vb.net programming.
So, I need completed code which is ready to run.
Because I have no idea what you mean :)
Re: How to prevent a TabItem from being selected
If all you're going to do is copy code from elsewhere then that's your prerogative but that's not programming, so I'm not much interested in helping you do that. If you want to learn how to program then you actually need to learn the basics first and then apply them. People should be able to use terms like class, method, property, constructor, event, etc and you should know what they mean or know how to find out. We shouldn't have to teach the basics each time we answer a specific question. As I said, what you do is up to you but if you want to jump in without a decent grounding in the fundamentals then you're going to get stuck on easy stuff and I don't see it as my job to help people with the easy stuff that they can find for themselves without much effort. I want to help with the hard stuff that people have attempted but can't get on their own. How to write a constructor doesn't fall into that category as far as I'm concerned. Others may feel differently and you're welcome to wait and see if someone else does, but It would be a good idea to research the fundamentals in the mean time.
Re: How to prevent a TabItem from being selected
I have searched for constructors in the web.
Constructors are very difficult for me to understand.
So, I need completed code which is ready to run.
Re: How to prevent a TabItem from being selected
So you spent less than 22 minutes researching constructors and decided that it was too hard? I'll leave you to it.
Re: How to prevent a TabItem from being selected
I dont think constructors are difficult.
Code from https://joshsmithonwpf.wordpress.com...eing-selected/ is so difficult for newbies like me.
You can help me, I trust you ;)
Re: How to prevent a TabItem from being selected
Based from the screenshot provided, you need to import the System.ComponentModel namespace to use the CurrentChangingEventArgs class and make sure to add a checkbox control with name chk in your XAML since it's looking for that control which does not exist in your XAML.
- kgc
Re: How to prevent a TabItem from being selected
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