Link to home
Start Free TrialLog in
Avatar of ACAE
ACAE

asked on

Problem with ancestor on form

Hello,
I have created an ancestor datagrid (inherits from datagrid), which I use to put on forms. Hovewer, I have a problem with Vb.net 2005 : it gives an error when I open the form, and when I have changed the code and save, it changes the code back to what is was and gives an error again.

This is the situation:
- I have put the datagrid on my form, DGROUTE, and linked a dataview to it
- Each time I open the form, I get the error 'Datagrid received an exception at design time. Reset the datasource and Datamember property on the grid'.
- I reset the datagrid
- I immediately get the error 'Name 'dgRoute' is not declared
- I then check my code, and see that vs.net has declared the dgRoute as a local variabel in the 'InitializeComponent' function
- I change it to 'Friend WithEvents dgRoute As YnpClasses.YnpDataGrid', and ad the line 'Me.dgRoute = New YnpClasses.YnpDataGrid' -> The errors disappear
- I go back to the design of the form, and get the same error again: 'Datagrid received an exception at design time. Reset the datasource and Datamember property on the grid'.
- I reset the datagird
- I immediately get the error 'Name 'dgRoute' is not declared
And so on and so on.

I didn't have this problem in vs.net 2003. It seems that vs.net 2005 changes my code incorrectly. I think it is to stupid to do so. It there a way to stop this from happening ? On other forms, this problem also appears but it doesn't change my code
Avatar of indianguru2
indianguru2
Flag of India image

Avatar of ACAE
ACAE

ASKER

Do you mean that I have to add the 'If not me.designmode' too ?
The problem with A User Control is that it doesn't know when the form is called (in Design/Run mode).
So if you write any code on Load / Init which can only work when it is in Run mode, it will crash.
So you have to tell the Control whether its in Design Mode or Run Mode.
Avatar of ACAE

ASKER

I have replaced the code in the New() function of the form to:

Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        If Not Me.DesignMode = True Then
            InitializeComponent()
        End If

        'Add any initialization after the InitializeComponent() call
    End Sub

But this doesn't change the error. Do I have to put other code in the designmode ?
ASKER CERTIFIED SOLUTION
Avatar of indianguru2
indianguru2
Flag of India 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
Avatar of ACAE

ASKER

OK, changed this back. I have checked the form Load, Paint, Init, .. event, and changed all the code. I also dit this on the ancestor, but the problem remains. I also checked all the code of all my ancestor objects, and changed some code and: HURAY!! This works fine now.µ

Thank you very very much for you help
Avatar of ACAE

ASKER

I was a little bit too fast: .Net still changes my code, so this is now the current situation:

This is the situation:
- I open the form with the datagrid on it, and get no error
- when I save the form, I get the error 'Name 'dgRoute' is not declared
- I then check my code, and see that vs.net has declared the dgRoute as a local variabel in the 'InitializeComponent' function (**)
- I change it to 'Friend WithEvents dgRoute As YnpClasses.YnpDataGrid', and ad the line 'Me.dgRoute = New YnpClasses.YnpDataGrid' -> The errors disappear
- Now the form is OK, I close it
- When I reopen it, the problem occurs again. It changes the code (**) again, so I have to reset it.
The strange thing is: this only happes for 1 form, all the other forms I checked are OK.
Can you try and remove that grid and add a new grid with the same name?