简体   繁体   中英

Making panel or groupbox visible throws Argumentoutofrangeexception

I have a MVC winform app in vb.net,

There is a BaseController which has a reference to a Model, and 2 concreteControllers inherit from this BaseController.

Before InitializeComponent() one controller is instantiated, then InitializeComponent is called and afterwards, some bindings like this one:

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

chkGeolocalizacion is inside a groupbox or panel (tried both).

Then the Form waits for user input to choose between 2 radio buttons and an event is triggered, which replaces the controller with the one the user chose. Afterwards I want to make visible the groupbox which has the controls binded and Argumentoutofrangeexception is thrown, with something like "value 0 is not between minimum and maximum".

The code works flawlessly if I never modify the visible property.

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

Is this a .net bug or what? I couldn't make it work and the only workaround is to make visible/invisible the controls and not the groupbox or panel that contains them

(I tried with both panel and groupbox, same exception thrown)

Thanks!

ps: if you need to see more of my code ask for it.

edit: Adding extra code

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

to clarify: Everything works except making the panel visible property on true if I set it false before. If it is true and I don't modify it, it works.

edit2: The exception is only thrown when making visible = true on panels or groupbox which have several controls binded to the model of a controller. Making visible = true or false on another groupbox which contains controls NOT binded works flawlessly.

I fixed this error by adding the databinding in the form_load event rather than adding them in the new (after InitializeComponents() is called). It's weird I know but this solved the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM