Click here to Skip to main content
15,907,497 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Public Sub New()

    InitializeComponent()

End Sub


I use VB.Net and WPF but code keeps getting the message

'InitializeComponent' is not declared.

I can't find anywhere why designer does this.

Thank you

What I have tried:

I have search on Google.
But I can't find .
Posted
Updated 14-May-24 10:53am
v2

1 solution

Just add a method to the class called InitializeComponent. That will do it.

As quator noted, this is code that has a special purpose. The way that WPF works is that the XAML is compiled down to code. Crucially, the code in your class is a partial class.

Reasons for the failure can include the XAML file not being the right resource type. Make sure it's set to Page, not Resource in the Build action. The commonest cause is that you have an incorrect x:Class definition so the compilation doesn't tie the partial classes together.
 
Share this answer
 
v2
Comments
quator 14-May-24 15:23pm    
This is not the correct answer, the rest of the program will not work as it should.
Pete O'Hanlon 14-May-24 15:34pm    
You are, of course, correct. This is what happens when you answer a question from memory.
quator 14-May-24 15:29pm    
InitializeComponent() is created by the designer, but that does not always go well. You have no influence on that. If you are lucky, you close visual studio and then start it again and those "red lines" are gone.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900