Wrox Programmer Forums
|
Visual Basic 2010 General Discussion For any discussions about Visual Basic 2010 topics which aren't related to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2010 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 2nd, 2013, 08:50 PM
Authorized User
 
Join Date: Jan 2012
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default need help with error

I received an "InvalidOperationException was unhandled" Error
Here's what it states:
An error occurred creating the form. See Exception.InnerException for details. The error is: The form referred to itself during construction from a default instance, which led to infinite recursion. Within the Form's constructor refer to the form using 'Me.'

Here's a brief description on what I'm trying to do:
On form1 I have a button called btnChangeDate and label called lblDate
On form2 I have a calendar(MonthCalendar1) and a button called btnDate and a textbox called txtDateSelect which would be invisible.(It would be visible for test purposes only).

When form1 opens, it displays the current date in a certain format. If the user wants to select a different date, they would select btnChangeDate which opens form2. Once desired date is selected and the user clicks on button (btnDate) it is then displayed in txtDateSelect.

Here's the code.
Code:
Public Class Form1
Private Sub Label1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lblDate.TextChanged
        Dim a As Integer
        Dim hour As Integer = DateTime.Now().Hour
        Select Case hour
            Case 0 To 6, 23
                a = 1
            Case 7 To 14
                a = 2
            Case Else
                a = 3
        End Select

        'Sets Date Code format

        lblDate.Text = Date.Now.ToString("S" & "yyMMdd" & a)
        Form2.txtDateSelect.Text = Me.lblDate.Text Part of problem is here
    End Sub
    
    Private Sub btnChangeDate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChangeDate.Click
        Form2.ShowDialog()
    End Sub
End Class
Code for form2 (It displays the calendar)
Code:
Public Class Form2

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub

    Private Sub txtDateSelect_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDateSelect.TextChanged
        Form1.lblDate.Text = Me.txtDateSelect.Text Here's where the other part problem is. This line of code and the other on form1 is why I'm getting that message
    End Sub
End Class
To summarize if the user wants to change the current date displayed then they would click on btnChangeDate. Once form2 opens they select date from calendar and press btnDate which then displays selected date in txtDateSelect as well as in lblDate.text located on form1.

Sorry about the long explanation. Hope it makes sense and Thanks for your help.

Last edited by pooky; March 2nd, 2013 at 09:14 PM..
 
Old March 3rd, 2013, 09:49 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

The calls are creating a circular reference and that is the problem. Instead of writing it in Change event you can use in Form Load event

Code:
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
   txtDateSelect.Text = Form1.lblDate.Text 'If the form1 is active - else you need to store in this a variable and pass   
    End Sub
Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old March 4th, 2013, 12:42 AM
Authorized User
 
Join Date: Jan 2012
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

didn't work. nothing happened.
 
Old March 4th, 2013, 07:21 AM
Authorized User
 
Join Date: Jan 2012
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Even tried this:
Me.txtDateSelect.Text=Form1.lblDate.Text
Also didn't work





Similar Threads
Thread Thread Starter Forum Replies Last Post
file system object error. automation error. library not registered shaamir BOOK: Beginning ASP.NET 4 : in C# and VB 1 September 25th, 2012 09:27 AM
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING ngchpg BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 July 16th, 2012 03:28 AM
SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified soufya BOOK: Beginning ASP.NET 4 : in C# and VB 7 September 14th, 2011 08:08 AM
HTTP error 500: Internal Server Error in Load Testing sherin Visual Studio 2008 0 May 19th, 2010 09:02 AM
Insert Query Error & Run-Time Error 3022 DavidWE Access 1 July 31st, 2008 11:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.