如何判断您要从 WinForms 选项卡控件中移动到哪个选项卡?

发布于 2024-08-03 05:21:56 字数 321 浏览 8 评论 0原文

我需要确定用户在切换选项卡时来自和前往哪个选项卡,并可能取消切换。我尝试过取消选择、取消选择、选择、选定事件,所有这些事件都显示 e.TabPageIndex 与 sender.SelectedIndex 相同。

是否有一个事件或属性可供我使用,以便我可以确定此事件的双方,或者我是否必须将某些内容与从一个事件缓存它并在新事件中使用该值一起进行修改。

我试图避免处理取消选择/取消选择事件并缓存要在选择事件中使用的值。我已经知道我可以做到这一点,所以我问是否有更干净的方法,而不这样做。

我在 C# 和 VB 中都进行了尝试,得到了相同的结果(毫不奇怪)。

谢谢。

I need to determine which tab the user is coming from, and going to, when they switch tabs, and possibly cancel the switch. I have tried the Deselecting, Deselected, Selecting, Selected events, and all of them show the e.TabPageIndex to be the same as the sender.SelectedIndex.

Is there an event, or property, that I can use so that I can determine both sides of this, or do I have to hack something together with caching it from one event and using that value in the new event.

I am trying to avoid handling the Deselecting/Deselected events and caching the value to use in the Selecting event. I already know I can do this, so I am asking if there is a cleaner way, without doing this.

I have tried in both C# and VB, with the same results (no surprise).

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

毁梦 2024-08-10 05:21:56

看起来任何一个事件参数都不会同时携带上一个和当前选项卡的详细信息,因此您需要处理几个事件来跟踪。

至少,您需要使用 Deselected 事件来存储对先前选择的选项卡的引用。您始终可以查询 TabControl 的当前选项卡。为了进一步扩展,您还可以处理 Selected 事件来跟踪当前选项卡。

Option Strict On
Option Explicit On

Public Class Form1

    Private PreviousTab As TabPage
    Private CurrentTab As TabPage

    Private Sub TabControl1_Deselected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControl1.Deselected
        PreviousTab = e.TabPage
        Debug.WriteLine("Deselected: " + e.TabPage.Name)
    End Sub

    Private Sub TabControl1_Selected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControl1.Selected
        CurrentTab = e.TabPage
        Debug.WriteLine("Selected: " + e.TabPage.Name)
    End Sub

    Private Sub TabControl1_Selecting(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlCancelEventArgs) Handles TabControl1.Selecting
        If CurrentTab Is Nothing Then Return
        Debug.WriteLine(String.Format("Proposed change from {0} to {1}", CurrentTab.Name, e.TabPage.Name))
    End Sub

End Class

It doesn't look like any one event argument will carry the details of both the previous and current tabs, so you'll need to handle a couple of events to keep track.

At a minimum, you'd need to use the Deselected event to store a reference to the previously-selected tab. You can always query the TabControl for its current tab. To stretch a little further, you can also handle the Selected event to track the current tab.

Option Strict On
Option Explicit On

Public Class Form1

    Private PreviousTab As TabPage
    Private CurrentTab As TabPage

    Private Sub TabControl1_Deselected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControl1.Deselected
        PreviousTab = e.TabPage
        Debug.WriteLine("Deselected: " + e.TabPage.Name)
    End Sub

    Private Sub TabControl1_Selected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControl1.Selected
        CurrentTab = e.TabPage
        Debug.WriteLine("Selected: " + e.TabPage.Name)
    End Sub

    Private Sub TabControl1_Selecting(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlCancelEventArgs) Handles TabControl1.Selecting
        If CurrentTab Is Nothing Then Return
        Debug.WriteLine(String.Format("Proposed change from {0} to {1}", CurrentTab.Name, e.TabPage.Name))
    End Sub

End Class
独行侠 2024-08-10 05:21:56

您可以订阅TabControl.Deselecting。当它触发时,事件参数中的选项卡索引将是旧索引。

然后,您可以订阅 TabControl.Selected。当此事件触发时,选项卡索引将是新选择的选项卡的索引。

You can subscribe to TabControl.Deselecting. When it fires, the tab index in the event args will be the old index.

You can then subscribe to TabControl.Selected. When this event fires, the tab index will be the index of the newly selected tab.

作死小能手 2024-08-10 05:21:56

我相信选择事件中的 e.Cancel 标志应该可以做到这一点。这会将选定的选项卡发送回取消时的原始状态:

    private void tabControl1_Selecting( object sender, TabControlCancelEventArgs e )
    {
        int badIndex = 0;

        if ( tabControl1.SelectedIndex == badIndex )
            e.Cancel = true;            
    }

如果您确实需要更复杂的行为,则可以对选项卡控件进行子类化,然后重写 onSelecting 方法。这不应该轻易完成。如果将来更改控件,您的代码将会损坏。 确认选项卡控件的所有行为均得到满足(即构造函数...)

并且您必须根据反馈 编辑:。这将在选择之前在内部跟踪原始选项卡。

public class MyTab : System.Windows.Forms.TabControl
{
    int _previousTab;

    protected override void OnSelecting( TabControlCancelEventArgs e )
    {
        // Some logic here to do cool UI things, perhaps use _previousTab

        // Call the base method
        base.OnSelecting( e );           
    }
    protected override void OnDeselecting( TabControlCancelEventArgs e )
    {
        // Store the value for use later in the chain of events
        _previousTab = this.SelectedIndex;

        // Call the base method
        base.OnDeselecting( e );
    }

}

I believe the e.Cancel flag in the selecting event should do this trick. This sends the selected tab back to the original on cancel:

    private void tabControl1_Selecting( object sender, TabControlCancelEventArgs e )
    {
        int badIndex = 0;

        if ( tabControl1.SelectedIndex == badIndex )
            e.Cancel = true;            
    }

If you really need more complex beharvior, subclassing the tab control will work, Then override the onSelecting method. This shouldn't be done lightly. If the control is changed in the future you will have broken code. And you have to confirm that all the behavior of the tab control are met (ie constructors ...)

EDIT: based on feedback. This will internally track the original tab before selection.

public class MyTab : System.Windows.Forms.TabControl
{
    int _previousTab;

    protected override void OnSelecting( TabControlCancelEventArgs e )
    {
        // Some logic here to do cool UI things, perhaps use _previousTab

        // Call the base method
        base.OnSelecting( e );           
    }
    protected override void OnDeselecting( TabControlCancelEventArgs e )
    {
        // Store the value for use later in the chain of events
        _previousTab = this.SelectedIndex;

        // Call the base method
        base.OnDeselecting( e );
    }

}
请帮我爱他 2024-08-10 05:21:56

你可以这样做:

    private int _oldIndex = -1;

    private void tabControl1_Deselected(object sender, TabControlEventArgs e)
    {
        _oldIndex = e.TabPageIndex;
    }

    private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
    {
        if (_oldIndex != -1)
        {
            if (CanChangeTab(_oldIndex, e.TabPageIndex))
            {
                e.Cancel = true;
            }
        }
    }

    private bool CanChangeTab(int fromIndex, int toIndex)
    {
        // Put your logic here
    }

You could do something like that :

    private int _oldIndex = -1;

    private void tabControl1_Deselected(object sender, TabControlEventArgs e)
    {
        _oldIndex = e.TabPageIndex;
    }

    private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
    {
        if (_oldIndex != -1)
        {
            if (CanChangeTab(_oldIndex, e.TabPageIndex))
            {
                e.Cancel = true;
            }
        }
    }

    private bool CanChangeTab(int fromIndex, int toIndex)
    {
        // Put your logic here
    }
柠檬色的秋千 2024-08-10 05:21:56

您可以通过取消选择事件获取用户要离开的选项卡的索引,并将其存储在变量中以供以后使用:

Private Sub TabControl1_Deselecting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TabControlCancelEventArgs) Handles TabControl1.Deselecting
    someClassLevelVariable = e.TabPageIndex
End Sub

您希望放置代码以防止在选择事件中进行切换。下面是 VB.NET 中的一个示例,它将阻止您选择选项卡控件上的选项卡 2:

Private Sub TabControl1_Selecting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TabControlCancelEventArgs) Handles TabControl1.Selecting
    If (e.TabPageIndex = 1) Then
        e.Cancel = True
    End If
End Sub

You can get the index of the tab the user is moving away from with the Deselecting event and store it in a variable for later use:

Private Sub TabControl1_Deselecting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TabControlCancelEventArgs) Handles TabControl1.Deselecting
    someClassLevelVariable = e.TabPageIndex
End Sub

You want put code to prevent the switch in the Selecting event. Here's an example in VB.NET that will prevent you from selecting tab 2 on a tab control:

Private Sub TabControl1_Selecting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TabControlCancelEventArgs) Handles TabControl1.Selecting
    If (e.TabPageIndex = 1) Then
        e.Cancel = True
    End If
End Sub
转角预定愛 2024-08-10 05:21:56

为了提供我最初答案的替代方案......这里是扩展 TabControl 的示例,它修改事件参数以包含更多详细信息。

免责声明这是拼凑在一起的,肯定需要一些调整!

Option Strict On
Option Explicit On

Imports System
Imports System.Windows.Forms

Public Class ExtendedTabControl
    Inherits TabControl

    Public Shadows Event Selecting As EventHandler(Of SelectedTabChangingEventArgs)
    Public Shadows Event Selected As EventHandler(Of SelectedTabChangedEventArgs)

    Private _PreviousTab As TabPage
    Public Property PreviousTab() As TabPage
        Get
            Return _PreviousTab
        End Get
        Private Set(ByVal value As TabPage)
            _PreviousTab = value
        End Set
    End Property

    Private _CurrentTab As TabPage
    Public Property CurrentTab() As TabPage
        Get
            Return _CurrentTab
        End Get
        Private Set(ByVal value As TabPage)
            _CurrentTab = value
        End Set
    End Property

    Protected Overrides Sub OnDeselected(ByVal e As System.Windows.Forms.TabControlEventArgs)
        PreviousTab = e.TabPage
        MyBase.OnDeselected(e)
    End Sub

    Protected Overrides Sub OnSelected(ByVal e As System.Windows.Forms.TabControlEventArgs)
        CurrentTab = e.TabPage
        Dim selectedArgs As New SelectedTabChangedEventArgs(e, PreviousTab)
        RaiseEvent Selected(Me, selectedArgs)
    End Sub

    Protected Overrides Sub OnSelecting(ByVal e As System.Windows.Forms.TabControlCancelEventArgs)
        Dim selectedArgs As New SelectedTabChangingEventArgs(e, CurrentTab)
        RaiseEvent Selecting(Me, selectedArgs)
    End Sub

End Class

Public Class SelectedTabChangingEventArgs
    Inherits TabControlCancelEventArgs

    Public Sub New(ByVal selectingEventArgs As TabControlCancelEventArgs, ByVal previousTabPage As TabPage)
        MyBase.New(selectingEventArgs.TabPage, selectingEventArgs.TabPageIndex, selectingEventArgs.Cancel, selectingEventArgs.Action)
        Me.CurrentTab = previousTabPage
    End Sub

    Private _CurrentTab As TabPage
    Public Property CurrentTab() As TabPage
        Get
            Return _CurrentTab
        End Get
        Set(ByVal value As TabPage)
            _CurrentTab = value
        End Set
    End Property

End Class

Public Class SelectedTabChangedEventArgs
    Inherits TabControlEventArgs

    Public Sub New(ByVal selectedEventArgs As TabControlEventArgs, ByVal previousTabPage As TabPage)
        MyBase.New(selectedEventArgs.TabPage, selectedEventArgs.TabPageIndex, selectedEventArgs.Action)
        Me.PreviousTab = previousTabPage
    End Sub

    Private _PreviousTab As TabPage
    Public Property PreviousTab() As TabPage
        Get
            Return _PreviousTab
        End Get
        Set(ByVal value As TabPage)
            _PreviousTab = value
        End Set
    End Property

End Class

...以及使用此控件的示例表单...

Option Strict On
Option Explicit On

Public Class Form1

    Private Sub TabControl1_Selecting(ByVal sender As Object, ByVal e As SelectedTabChangingEventArgs) Handles TabControl1.Selecting
        If e.CurrentTab Is Nothing Then Return
        Console.WriteLine(String.Format("Proposed change from {0} to {1}", e.CurrentTab.Name, e.TabPage.Name))
    End Sub

    Private Sub TabControl1_Selected(ByVal sender As Object, ByVal e As SelectedTabChangedEventArgs) Handles TabControl1.Selected
        Console.WriteLine(String.Format("Changed from {0} to {1}", e.PreviousTab.Name, e.TabPage.Name))
    End Sub

End Class

To provide an alternative to my initial answer.... here's a sample of an extended TabControl which modifies the event arguments to include some more details.

Disclaimer This is slapped together, it'll need some adjustments for sure!

Option Strict On
Option Explicit On

Imports System
Imports System.Windows.Forms

Public Class ExtendedTabControl
    Inherits TabControl

    Public Shadows Event Selecting As EventHandler(Of SelectedTabChangingEventArgs)
    Public Shadows Event Selected As EventHandler(Of SelectedTabChangedEventArgs)

    Private _PreviousTab As TabPage
    Public Property PreviousTab() As TabPage
        Get
            Return _PreviousTab
        End Get
        Private Set(ByVal value As TabPage)
            _PreviousTab = value
        End Set
    End Property

    Private _CurrentTab As TabPage
    Public Property CurrentTab() As TabPage
        Get
            Return _CurrentTab
        End Get
        Private Set(ByVal value As TabPage)
            _CurrentTab = value
        End Set
    End Property

    Protected Overrides Sub OnDeselected(ByVal e As System.Windows.Forms.TabControlEventArgs)
        PreviousTab = e.TabPage
        MyBase.OnDeselected(e)
    End Sub

    Protected Overrides Sub OnSelected(ByVal e As System.Windows.Forms.TabControlEventArgs)
        CurrentTab = e.TabPage
        Dim selectedArgs As New SelectedTabChangedEventArgs(e, PreviousTab)
        RaiseEvent Selected(Me, selectedArgs)
    End Sub

    Protected Overrides Sub OnSelecting(ByVal e As System.Windows.Forms.TabControlCancelEventArgs)
        Dim selectedArgs As New SelectedTabChangingEventArgs(e, CurrentTab)
        RaiseEvent Selecting(Me, selectedArgs)
    End Sub

End Class

Public Class SelectedTabChangingEventArgs
    Inherits TabControlCancelEventArgs

    Public Sub New(ByVal selectingEventArgs As TabControlCancelEventArgs, ByVal previousTabPage As TabPage)
        MyBase.New(selectingEventArgs.TabPage, selectingEventArgs.TabPageIndex, selectingEventArgs.Cancel, selectingEventArgs.Action)
        Me.CurrentTab = previousTabPage
    End Sub

    Private _CurrentTab As TabPage
    Public Property CurrentTab() As TabPage
        Get
            Return _CurrentTab
        End Get
        Set(ByVal value As TabPage)
            _CurrentTab = value
        End Set
    End Property

End Class

Public Class SelectedTabChangedEventArgs
    Inherits TabControlEventArgs

    Public Sub New(ByVal selectedEventArgs As TabControlEventArgs, ByVal previousTabPage As TabPage)
        MyBase.New(selectedEventArgs.TabPage, selectedEventArgs.TabPageIndex, selectedEventArgs.Action)
        Me.PreviousTab = previousTabPage
    End Sub

    Private _PreviousTab As TabPage
    Public Property PreviousTab() As TabPage
        Get
            Return _PreviousTab
        End Get
        Set(ByVal value As TabPage)
            _PreviousTab = value
        End Set
    End Property

End Class

...and a sample Form using this control...

Option Strict On
Option Explicit On

Public Class Form1

    Private Sub TabControl1_Selecting(ByVal sender As Object, ByVal e As SelectedTabChangingEventArgs) Handles TabControl1.Selecting
        If e.CurrentTab Is Nothing Then Return
        Console.WriteLine(String.Format("Proposed change from {0} to {1}", e.CurrentTab.Name, e.TabPage.Name))
    End Sub

    Private Sub TabControl1_Selected(ByVal sender As Object, ByVal e As SelectedTabChangedEventArgs) Handles TabControl1.Selected
        Console.WriteLine(String.Format("Changed from {0} to {1}", e.PreviousTab.Name, e.TabPage.Name))
    End Sub

End Class
心作怪 2024-08-10 05:21:56

tabControl 有一个非公共成员“lastSelection”,其中包含您想要的信息。不幸的是,我没有找到实现它的方法。当他们拥有你想要的东西但不让你使用时,这是非常令人沮丧的。

The tabControl has a non-public member "lastSelection" that has the info you want. Unfortunately, I don't see a way to get to it. It's very frustrating when they have what you want but don't let you use it.

清浅ˋ旧时光 2024-08-10 05:21:56

在 Deselect_Event 中试试这个:

private void tabControl1_Deselected(object sender, TabControlEventArgs e)
{
    var test = e.TabPage.Text;
}

Try this in Deselect_Event:

private void tabControl1_Deselected(object sender, TabControlEventArgs e)
{
    var test = e.TabPage.Text;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文