Link to home
Start Free TrialLog in
Avatar of BlakeMcKenna
BlakeMcKennaFlag for United States of America

asked on

How to initialize a TabPage?

I'm having an issue with my tabpages. I have a particular Windows form that contains a TabControl with two TabPages. The first TabPage contains a DataGridView (DGV). When a User double-clicks on a row in the DGV, the data is loaded into various controls and variables and then the second TabPage is selected.

Now, on the second TabPage, I have several ComboBox (CB) controls. The way my logic is set up is that when the User selects a row from the first TabPage, certain values are are assigned to the "SelectedValue" of some CB's. When that happens, the SelectedIndex Event of the CB's fires and processes data accordingly. This works fine except for the very first time a row is selected from TabPage 1. The CB's SelectedIndex Event fires but the different controls don't get populated correctly. Once the second TabPage is shown, then this whole process works as it should. It's just on the initial row selection from the first TabPage.

Does anyone know why this happens? I hope I've explained this issue correctly.

Thanks!
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

**Shot in the Dark**

Perhaps the second TabPage isn't being initialized properly?  In the Load() event of the Form, try iterating over all the TabPages and selecting them.  This should force initializing of the TabPage and all controls within it:
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For i As Integer = 0 To TabControl1.TabCount - 1
            TabControl1.SelectedIndex = i
            Application.DoEvents()
        Next
        TabControl1.SelectedIndex = 0
    End Sub

Open in new window

Avatar of BlakeMcKenna

ASKER

That was a great idea but unfortunately it didn't work...
Not sure then.  We may need to see some code to figure it...
What I'm trying to do is calculate a Date based on what a CB value is and then place it in a DateTimePicker control. That's the part that isn't working on the initial try. The following CB Event executes a Sub that calculates the Date Difference.

    Private Sub cmbInterval_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbInterval.SelectedIndexChanged
        'Debug.Print("cmbInterval_SelectedIndexChanged")

        Try
            EH.ErrorMessage = String.Empty

            If blnIsLoaded Then
                If cmbInterval.SelectedIndex <> -1 Then
                    iInterval_ID = CInt(cmbInterval.SelectedValue)

                    If cmbInterval.Text = String.Empty Then
                        decIntervalValue = 12
                    Else
                        decIntervalValue = cmbInterval.Text
                    End If

                    If blnCustomFormatApplied Then
                        blnCustomFormatApplied = False
                        dtCDCalDate.Format = DateTimePickerFormat.Short
                        dtCDExtensionDate.Format = DateTimePickerFormat.Short
                        dtCDCalDate.Value = Now
                        nextCalDate = Now.AddMonths(decIntervalValue)
                    Else
                        nextCalDate = DateAdd(DateInterval.Month, decIntervalValue, dtCDCalDate.Value)
                    End If

                    CalculateNextCalibrationDate(nextCalDate)

                    If EH.ErrorMessage = String.Empty Then
                        dtCDCalDate.Enabled = True
                        txtTraceabilityNO.Enabled = True
                        txtCombinedUncertainty.Enabled = True
                        dtCDCalDate.Focus()
                    End If
                End If
            End If

        Catch ex As Exception
            EH.ErrorMessage = "cmbInterval_SelectedIndexChanged() - " & ex.Message & "~E"
        End Try

        EH.ProcessMessages(Me, sbr, EH.ErrorMessage)
    End Sub
    '
    '
    '
    Private Sub CalculateNextCalibrationDate(ByVal nextCalDate As Date)
        'Debug.Print("CalculateNextCalibrationDate")

        Try
            EH.ErrorMessage = String.Empty

            If IsDate(dtCDCalDate.Value) Then
                dtNextCalibrationDate.Format = DateTimePickerFormat.Short
                nextCalDate = DateAdd(DateInterval.Month, decIntervalValue, dtCDCalDate.Value)
                dtNextCalibrationDate.Value = nextCalDate
            End If

        Catch ex As Exception
            EH.ErrorMessage = "cboCDSetDate_CheckedChanged() - " & ex.Message & "~E"
        End Try
    End Sub

Open in new window


The above code works everytime except for the initial row selection from the first TabPage.
You sure "blnIsLoaded" has been set the first time round?  Throw a Debug.Print() in there to make sure.
Positive...I've already run this through the debugger.
ASKER CERTIFIED SOLUTION
Avatar of BlakeMcKenna
BlakeMcKenna
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I just rearranged some code. I needed to change some design considerations but I finally got it working.