VB.net 禁用 TabControl 的 TabPage
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19152181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.net disable a TabPage of a TabControl
提问by Marc Intes
I currently have a form that uses TabControl
which has 5 TabPages
. I want to create a button that could disable a specific TabPage
.
我目前有一个使用TabControl
5的表格TabPages
。我想创建一个可以禁用特定TabPage
.
I have tried
我试过了
TabPage1.Enabled = False
But it does not work. How do I do this?
但它不起作用。我该怎么做呢?
回答by Archlight
you need to use the TabPages collection. Add a button to your form and try this
您需要使用 TabPages 集合。在您的表单中添加一个按钮并尝试此操作
Private Sub Button1_Click( sender As Object, e As EventArgs) Handles Button1.Click
TabControl1.TabPages(0).Enabled =false
End Sub
It's a base zero array, so in your case it should be from 0-4.
它是一个基数为零的数组,所以在你的情况下它应该是 0-4。
Or you can access it from the text of the tab
或者您可以从选项卡的文本中访问它
Private Sub Button2_Click( sender As Object, e As EventArgs) Handles Button2.Click
Dim tabPage As TabPage
For Each tabPage In TabControl1.TabPages
If tabPage.Text ="TabPage2"
tabPage.Enabled =False
End If
Next
End Sub
回答by j3josh6
Currently, the following two code blocks does the same thing: disables all the controls on that TabPage (Sets Control.Enabled = False). The tab itself is still visible and selectable from the TabControl, it is not hidden. The tab is selectable and all the elements appear disabled.
目前,以下两个代码块执行相同的操作:禁用该 TabPage 上的所有控件(设置 Control.Enabled = False)。选项卡本身仍然可见并可从 TabControl 中选择,它并未隐藏。该选项卡是可选的,并且所有元素都显示为禁用。
TabMyTab.Enabled = False
MyTabControl.TabPages(4).Enabled = False
where the TabPages(4) is the 5th in the TabControl collection.
MyTabControl.TabPages(4).Enabled = False
其中 TabPages(4) 是 TabControl 集合中的第 5 个。
Your initial code should work if that is your intent.
如果这是您的意图,您的初始代码应该可以工作。
If you want to disable the tab similar to i.e. button.Enabled = False
which does not allow the control to be used, you will need to do something different as disabling a TabPage as in the code above disables all controls in that tab. If this is what you want, keep reading. A lot of programmers suggest using the TabControl to disallow the tab from being selected by selecting a different or the previously selected tab. This is the most effective way I know. I would implement this as follows:
如果您想禁用类似于 iebutton.Enabled = False
不允许使用控件的选项卡,您需要做一些不同的事情,因为禁用 TabPage 就像在上面的代码中禁用该选项卡中的所有控件一样。如果这是您想要的,请继续阅读。许多程序员建议使用 TabControl 通过选择不同的或以前选择的选项卡来禁止选择选项卡。这是我知道的最有效的方法。我将按如下方式实施:
Private PreviousTab As New TabPage
Private CurrentTab As New TabPage
Private Sub TabControlName_Deselected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControlName.Deselected
PreviousTab = e.TabPage
End Sub
Private Sub TabControlName_Selected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControlName.Selected
CurrentTab = e.TabPage
If (PreviousTab.Name <> CurrentTab.Name) And (CurrentTab.Name = UnselectableTab.Name) Then
MessageBox.Show("Tab disabled.", "Selection Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
TabControlName.SelectedTab = PreviousTab
End If
End Sub
Substitute your own values for "UnselectableTab" and "TabControlName" for your project.
将您自己的值替换为您的项目的“UnselectableTab”和“TabControlName”。
回答by Sevvn
You can combine the use of disabling the tab, that way the behavior is dynamic if you change which tabs are enabled or disabled in code.
您可以结合使用禁用选项卡,如果您更改代码中启用或禁用哪些选项卡,则行为是动态的。
Private Sub TabControl1_Deselected(sender As Object, e As TabControlEventArgs) Handles TabControl1.Deselected
PreviousTab = e.TabPage
End Sub
.
.
Private Sub TabControl1_Selected(sender As Object, e As TabControlEventArgs) Handles TabControl1.Selected
If Not e.TabPage.Enabled Then
TabControl1.SelectedTab = PreviousTab
End If
End Sub
回答by anpadia
You can disabled a tab by setting its Enabled
property:
您可以通过设置其Enabled
属性来禁用选项卡:
TabControl1.TabPages("tbPage1").Enabled = False
TabControl1.TabPages("tbPage1").Enabled = False