这只是我的第二篇文章。我过得很愉快。
此代码有问题
For i As Integer = 1 To 7
CType(Me.Controls.Find(String.Format("CheckBox{0}", i), True)(0), CheckBox).ForeColor = myFontDialog.Color
Next`
这些控件所使用的表单基本上位于选项卡控件中,该选项卡具有删除不需要的页面的选项。唯一的问题是,如果删除其中一个具有这些控件的页面之一,则在选择字体颜色时会在运行时得到“索引超出数组范围”的信息。 我尝试使用Tabpage1.visible = false,但这实际上并未从控件中删除页面选项卡。我希望获得有关如何使整数范围以变化的整数结尾或如何使自己更容易的一些指导。我只是一个业余编码员,所以大多数较复杂的内容都有些麻烦,但是我在google和其他网站上发现的所有“答案”都没有用。
答案 0 :(得分:0)
我将创建一个复选框列表,并在关闭的选项卡上删除相应的复选框。然后,当您要更改属性时,只需遍历列表即可。
Private lstCheck As List(Of CheckBox)
Private Sub CreateCheckBoxesList()
lstCheck = New List(Of CheckBox) From {CheckBox1, CheckBox2, CheckBox3, CheckBox4, CheckBox5, CheckBox6, CheckBox7}
End Sub
Private Sub CloseTab(TabIndexClosed As Integer)
Select Case TabIndexClosed
Case 0
lstCheck.Remove(CheckBox1)
lstCheck.Remove(CheckBox2)
Case 1
lstCheck.Remove(CheckBox3)
lstCheck.Remove(CheckBox4)
Case 2
lstCheck.Remove(CheckBox5)
lstCheck.Remove(CheckBox6)
lstCheck.Remove(CheckBox7)
End Select
End Sub
Private Sub ChangeCheckBoxColor()
For Each chk As CheckBox In lstCheck
chk.ForeColor = Color.Red
Next
End Sub
根据评论进行编辑: 这是另一个取决于事件过程的示例。该列表是在Form.Load方法中创建的。我已将对CloseTab和AddTab的调用添加到您的ToolStrip。这些调用将更新复选框列表。我在表单中添加了一个按钮,用于更改复选框的文本颜色。
Private lstCheck As List(Of CheckBox)
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CreateCheckBoxesList()
End Sub
Private Sub CreateCheckBoxesList()
lstCheck = New List(Of CheckBox) From {CheckBox1, CheckBox2, CheckBox3, CheckBox4, CheckBox5, CheckBox6, CheckBox7}
End Sub
Private Sub CloseTab(TabIndexClosed As String)
Select Case TabIndexClosed
Case "Tab1"
lstCheck.Remove(CheckBox1)
lstCheck.Remove(CheckBox2)
Case "Tab2"
lstCheck.Remove(CheckBox3)
lstCheck.Remove(CheckBox4)
Case "Tab3"
lstCheck.Remove(CheckBox5)
lstCheck.Remove(CheckBox6)
lstCheck.Remove(CheckBox7)
End Select
End Sub
Private Sub AddTab(TabAdded As String)
Select Case TabAdded
Case "Tab1"
lstCheck.Add(CheckBox1)
lstCheck.Add(CheckBox2)
Case "Tab2"
lstCheck.Add(CheckBox3)
lstCheck.Add(CheckBox4)
Case "Tab3"
lstCheck.Add(CheckBox5)
lstCheck.Add(CheckBox6)
lstCheck.Add(CheckBox7)
End Select
End Sub
Private Sub btnRed_Click(sender As Object, e As EventArgs) Handles btnRed.Click
For Each chk As CheckBox In lstCheck
chk.ForeColor = Color.Red
Next
End Sub
Private Sub HideRouterFormatterToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles HideRouterFormatterToolStripMenuItem.Click
If HideRouterFormatterToolStripMenuItem.Checked Then
TabControl1.TabPages.Remove(TabPage1)
CloseTab("Tab1)")
End If
If HideRouterFormatterToolStripMenuItem.CheckState = 0 Then
TabControl1.TabPages.Add(TabPage1)
AddTab("Tab1")
End If
End Sub
我只是从您的工具条的名称中认为这可能是WPF应用程序,在这种情况下,所有赌注都无效。我假设使用Windows Forms。包括在您的问题中将是很好的信息。这可能有效,但它是为WinForms设计的。
答案 1 :(得分:0)
根据注释进行编辑:这是另一个取决于事件过程的示例。该列表是在Form.Load方法中创建的。我已将对CloseTab和AddTab的调用添加到您的ToolStrip。这些调用将更新复选框列表。我在表单中添加了一个按钮,用于更改复选框的文本颜色。
私有lstCheck为列表(复选框)
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CreateCheckBoxesList()
End Sub
Private Sub CreateCheckBoxesList()
lstCheck = New List(Of CheckBox) From {CheckBox1, CheckBox2, CheckBox3, CheckBox4, CheckBox5, CheckBox6, CheckBox7}
End Sub
Private Sub CloseTab(TabIndexClosed As String)
Select Case TabIndexClosed
Case "Tab1"
lstCheck.Remove(CheckBox1)
lstCheck.Remove(CheckBox2)
Case "Tab2"
lstCheck.Remove(CheckBox3)
lstCheck.Remove(CheckBox4)
Case "Tab3"
lstCheck.Remove(CheckBox5)
lstCheck.Remove(CheckBox6)
lstCheck.Remove(CheckBox7)
End Select
End Sub
Private Sub AddTab(TabAdded As String)
Select Case TabAdded
Case "Tab1"
lstCheck.Add(CheckBox1)
lstCheck.Add(CheckBox2)
Case "Tab2"
lstCheck.Add(CheckBox3)
lstCheck.Add(CheckBox4)
Case "Tab3"
lstCheck.Add(CheckBox5)
lstCheck.Add(CheckBox6)
lstCheck.Add(CheckBox7)
End Select
End Sub
Private Sub btnRed_Click(sender As Object, e As EventArgs) Handles btnRed.Click
For Each chk As CheckBox In lstCheck
chk.ForeColor = Color.Red
Next
End Sub
Private Sub HideRouterFormatterToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles HideRouterFormatterToolStripMenuItem.Click
If HideRouterFormatterToolStripMenuItem.Checked Then
TabControl1.TabPages.Remove(TabPage1)
CloseTab("Tab1)")
End If
If HideRouterFormatterToolStripMenuItem.CheckState = 0 Then
TabControl1.TabPages.Add(TabPage1)
AddTab("Tab1")
End If
End Sub