שתף באמצעות


Code in Form1.vb or Form1.Designer.vb

Question

Thursday, August 9, 2012 12:08 AM

I am new to VB and am using VB 2005. I am going through a project in a book and everything works as expected. I started my own project and designed a from with many controls.  To start writing code I double clicked a button control on the form.  VB took me to the form1.designer.vb file. In the book example a double click on a button control took me to the form1.vb file (as expected). After much searching for an answer I ended up deleting the project and starting again, same result. How do I get the code into the form1.vb file?

Form1.Designer.vb file has all of the code to set up the form

Form1.vb file is completely blank

In Solution Explorer Form1.vb has an icon with VB written on it

I have not manually edited Form1.vb. I only edited Form1.Designer.vb to remove the code entered when I double clicked the button on the form (I did manually edit these forms on my first appempt and found the hard way it is not the thing for a novice to do).

If I right click on the form and click view code it brings me to the empty Form1.vb file

If I double click the form title area it takes me to the Forn1.designer.vb file

Thanks, Peter

All replies (10)

Thursday, August 9, 2012 5:37 AM ✅Answered

Hallo Peter,

The simplest start, double click on the form and the method for the event and its handler of the load of that form after initializing is created. 

In that you can write your first code.

Never (with some corrections exceptions with large problems) write code in the designer vb file. If you use it is mostly to remove in a quick way controls).

Success
Cor


Thursday, August 9, 2012 12:13 AM

The solution explorer should look like this:

No "icon with VB written" on it.

Did you accidently delete

    Public Class Form1

    End Class

from Form1.vb? That should stay there. If you create a new project or add a new Form, it is there.

Armin


Thursday, August 9, 2012 5:03 AM

Thanks Armin

My Form1.vb icon is similar to your WindowsApplication1 icon but it does not the extra small form.  No, I did not touch the Form1.vb form. I learnt that lesson in my first attempt as previously mentioned

I tried again with a new project. I put one button on the form, double clicked it and the Form1.vb came up. It appears to be something I have done on the "Cutting2" project, but as mentioned, I have not touched either Form1.vb or Form1.designer.vb.

Thanks


Thursday, August 9, 2012 6:09 AM

Thanks Cor

I have just been trying to get Form1.vb(design) to reappear after it disappeared since my last post. I found a solution that said put

Public Class Form1

End Class

into the form.vb file. This returned Form1.vb(design).  It also allows me to enter code in Form1.vb when I double click a button. I guess the question now is why did a double click on a button initially open Form1.designer.vb instead of Form1.vb?

Thanks Peter


Thursday, August 9, 2012 12:53 PM

I guess the question now is why did a double click on a button initially open Form1.designer.vb instead of Form1.vb?

Probably because "class form1...end class" was missing in form1.vb, so the only code file for Form1 was the designer file. Consequently the code was put there.

However, I don't know why form1.vb can be empty. Have a look at

C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplates\VisualBasic\Windows\1031\WindowsApplication.zip

(the bolded parts you probably have to modify and find where it is on your machine)

In the Zip file, there's a Form1.vb. Open it in a text editor. Is the file also empty?

Well, does the problem still occur with new projects or is it solvd now?

Armin


Thursday, August 9, 2012 1:38 PM

Try this .

1.Go to tools at the top of visual studio

2.Then go to import export settings

3.Choose reset all settings

4.And then choose "No, just reset all my settings , overwriting my current settings"

5. Then choose what settings you want to reset to , If you not sure go with visual basic as this is the language you are working with.

I thought it might help.

Google Is My Friend...


Thursday, August 9, 2012 2:55 PM

However, I don't know why form1.vb can be empty. 

Armin

Hi Armin,

This is probably because Form1.Designer.Vb

is a partial class for Form1.

The code within Form1.Vb and Form1.Designer.Vb

are treated ( by the compiler ) as if they were one file.

'

In 2002 and 2003 editions of VB.Net the

'Designer generated code section'

used to reside at the top of Form1.Vb

with an automatically generated comment as a warning which read something like.

'Please do not attempt to alter code in this section using the editor."

'

 You can, in fact, have all your code in one file for a single Form project ( if you choose to do so ) in all versions of VB.Net as far as I know.

 I think you can also do the same for a multiple Form project provided the code is layed out correctly.  ;-)

This is in much the same way that you can write another block of CLASS code below the Form1.Vb last line of End Class

Just to let you know, yes, I have done some experimentation.  :)

Regards,

Click this link to see the NEW way of how to insert a picture into a forum post.

Installing VB6 on Windows 7

App Hub for Windows Phone & XBOX 360 developers.


Thursday, August 9, 2012 3:05 PM

Hi Palpha,

As Cor Ligthert pointed out

you should not need to touch, edit or delete anything from the Form1.Designer.Vb

especially if;

  • You have just started to use VB.Net.
  • You are unsure about what any of the code in there means.

Always write your majority of code within the Form1.Vb file or Form2.Vb etc....

 ( if you are using more than one Form in your project ).

You can also write additional code in other areas. I can tell you more on that later.  :)

Regards,

Click this link to see the NEW way of how to insert a picture into a forum post.

Installing VB6 on Windows 7

App Hub for Windows Phone & XBOX 360 developers.


Thursday, August 9, 2012 3:25 PM

John,

the problem was that the event handler has been automatically created inside form1.designer.vb. It was not Peter's intention to do so. As I said, this was because it was the only file containing the class declaration as it was missing in form1.vb.

Armin


Thursday, August 9, 2012 11:28 PM

Thanks everyone for your valued help.

Cor, the file you directed me to is as it should be.

If I insert a new form it is as it should be. If I start a new project everything is fine. So until next time, thanks again

Peter