2017-05-08 1 views
2

私はVB6からVB.NETへのいくつかのレガシーコードを移行しており、厄介な問題にぶつかってきました。現在起こっているのは、ユーザーにRadionButtonの階層を示すコントロールが提供されていることです。彼らが値を選択すると、コードはそれが有効であることを検証します(CをAの子にすることはできず、Bの子でなければなりません)。もしそうでなければ、RadioButtonを元の設定に戻します。イベントでRadioButtonチェックステートを変更します

問題は、これを行う関数がイベントハンドラに戻ると、RadioButtonの状態がクリックされたときの状態に戻ります(ユーザーがクリックした場合、コードはBに戻ります。無限ループを引き起こすなど、Bに戻すなどのイベントを発生させます)。 RadioButton is selected inside a function called by CheckedChanged`イベントを変更する方法はありますか?

私はより良いデザインは前もって無効RadioButtonコントロールを無効にすることである知っているが、これは、それが設計されています方法ですし、私の仕事は、それが限られた時間枠での作業を取得することですので、私は今のように悪いデザインとこだわっています後で良いデザインに反対する。

コード:

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に移行します終了したときに、プロセスが永遠に繰り返されていることがわかります。

答えて

2

問題はByRefの使用である:

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を変更しても問題が解決されます。これにより、valueの「値」が保持されなくなります。

私は、これがこのに収まらない場合がありますので、あなたが悪いデザインで立ち往生していることを理解するには、スコープを投写するが、いくつかの点で、あなたは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 
関連する問題