控制数值控件中的值

时间:2016-02-29 16:49:13

标签: .net vb.net

可能很容易,但我无法弄清楚如何实现这一点。我的表单上有3个数字控件。那里的值可以是1,2 or 3。假设这三个值中的每一个只能在其中一个控件中出现一次。所以,让我说我有

IntInputVariation1.value = 1
IntInputVariation2.value = 2
IntInputVariation3.value = 3

现在,如果有人更改其中一个值,请将IntInputVariation2.value设为3,则IntInputVariation3.value必须为2.

希望你知道我的意思。

当表单加载时,所有控件都设置为1到3,如下所示:

IntInputVariation1.value = 1
IntInputVariation2.value = 2
IntInputVariation3.value = 3

此时用户可以更改它......

到目前为止,我做到了这一点,但仍有问题。你能帮忙吗?

我试图保存我的控件的第一个状态:

Private Class VariationPositionValues
        Public Shared First As String = "IntInputVariation1"
        Public Shared Second As String = "IntInputVariation2"
        Public Shared Third As String = "IntInputVariation3"
    End Class

然后在下面的处理程序方法i关联到所有那些控制ValueChanged事件:

 Private Sub IntInputVariation_ValueChanged(sender As Object, e As EventArgs)



        Dim inputNumbControl As DevComponents.Editors.IntegerInput = CType(sender, DevComponents.Editors.IntegerInput)
        Dim NewValue As Integer = inputNumbControl.Value


        Select Case inputNumbControl.Name
            Case "IntInputVariation1"

                If IntInputVariation2.Value = NewValue Then
                    IntInputVariation2.Value = 1
                    VariationPositionValues.IntInputVariation1 = IntInputVariation1.Value
                    VariationPositionValues.IntInputVariation2 = IntInputVariation2.Value
                End If
                If IntInputVariation3.Value = NewValue Then
                    IntInputVariation3.Value = VariationPositionValues.IntInputVariation1
                    VariationPositionValues.IntInputVariation1 = IntInputVariation1.Value
                    VariationPositionValues.IntInputVariation3 = IntInputVariation3.Value
                End If

            Case "IntInputVariation2"
                If IntInputVariation1.Value = NewValue Then
                    IntInputVariation1.Value = VariationPositionValues.IntInputVariation2
                    VariationPositionValues.IntInputVariation2 = IntInputVariation2.Value
                    VariationPositionValues.IntInputVariation1 = IntInputVariation1.Value
                End If
                If IntInputVariation3.Value = NewValue Then
                    IntInputVariation3.Value = VariationPositionValues.IntInputVariation2
                    VariationPositionValues.IntInputVariation2 = IntInputVariation2.Value
                    VariationPositionValues.IntInputVariation3 = IntInputVariation3.Value
                End If

            Case "IntInputVariation3"
                If IntInputVariation1.Value = NewValue Then
                    IntInputVariation1.Value = VariationPositionValues.IntInputVariation3
                    VariationPositionValues.IntInputVariation3 = IntInputVariation3.Value
                    VariationPositionValues.IntInputVariation1 = IntInputVariation1.Value
                End If
                If IntInputVariation2.Value = NewValue Then
                    IntInputVariation2.Value = VariationPositionValues.IntInputVariation3
                    VariationPositionValues.IntInputVariation3 = IntInputVariation3.Value
                    VariationPositionValues.IntInputVariation2 = IntInputVariation2.Value
                End If
        End Select
    End Sub

进一步讨论:

  Select Case variationCount
                Case 1
                    nums = Enumerable.Range(1, 1).ToArray
                    nuds = {IntInputVariation1}
                Case 2
                    nums = Enumerable.Range(1, 2).ToArray
                    nuds = {IntInputVariation1, IntInputVariation2}
                Case 3
                    nums = Enumerable.Range(1, 3).ToArray
                    nuds = {IntInputVariation1, IntInputVariation2, IntInputVariation3}
            End Select

            ignore = False

然后当代码在第二次尝试访问该行时:

If ignore Then Exit Sub

然后出现此错误:System.IndexOutOfRangeException

Plutonix的工作解决方案:

在表单加载时得到了这个:

 variationCount = BusinessLayer.SubKategorie_Variation.GetVariationCountBySubKategorie(AdvTreeSubKategories.SelectedValue)


        Select Case variationCount
            Case 1
                IntInputVariation1.MaxValue = 1

                nums = Enumerable.Range(1, 1).ToArray
                nuds = {IntInputVariation1}
            Case 2
                IntInputVariation1.MaxValue = 2
                IntInputVariation2.MaxValue = 2

                nums = Enumerable.Range(1, 2).ToArray
                nuds = {IntInputVariation1, IntInputVariation2}
            Case 3
                IntInputVariation1.MaxValue = 3
                IntInputVariation2.MaxValue = 3
                IntInputVariation3.MaxValue = 3

                nums = Enumerable.Range(1, 3).ToArray
                nuds = {IntInputVariation1, IntInputVariation2, IntInputVariation3}
        End Select

        ignore = False

后来我得到了这个:

  If ignore Then Exit Sub

        ' dont do this while doing this ;)
        ignore = True

        '--Whho called
        Dim nud = CType(sender, DevComponents.Editors.IntegerInput)
        '--Other ctls, vals, remove the one the user changed
        Dim otherVals = nums.Except(New Int32() {nud.Value}).ToArray
        Dim others = nuds.Except(New DevComponents.Editors.IntegerInput() {nud}).ToArray

        ' set the first
        others(0).Value = otherVals(0)
        otherVals = otherVals.Except(New Int32() {others(0).Value}).ToArray
        If variationCount > 2 Then others(1).Value = otherVals(0)

        ignore = False

2 个答案:

答案 0 :(得分:0)

由于代码以及用户更改值,您可能会遇到触发该事件的问题?这使用标准NumericUpDown控件 - 事件等可能不同。

' form level vars: 
Private ignore As Boolean = True
Private nuds As NumericUpDown()
Private nums As Int32() 

在表单加载中,设置为多个:

variationCount = 3

nuds = {nud1, nud2, nud3}.Take(variationCount).ToArray()
nums = Enumerable.Range(1, variationCount).ToArray()

ignore = False            ' at the end

忽略标志是因为NET构建表单时可以忽略该事件。设置值时也需要它。

Private Sub nud3_ValueChanged(sender As Object, 
          e As EventArgs) Handles nud3.ValueChanged, nud1.ValueChanged, nud2.ValueChanged
    If ignore Then Exit Sub

    ' dont do this while doing this ;)
    ignore = True

    ' who called
    Dim nud = CType(sender, NumericUpDown)
    ' other ctls, vals, remove the one the user changed
    Dim otherVals = nums.Except(New Int32() {nud.Value}).ToArray
    Dim others = nuds.Except(New NumericUpDown() {nud}).ToArray

    ' set the first
    others(0).Value = otherVals(0)
    otherVals = otherVals.Except(New Int32() {others(0).Value}).ToArray

    If variationCount > 2 Then others(1).Value = otherVals(0)

    ignore = False
End Sub

它可能会不必要地改变其中一个,仅用3来很难说,而且没有OldValue

答案 1 :(得分:0)

所以我读了你的代码并想出了这个:

Public Class Form1

    Private _PreviousValues As New Dictionary(Of NumericUpDown, Integer)  
    Private _bIgnoreChange As Boolean = True

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        InitValues()
        SetPreviousVars()
        _bIgnoreChange = False
        ' Add any initialization after the InitializeComponent() call.

    End Sub
    Private Sub IntInputVariation_ValueChanged(sender As Object, e As EventArgs) Handles IntInputVariation1.ValueChanged, IntInputVariation2.ValueChanged, IntInputVariation3.ValueChanged

        If Not _bIgnoreChange Then
            CheckForSwapValue(DirectCast(sender, NumericUpDown), IntInputVariation1)
            CheckForSwapValue(DirectCast(sender, NumericUpDown), IntInputVariation2)
            CheckForSwapValue(DirectCast(sender, NumericUpDown), IntInputVariation3)
        End If

        SetPreviousVars()

    End Sub

    Private Sub CheckForSwapValue(changedNUD As NumericUpDown, testNUD As NumericUpDown)

        If changedNUD IsNot testNUD AndAlso changedNUD.Value = testNUD.Value Then
            _bIgnoreChange = True
            testNUD.Value = _PreviousValues(changedNUD)
            _bIgnoreChange = False
        End If

    End Sub


    Private Sub InitValues()
        IntInputVariation1.Value = 1
        IntInputVariation2.Value = 2
        IntInputVariation3.Value = 3
    End Sub

    Private Sub SetPreviousVars()
        _PreviousValues(IntInputVariation1) = CInt(IntInputVariation1.Value)
        _PreviousValues(IntInputVariation2) = CInt(IntInputVariation2.Value)
        _PreviousValues(IntInputVariation3) = CInt(IntInputVariation3.Value)
    End Sub
End Class

我觉得使用共享/静态变量对于你正在做的事情是不必要的。如果要以这种方式跟踪旧值,则只需要类级变量。

当然所有这些代码都假设你正在使用winforms和NumericUpDown控件,但它仍然适用于其他控件和WPF中的更改控件类型。