使面板或组框可见抛出Argumentoutofrangeexception

时间:2014-08-01 12:30:53

标签: vb.net winforms data-binding

我在vb.net中有一个MVC winform应用程序,

有一个BaseController,它引用了Model,2个concreteControllers继承自这个BaseController。

InitializeComponent()实例化一个控制器之前,然后调用InitializeComponent,然后调用一些这样的绑定:

chkGeolocalizacion.DataBindings.Add("Checked", controller.Model, "SolicitarGeolocalizacion", False, DataSourceUpdateMode.OnPropertyChanged)

chkGeolocalizacion位于组框或面板内(同时尝试过)。

然后表单等待用户输入以在2个单选按钮之间进行选择并触发事件,这将用用户选择的控制器替换控制器。之后我想让组合框显示控件绑定并抛出Argumentoutofrangeexception,类似"值0不在最小值和最大值之间#34;。

如果我从不修改可见属性,代码可以完美运行。

Dim modelo As Model_Reporte = controller.Model
If rbtScoringDistancia.Checked Then
    controller = New Reporte_controller_SinK()
    chkGeolocalizacion.Visible = True
Else
    controller = New Reporte_Controller(Me)
    chkGeolocalizacion.Visible = False
End If
controller.Model = modelo
pnlConfig.Visible = True

这是一个.net错误还是什么?我无法使其工作,唯一的解决方法是使控件可见/不可见,而不是包含它们的组框或面板

(我尝试使用面板和组合框,抛出相同的异常)

谢谢!

ps:如果您需要查看更多我的代码,请求它。

编辑:添加额外代码

Public Class Reporte

Private controller As Reporte_ControllerBase

Public Sub New()
    controller = New Reporte_Controller(Me)
    InitializeComponent()
    addDataBindings()
End Sub

Private Sub addDataBindings()
    dt_fecha_desde.DataBindings.Add("Value", controller.Model, "GetFechaDesde", False, DataSourceUpdateMode.OnPropertyChanged)
    dt_fecha_hasta.DataBindings.Add("Value", controller.Model, "GetFechaHasta", False, DataSourceUpdateMode.OnPropertyChanged)
    KM_Scoring.DataBindings.Add("Value", controller.Model, "KM_Scoring", False, DataSourceUpdateMode.OnPropertyChanged)
    chkGeolocalizacion.DataBindings.Add("Checked", controller.Model, "SolicitarGeolocalizacion", False, DataSourceUpdateMode.OnPropertyChanged)
End Sub

Private Sub rbtScoringDistancia_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles rbtScoringDistancia.CheckedChanged, rbtScoringAlternativo.CheckedChanged
    Dim modelo As Model_Reporte = controller.Model
    If rbtScoringDistancia.Checked Then
        lblKm.Text = "Iteraciones"
        LblScoring.Text = "Considerar infracción a las "
        controller = New Reporte_controller_SinK()
        chkGeolocalizacion.Visible = True
    Else
        LblScoring.Text = "Evaluar scoring cada "
        lblKm.Text = "Kilómetros"
        controller = New Reporte_Controller(Me)
        chkGeolocalizacion.Visible = False
    End If
    controller.Model = modelo
    pnlConfig.Visible = True 'Exception thrown here
End Sub
End Class

Public Class Reporte_Controller
     Inherits Reporte_ControllerBase
'extra code...
 End Class


Public MustInherit class Reporte_ControllerBase
Protected modelo As Model_Reporte = New Model_Reporte


Public Property Model() As Model_Reporte
    Get
        Return modelo
    End Get
    Set(ByVal value As Model_Reporte)
        modelo = value
    End Set
End Property
End Class

Public Class Model_Reporte

    Private _getFechaDesde As Date = Date.Today.AddMonths(-1)
    Private _getfechaHasta As Date = Date.Today
    Private _kmScoring As Integer
    Private _solicitarGeolocalizacion As Boolean
    Private _limiteRegular As Int32 = 7
    Private _limiteMal As Int32 = 25

'getters and setters for each property
End Class

澄清:如果我之前将其设置为false,那么除了使面板可见属性为true之外,一切都有效。如果它是真的并且我没有修改它,那就有效。

edit2:只有在面板或组合框中创建visible = true时才会抛出异常,这些面板或组合框具有绑定到控制器模型的多个控件。在包含控件NOT binded的另一个组合框中使visible = true或false可以完美无缺地工作。

1 个答案:

答案 0 :(得分:0)

我通过在form_load事件中添加数据绑定而不是在new中添加它们(在调用InitializeComponents()之后)来修复此错误。我知道这很奇怪,但这解决了这个问题。