我怎样才能将这段代码简化为一个过程

发布于 2024-12-27 02:28:51 字数 2308 浏览 2 评论 0原文

我对整个 .NET 事物还比较陌生,因为我有 VB 经典背景。

在我的表单上,我有一个选项卡控件,有 4 个选项卡。大多数代码是使用共享处理程序来处理的,但对于其他代码,我必须为每个代码编写一个处理程序。

如何将这些例程优化为单个程序?

    Private Sub cboCalc0_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCalc0.SelectedIndexChanged
    'Page 0
    If (Not (IsNothing(trvSignals0.SelectedNode)) And txtSignalName0.Enabled = True) AndAlso trvSignals0.SelectedNode.Level = 3 Then
        tempChannelProp(0, trvSignals0.SelectedNode.Tag).CalcVariant = cboCalc0.SelectedIndex
    End If
End Sub

Private Sub cboCalc1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCalc1.SelectedIndexChanged
    'Page 1
    If (Not (IsNothing(trvSignals1.SelectedNode)) And txtSignalName0.Enabled = True) AndAlso trvSignals1.SelectedNode.Level = 3 Then
        tempChannelProp(1, trvSignals1.SelectedNode.Tag).CalcVariant = cboCalc1.SelectedIndex
    End If
End Sub

Private Sub cboCalc2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCalc2.SelectedIndexChanged
    'Page 2
    If (Not (IsNothing(trvSignals2.SelectedNode)) And txtSignalName2.Enabled = True) AndAlso trvSignals2.SelectedNode.Level = 3 Then
        tempChannelProp(2, trvSignals2.SelectedNode.Tag).CalcVariant = cboCalc2.SelectedIndex
    End If
End Sub

Private Sub cboCalc3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCalc3.SelectedIndexChanged
    'Page 3
    If (Not (IsNothing(trvSignals3.SelectedNode)) And txtSignalName3.Enabled = True) AndAlso trvSignals3.SelectedNode.Level = 3 Then
        tempChannelProp(3, trvSignals3.SelectedNode.Tag).CalcVariant = cboCalc3.SelectedIndex
    End If
End Sub

我已经按如下方式处理了其他部分,并且效果很好,但我只是不知道如何使用上面的代码来完成它。

    Private Sub trvSignals_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles trvSignals0.AfterCheck, trvSignals1.AfterCheck, trvSignals2.AfterCheck, trvSignals3.AfterCheck
    'Handles Page 0,1,2,3
    sender.SelectedNode = e.Node
    If e.Node.Level = 3 Then
        tempChannelProp(sender.tag, e.Node.Tag).Active = e.Node.Checked
    End If
End Sub

我使用每个页面控件的 tag 属性来适当地保存 0、1、2 或 3。

谢谢格雷厄姆

I am relatively new to the whole .NET thing, coming from a VB classic background.

On my form I have a tabcontrol, with 4 tabs. Most of the code is handled using a shared handler, but for the others I have to write a handler for each.

How can I optimize these routines into a single procedure?

    Private Sub cboCalc0_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCalc0.SelectedIndexChanged
    'Page 0
    If (Not (IsNothing(trvSignals0.SelectedNode)) And txtSignalName0.Enabled = True) AndAlso trvSignals0.SelectedNode.Level = 3 Then
        tempChannelProp(0, trvSignals0.SelectedNode.Tag).CalcVariant = cboCalc0.SelectedIndex
    End If
End Sub

Private Sub cboCalc1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCalc1.SelectedIndexChanged
    'Page 1
    If (Not (IsNothing(trvSignals1.SelectedNode)) And txtSignalName0.Enabled = True) AndAlso trvSignals1.SelectedNode.Level = 3 Then
        tempChannelProp(1, trvSignals1.SelectedNode.Tag).CalcVariant = cboCalc1.SelectedIndex
    End If
End Sub

Private Sub cboCalc2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCalc2.SelectedIndexChanged
    'Page 2
    If (Not (IsNothing(trvSignals2.SelectedNode)) And txtSignalName2.Enabled = True) AndAlso trvSignals2.SelectedNode.Level = 3 Then
        tempChannelProp(2, trvSignals2.SelectedNode.Tag).CalcVariant = cboCalc2.SelectedIndex
    End If
End Sub

Private Sub cboCalc3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCalc3.SelectedIndexChanged
    'Page 3
    If (Not (IsNothing(trvSignals3.SelectedNode)) And txtSignalName3.Enabled = True) AndAlso trvSignals3.SelectedNode.Level = 3 Then
        tempChannelProp(3, trvSignals3.SelectedNode.Tag).CalcVariant = cboCalc3.SelectedIndex
    End If
End Sub

I have handled the other bits as follows, and it works great, but I just cannot figure out how to do it with code like that above.

    Private Sub trvSignals_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles trvSignals0.AfterCheck, trvSignals1.AfterCheck, trvSignals2.AfterCheck, trvSignals3.AfterCheck
    'Handles Page 0,1,2,3
    sender.SelectedNode = e.Node
    If e.Node.Level = 3 Then
        tempChannelProp(sender.tag, e.Node.Tag).Active = e.Node.Checked
    End If
End Sub

I use the tag property of the control of each page to hold either 0,1,2 or 3 as appropriate.

Thanks

Graham

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

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

发布评论

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

评论(3

听风吹 2025-01-03 02:28:51

创建可以通过索引引用的控件数组,并将索引放入每个控件的标签中。

Class MyForm

    Private ReadOnly cboCalcArray As ComboBox()
    Private ReadOnly trvSignalsArray As TreeView()
    Private ReadOnly txtSignalNameArray As TextBox()

    Public Sub New()
        InitializeComponent()
        cboCalcArray = new ComboBox() {cboCalc0, cboCalc1, cboCalc2, cboCalc3}
        trvSignalsArray = new TreeView() {trvSignals0, trvSignals1, trvSignals2, trvSignals3}
        txtSignalNameArray = new TextBox() {txtSignalName0, txtSignalName1, txtSignalName2, txtSignalName3}
    End Sub

    Private Sub cboCalcX_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) _
        Handles cboCalc0.SelectedIndexChanged, 
                cboCalc1.SelectedIndexChanged, 
                cboCalc2.SelectedIndexChanged, 
                cboCalc3.SelectedIndexChanged
        Dim index As Integer = sender.Tag
        If (Not (IsNothing(trvSignalsArray(index).SelectedNode)) And txtSignalNameArray(index).Enabled = True) AndAlso trvSignals2.SelectedNode.Level = 3 Then      
            tempChannelProp(index, trvSignalsArray(index).SelectedNode.Tag).CalcVariant = cboCalcArray(index).SelectedIndex      
        End If      
    End Sub
End Class

Create control arrays that you can reference by index, and put the index in each controls' Tag.

Class MyForm

    Private ReadOnly cboCalcArray As ComboBox()
    Private ReadOnly trvSignalsArray As TreeView()
    Private ReadOnly txtSignalNameArray As TextBox()

    Public Sub New()
        InitializeComponent()
        cboCalcArray = new ComboBox() {cboCalc0, cboCalc1, cboCalc2, cboCalc3}
        trvSignalsArray = new TreeView() {trvSignals0, trvSignals1, trvSignals2, trvSignals3}
        txtSignalNameArray = new TextBox() {txtSignalName0, txtSignalName1, txtSignalName2, txtSignalName3}
    End Sub

    Private Sub cboCalcX_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) _
        Handles cboCalc0.SelectedIndexChanged, 
                cboCalc1.SelectedIndexChanged, 
                cboCalc2.SelectedIndexChanged, 
                cboCalc3.SelectedIndexChanged
        Dim index As Integer = sender.Tag
        If (Not (IsNothing(trvSignalsArray(index).SelectedNode)) And txtSignalNameArray(index).Enabled = True) AndAlso trvSignals2.SelectedNode.Level = 3 Then      
            tempChannelProp(index, trvSignalsArray(index).SelectedNode.Tag).CalcVariant = cboCalcArray(index).SelectedIndex      
        End If      
    End Sub
End Class
薄凉少年不暖心 2025-01-03 02:28:51

您可以利用以下几个功能:

1) VB Handles 语句可以将多个控制事件挂接到同一方法。

2)您可以从发送者那里获取组合框,然后提取索引。

3) 从索引中,您可以找到其他关联的控件。

Private Sub cboCalc_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCalc0.SelectedIndexChanged, cboCalc1.SelectedIndexChanged, cboCalc2.SelectedIndexChanged, cboCalc3.SelectedIndexChanged

    Dim oCombo As ComboBox

    oCombo = DirectCast(sender, ComboBox)

    Dim wIndex As Integer
    Dim oTree As TreeView
    Dim oTextBox As TextBox

    wIndex = CInt(oCombo.Name.Substring(oCombo.Name.Length - 1))

    oTree = DirectCast(Me.Controls.Find("trvSignals" & wIndex.ToString, True)(0), TreeView)
    oTextBox = DirectCast(Me.Controls.Find("txtSignalName" & wIndex.ToString, True)(0), TextBox)

    If oTree.SelectedNode IsNot Nothing AndAlso oTextBox.Enabled AndAlso oTree.SelectedNode.Level = 3 Then
        tempChannelProp(wIndex, oTree.SelectedNode.Tag).CalcVariant = oCombo.SelectedIndex
    End If

End Sub

You can leverage a couple of features:

1) The VB Handles statement can hook multiple control events to the same method.

2) You can get the combobox from the sender, then extract the index.

3) From the index, you can find the other associated controls.

Private Sub cboCalc_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCalc0.SelectedIndexChanged, cboCalc1.SelectedIndexChanged, cboCalc2.SelectedIndexChanged, cboCalc3.SelectedIndexChanged

    Dim oCombo As ComboBox

    oCombo = DirectCast(sender, ComboBox)

    Dim wIndex As Integer
    Dim oTree As TreeView
    Dim oTextBox As TextBox

    wIndex = CInt(oCombo.Name.Substring(oCombo.Name.Length - 1))

    oTree = DirectCast(Me.Controls.Find("trvSignals" & wIndex.ToString, True)(0), TreeView)
    oTextBox = DirectCast(Me.Controls.Find("txtSignalName" & wIndex.ToString, True)(0), TextBox)

    If oTree.SelectedNode IsNot Nothing AndAlso oTextBox.Enabled AndAlso oTree.SelectedNode.Level = 3 Then
        tempChannelProp(wIndex, oTree.SelectedNode.Tag).CalcVariant = oCombo.SelectedIndex
    End If

End Sub
浅沫记忆 2025-01-03 02:28:51

某处可能有更优雅的解决方案来解决您的问题。我发现,如果我为选项卡控件上的多个选项卡定义相同的控件和方法,那么可能是时候检查我的设计了。 。 。但并非总是如此。 :-)

这可能不太笨拙,尽管使用 Select Case 在 UI 元素的其他相同配置之间进行选择会让我感到惊慌(但是,它确实将所有代码放入一个处理程序中):

    Private Sub PerformCalcs(ByVal sender As Object, ByVal e As System.EventArgs) _
        Handles _
        cboCalc0.SelectedIndexChanged,
        cboCalc1.SelectedIndexChanged,
        cboCalc2.SelectedIndexChanged,
        cboCalc3.SelectedIndexChanged

        Dim cboCalc As ComboBox = DirectCast(sender, ComboBox)

        ' The Parent of the combo box is the TabPage, and the Parent of the TabPage is the TabControl:
        Dim t As TabControl = cboCalc.Parent.Parent

        Dim SelectedNode As TreeNode
        Dim TxtSignalName As TextBox

        ' The TabControl knows the index value of the currently selected tab:
        Select Case t.SelectedIndex
            Case 0 'Page 0
                SelectedNode = trvSignals0.SelectedNode
                TxtSignalName = txtSignalName0
            Case 1 ' Page 1
                SelectedNode = trvSignals1.SelectedNode
                TxtSignalName = txtSignalName1
            Case 2 ' Page 2
                SelectedNode = trvSignals2.SelectedNode
                TxtSignalName = txtSignalName2
            Case 3 ' Page 3
                SelectedNode = trvSignals3.SelectedNode
                TxtSignalName = txtSignalName3
            Case Else ' Ooops! Something horrible happened!
                Throw New System.Exception("You have passed an invalid Control as a parameter")
        End Select

        If (Not (IsNothing(SelectedNode)) And TxtSignalName.Enabled = True) AndAlso trvSignals3.SelectedNode.Level = 3 Then
            tempChannelProp(3, SelectedNode.Tag).CalcVariant = cboCalc.SelectedIndex
        End If

    End Sub

There is likely a more elegant solution to your problem somewhere. I find that, if I am defining the same controls and methods for multiplpe tabs on a tab control, it might be time to examine my design . . . Not always, though. :-)

This might be a little less clumsy, although using Select Case to choose between otherwise identical configurations of UI elements gives me the heebie-jeebies (It DOES get all your code into one handler, however):

    Private Sub PerformCalcs(ByVal sender As Object, ByVal e As System.EventArgs) _
        Handles _
        cboCalc0.SelectedIndexChanged,
        cboCalc1.SelectedIndexChanged,
        cboCalc2.SelectedIndexChanged,
        cboCalc3.SelectedIndexChanged

        Dim cboCalc As ComboBox = DirectCast(sender, ComboBox)

        ' The Parent of the combo box is the TabPage, and the Parent of the TabPage is the TabControl:
        Dim t As TabControl = cboCalc.Parent.Parent

        Dim SelectedNode As TreeNode
        Dim TxtSignalName As TextBox

        ' The TabControl knows the index value of the currently selected tab:
        Select Case t.SelectedIndex
            Case 0 'Page 0
                SelectedNode = trvSignals0.SelectedNode
                TxtSignalName = txtSignalName0
            Case 1 ' Page 1
                SelectedNode = trvSignals1.SelectedNode
                TxtSignalName = txtSignalName1
            Case 2 ' Page 2
                SelectedNode = trvSignals2.SelectedNode
                TxtSignalName = txtSignalName2
            Case 3 ' Page 3
                SelectedNode = trvSignals3.SelectedNode
                TxtSignalName = txtSignalName3
            Case Else ' Ooops! Something horrible happened!
                Throw New System.Exception("You have passed an invalid Control as a parameter")
        End Select

        If (Not (IsNothing(SelectedNode)) And TxtSignalName.Enabled = True) AndAlso trvSignals3.SelectedNode.Level = 3 Then
            tempChannelProp(3, SelectedNode.Tag).CalcVariant = cboCalc.SelectedIndex
        End If

    End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文