2017-05-08 9 views
2

VB6에서 VB.NET으로 일부 기존 코드를 마이그레이션 중이므로 성가신 문제가 발생했습니다. 현재 어떤 일이 일어나고 있는지 사용자에게 RadionButton 컨트롤이 제공되어 계층 구조를 나타냅니다. 그들이 값을 선택할 때 코드는 그것이 유효하다는 것을 확인합니다 (C가 A의 자손이 될 수없고 B의 자이어야 함). 그렇지 않으면 RadioButton을 원래 설정으로 되돌립니다.이벤트에서 RadioButton checkstate를 변경하십시오.

문제는이 작업을 이벤트 처리기로 반환하면 클릭했을 때의 상태로 RadioButton 상태를 반환합니다 (사용자가 C를 클릭하면 코드가 B로 반환되고 함수가 종료하면 C로 반환 됨). 무한 루프를 일으키는 이벤트를 발생시켜 다시 B로 바꾼다. RadioButton is selected inside a function called by CheckedChanged` 이벤트를 변경하는 방법이 있습니까?

내가 더 좋은 디자인은 유효하지 않은 컨트롤을 사전에 무효화하는 것임을 알고 있지만 이것은 설계된 방법이며 제한된 시간 프레임에서 작업하는 것이므로 나쁜 디자인으로 붙어 있습니다. 나중에 좋은 디자인에 반대했다.

코드 :

Private Sub optIndent_2_ClickEvent(sender As Object, e As EventArgs) Handles optIndent_2.CheckedChanged 
    optIndent_Click(2, sender.Checked) 
End Sub 

Private Sub optIndent_3_ClickEvent(sender As Object, e As EventArgs) Handles optIndent_3.CheckedChanged 
    optIndent_Click(3, sender.Checked) 
    Dim i As Integer = 1 
End Sub 

Private Sub optIndent_Click(ByRef Index As Short, ByRef value As Short) 
    If value = False Then 
     Exit Sub 
    End If 

    If Index = 2 Then 
     Exit Sub 
    End If 
    If Index = 3 Then 
     optIndent_2.Checked = True 
     Exit Sub 
    End If 
End Sub 

당신은 코드가 optIndent_Click (3, sender.checked) 종료 할 때 값이 false에서 true로 전환 할 것을 볼 수 있습니다, 프로세스는 영원히 반복합니다.

Specifies that an argument is passed in such a way that the called procedure can change the value of a variable underlying the argument in the calling code.

이 대신 당신이 ByVal를 사용해야합니다 :

Specifies that an argument is passed in such a way that the called procedure or property cannot change the value of a variable underlying the argument in the calling code.

문제를 해결하는 ByVal 인수로 인수 value 변경

답변

2

문제는 ByRef의 사용이다. 그러면 value이 "값"을 유지하지 않게됩니다.

I는이에 맞지 않을 수 있도록 나쁜 디자인 붙어있는 것을 감사는 범위를 프로젝트하지만, 어떤 점에서 당신은 Option Strict On을 돌려 보라 :

Restricts implicit data type conversions to only widening conversions, disallows late binding, and disallows implicit typing that results in an Object type.

ByVal value As Short 강조 것 인 인 것으로 문제 :

Option Strict On disallows implicit conversions from 'Boolean' to 'Short'.

해결 방법은 다음과 같습니다. ByVal value As Boolean. 옵션 엄격한에는 오류 인 것으로 sender.Checked을 강조 것이다 갖는

:

Option Strict On disallows late binding

수정이 RadioButton 그래서 당신이 직접 그 속성에 액세스 할 수 있습니다로 sender 캐스팅하는 것; DirectCast(sender, RadioButton).Checked.

Private Sub optIndent_2_ClickEvent(sender As Object, e As EventArgs) Handles optIndent_2.CheckedChanged 
    optIndent_Click(2, DirectCast(sender, RadioButton).Checked) 
End Sub 

Private Sub optIndent_3_ClickEvent(sender As Object, e As EventArgs) Handles optIndent_3.CheckedChanged 
    optIndent_Click(3, DirectCast(sender, RadioButton).Checked) 
End Sub 

Private Sub optIndent_Click(ByVal Index As Short, ByVal value As Boolean) 
    If value = False Then 
     Exit Sub 
    End If 

    If Index = 2 Then 
     Exit Sub 
    End If 

    If Index = 3 Then 
     optIndent_2.Checked = True 
     Exit Sub 
    End If 
End Sub 
:

코드는 같을 것이다