Click here to Skip to main content
15,883,705 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Visual Studio Editor - Controls disappearing at Design Time

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
12 Jul 2012CPOL4 min read 38.2K   1   5
In Design Mode a form suddenly fails to display its controls even though the project continues to work normally.

The MSDN forums for Visual Studio Editor contain several posts relating to controls disappearing at design time. In Design Mode a form suddenly fails to display its controls even though the project continues to work normally. This is what happened to me – a form with 20+ controls suddenly, for no apparent reason, failed to display those controls even though the program ran perfectly. The posts contain several proposed solutions but I didn’t like the look of any of them. My solution is incredibly simple - but involved doing something Microsoft said you should not do! The best way to explain my solution is to give a simple example you can try for yourself.

Note: I code in Visual Basic. I don't know whether my solution will work for any of the other languages.

  • Create a new project - Windows Forms Application
  • Create a form. In properties (name) it frm1 (frm1). Add a PictureBox (pbx1) and a Button (btn1). Set pictureboxBackgroundImageLayout = Stretch.
  • Create a second form, (name) it frm2. Add a a PictureBox (pbx1), setBackgroundImageLayout = Stretch, and put aBackgroundImage (any image will do) in the picture box.
  • Back to Form1.
  • Double-click the button and in the code editor, add the following line for the button-click: sub:pbx1.BackgroundImage = frm2.pbx1.BackgroundImage.
  • Save the project. Make a note of where you save it to.
  • Run it. Click the button - the image on Form2 appears on Form1.

Now comes the bit to illustrate my solution - the bit I think you are not supposed to do!

  • In Visual Studio, close the project.
  • Run Notepad. Click File/Open and navigate to where you saved the project. Locate fileForm2.Designer.vb. You will need to make sure that Notepad displays 'All files' and not just '*.txt' files.
  • The file is shown below:
VB
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class frm2
Inherits System.Windows.Forms.Form

'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Dim resources As System.ComponentModel.ComponentResourceManager = _
    New System.ComponentModel.ComponentResourceManager(GetType(frm2))
Me.pbx1 = New System.Windows.Forms.PictureBox()
CType(Me.pbx1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'pbx1
'
Me.pbx1.BackgroundImage = CType(resources.GetObject("pbx1.BackgroundImage"), System.Drawing.Image)
Me.pbx1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch
Me.pbx1.Location = New System.Drawing.Point(61, 40)
Me.pbx1.Name = "pbx1"
Me.pbx1.Size = New System.Drawing.Size(175, 145)
Me.pbx1.TabIndex = 0
Me.pbx1.TabStop = False
'
'frm2
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(284, 262)
Me.Controls.Add(Me.pbx1)
Me.Name = "frm2"
Me.Text = "Form2"
CType(Me.pbx1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

End Sub
Friend WithEvents pbx1 As System.Windows.Forms.PictureBox
End Class
  • Observe the lines in bold. The first bold one tells you not to change this code manually!
  • You are now going to change it manually!!
  • The second line Me.Controls.Add(Me.pbx1) - delete it.
  • Save the file - but don't close Notepad.
  • Return to Visual Studio and reload the project.
  • Display Form2 (frm2) in Design Mode and observe that the PictureBox is no longer there.
  • Run the program and observe that it still works even though Form2 (frm2) does not display the PictureBox in Design Mode.
  • Close the project again - don't save it, just close it.
  • Return to Notepad and replace the deleted line Me.Controls.Add(Me.pbx1).
  • Save the file in Notepad.
  • Return to Visual Studio, reload the project, and Run it.
  • Observe Form2 (frm2) now shows the disappearing PictureBox in Design Mode.
  • Run it - and it works.

So if you have a form whose controls have suddenly disappeared for no apparent reason then maybe that is because the Me.Controls.Add(controlname) lines have disappeared. This is what happened in my project. I don't know why they disappeared - they just did! If I did something to make them disappear then I don't know what it was.

To correct the problem you can try my solution - it's very, very simple.

  • Close the project in Visual Studio.
  • Open the appropriate FormName.Designer.vb file in Notepad.
  • Make sure that it contains the line(s) Me.Controls.Add(controlname). There must be one line for each control - so if you have, say, 20 controls on the form, then you need (to add) 20 similar lines. The lines must be placed all together in a block in the position shown in the above code.
  • If you can't remember all the controls then you can find them from the file. Look at the end of the file above and observe the line Friend WithEvents pbx1 As System.Windows.Forms.PictureBox. Every control on the form has one of these Friend entries - so if you put 20 controls on the form then you will see 20 Friend entries. Each Friend entry must have its corresponding Me.Controls.Add line otherwise the control(s) will not appear in Design Mode even though the project will still work.
  • Manually enter all the necessary Me.Controls.Add(xxx) lines.
  • Save the file in Notepad.
  • Return to Visual Studio and reload the project.
  • All should be well.

I don't know if you can do the manual editing without first closing the project in Visual Studio - I can't be bothered to check it out!

Good coding.

License

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


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThe solution is so simple! Pin
ChrisRaisin27-Apr-21 7:40
ChrisRaisin27-Apr-21 7:40 
PraiseThanks. This solution fixes the problem. (It is 2016. The problem still happens occasionally) Pin
vertex434317-Aug-16 7:59
vertex434317-Aug-16 7:59 
QuestionHappened to me too! Pin
krazykatt5-Apr-14 18:02
krazykatt5-Apr-14 18:02 
QuestionThis happened to me.. fixed it in C# Pin
Member 837236611-Apr-13 9:39
Member 837236611-Apr-13 9:39 
AnswerRe: This happened to me.. fixed it in C# Pin
Ern Miller7-Jul-16 13:03
Ern Miller7-Jul-16 13:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.