我需要确定用户来自哪个选项卡,以及切换标签时可能取消切换的选项卡。我尝试了取消选择,取消选择,选择,选定的事件,并且所有这些事件都显示e.TabPageIndex与sender.SelectedIndex相同。
是否有可以使用的事件或属性,以便我可以确定这方面的两个方面,或者我是否必须通过从一个事件缓存并在新事件中使用该值来破解某些内容。
我正在尝试避免处理取消选择/取消选择的事件并缓存要在Selecting事件中使用的值。我已经知道我可以做到这一点,所以我问是否有一种更清洁的方式,而不是这样做。
我在C#和VB中都尝试过,结果相同(毫不奇怪)。
感谢。
答案 0 :(得分:8)
看起来任何一个事件参数都不会包含前一个和当前选项卡的详细信息,因此您需要处理几个事件以便跟踪。
至少,您需要使用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
答案 1 :(得分:2)
我相信选择事件中的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 );
}
}
答案 2 :(得分:1)
您可以订阅TabControl.Deselecting。当它触发时,事件args中的选项卡索引将是旧索引。
然后,您可以订阅TabControl.Selected。触发此事件时,选项卡索引将是新选择的选项卡的索引。
答案 3 :(得分:1)
您可以通过取消选择事件获取用户正在远离的选项卡的索引,并将其存储在变量中供以后使用:
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
答案 4 :(得分:1)
你可以这样做:
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
}
答案 5 :(得分:1)
提供我最初答案的替代方案....这里是一个扩展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
答案 6 :(得分:1)
tabControl有一个非公开成员“lastSelection”,它包含您想要的信息。不幸的是,我没有办法找到它。当他们拥有你想要的但却不让你使用它时,这是非常令人沮丧的。
答案 7 :(得分:1)
在Deselect_Event:
中尝试此操作private void tabControl1_Deselected(object sender, TabControlEventArgs e)
{
var test = e.TabPage.Text;
}