Load chart into tabControl in Visual Basic .NET

José Carlos 886 Reputation points
2023-07-07T19:47:59.09+00:00

I have a form with a tabcontrol with two tabPage. Each tabPage has a graphic. I would like that as soon as I load the form, the tabPage1 graphic is loaded automatically.

In the code below, where would the error be, because it does not load graph 1 automatically. It only works if you click on tabPage2 and then go back to tabPage1. There the graph is drawn. I couldn't find the error.

Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TabControl1.SelectedIndexChanged
    If TabControl1.SelectedTab Is TabPage1 Then
        ' Load the graphic to TabPage1 tab
        LoadChart1()
    ElseIf TabControl1.SelectedTab Is TabPage2 Then
        ' Load the graphic to TabPage2 tab
        LoadChart2()
    End If
End Sub

Thanks

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,368 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,768 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 119.2K Reputation points
    2023-07-08T02:43:57.8+00:00

    Try something like this:

    Private Sub Form_Load(sender As Object, e As EventArgs) Handles Me.Load
        LoadCurrentChart()
    End Sub
    
    Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TabControl1.SelectedIndexChanged
        LoadCurrentChart()
    End Sub
    
    Sub LoadCurrentChart()
        If TabControl1.SelectedTab Is TabPage1 Then
            ' Load the graphic to TabPage1 tab
            LoadChart1()
        ElseIf TabControl1.SelectedTab Is TabPage2 Then
            ' Load the graphic to TabPage2 tab
            LoadChart2()
        End If
    End Sub
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.