Manny265
Manny265

Reputation: 1709

When my form is hidden and reloaded from another form it is not executing the code in the Load event

So i have a program from which I am retrieving data from a datagrid and going through the grid one by one but when i close the form using theMe.Hide and the reload it again from another form using frmQuiz.Show it does not execute the code in the Load event of the frmQuiz form and as a result it ends up on a record I left on from last time. Here is code from the Load event of the form


   Private Sub frmQuiz_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    tracker = 0 'to keep track of which record to retrieve from datagrid,in this case the first

    'TODO: This line of code loads data into the 'CompLitDataSet.tblQuestions' table. You can move, or remove it, as needed.
    Me.TblQuestionsTableAdapter.Fill(Me.CompLitDataSet.tblQuestions)
    hideGrid()
    dgData.DataSource = TblQuestionsTableAdapter.GetAllUnsorted
    Me.StartPosition = FormStartPosition.CenterParent 'load form at center screen
    ReDim answers(TblQuestionsBindingSource.Count)

    lblQuestion.Text = ""
    lblQuestionNumber.Text = ""
    PictureBox1.Visible = False
    radA.Checked = False
    radB.Checked = False
    radC.Checked = False
    radD.Checked = False

    viewQuestions(0) 'show first questions

End Sub

Upvotes: 2

Views: 3345

Answers (4)

Victor Zakharov
Victor Zakharov

Reputation: 26454

You may want to move code from Form_Load handler to VisibleChanged handler instead:

Private Sub Form1_VisibleChanged(sender As Object,
                                 e As System.EventArgs) Handles Me.VisibleChanged
  If CType(sender, Form)).Visible = True Then 'your code here
End Sub

Upvotes: 0

Karl Anderson
Karl Anderson

Reputation: 34844

Me.Hide just makes the form invisible, it is still loaded in memory and accessible via code.

Me.Close unloads the form from memory and is not accessible via code anymore.

Upvotes: 2

Ruben_PH
Ruben_PH

Reputation: 1824

It wont fire your Form_Load event because your are "Hiding" your form.
You should Close your form then Show it again.

Upvotes: 1

Geeky Guy
Geeky Guy

Reputation: 9379

Your form is not being reloaded.

Check the docs: Form.Load Event

Occurs before a form is displayed for the first time.

Unfortunately, there seems to be no event for when the form is being hidden or shown - except when it's show for the very first time, which will trigger Load and then Shown.

There are events for when the form is closing and after it is closed, though. You may want to use those. If the state of the objects related to the form is important for you, you should store those states somewhere you can restore it when the form is recreated.

Upvotes: 1

Related Questions