Programatically hide/remove tabpages in VB.NET

2019-03-02 17:41发布

I have 10 tabpages on my form. Based on an input in a textbox, I want to programmatically remove number of tab pages, i.e. if textbox input is 3 then only first 3 tabpages should be visible and tabpages 4 to 10 must be removed or should not be visible. I tried following without any success,

For i = 0 To 9
Form1.TabControl1.TabPages.Remove(Form4.TabControl1.TabPages((val(textbox1.text)) + i))
Next

(No exceptions or errors are generated for above statements)

What is wrong with these statements?

Thanks.

2条回答
孤傲高冷的网名
2楼-- · 2019-03-02 18:36

Never use your Input unfiltered. Put the Textbix1.Text Input in a integer.tryparse construct. Also, activate Option strict for better code quality.

For your Problem:

Dim MaxVisible as Integer
Dim Sucess as Boolean
Sucess=Integer.Tryparse(textbox1.text, MaxVisible)
If Sucess=True

For index As Integer = 9 To MaxVisible  + 1 Step -1
 Me.TabControl1.TabPages(Index).visible=false
End If

That should make the unwanted tabcontrols invisible. I dont know if Tabpages(index) works, maybe you must youse getitems instead - I have no Winforms Project at hand to test it. More Information on TabControl: http://msdn.microsoft.com/de-de/library/system.windows.forms.tabcontrol.aspx

查看更多
虎瘦雄心在
3楼-- · 2019-03-02 18:44

check this.

    For i As Integer = TextBox1.Text + 1 To 9

        Form1.TabControl1.TabPages.Remove(Form4.TabControl1.TabPages(TextBox1.Text + 1))

    Next

or

    For index As Integer = 9 To TextBox1.Text + 1 Step -1

        Me.TabControl1.TabPages.Remove(Me.TabControl1.TabPages(index))
    Next
查看更多
登录 后发表回答